E
E
ellz2019-02-09 00:28:21
Transact SQL
ellz, 2019-02-09 00:28:21

Why is the procedure not updated?

There is a procedure:

CREATE PROCEDURE [dbo].[AddHotel]
  @adress nvarchar,
  @rating int,
  @name nvarchar,
  @line int,
  @distanceToBeach int, 
  @distanceToAirport	INT,
  @cityName nvarchar
AS
BEGIN
DECLARE @CityID int
set @CityID = (SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = @cityName)

  INSERT INTO [dbo].[Hotels]
           ([Name],[Address],[Rating], [DistanceToBeach], [DistanceToAirport], [Line], [Discription])
     VALUES
           (@name, @adress, @rating, @distanceToBeach, @distanceToAirport, @line, 'some discr')
END

If left as is, the cityId query will return null. If you do this:
SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = N'Название города'
, that is, it will return the id as needed.
But I take the data for sampling from the input parameter.
Decided to do this:
set @CityID = (SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = N(@cityName))

And I get procedure update error -
An error occurred while the batch was being executed

Cities.Name - is of type nvarchar

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2019-02-09
@ellz

You have not specified the dimension of the input parameters NVARCHAR.

Pointed out - did not help.
Check the input value.
Of course you need to use a debugger. Homemade check:
DECLARE @cityNameTest nvarchar(100)
SET @cityNameTest = N'Название города'
SELECT 1 WHERE @cityName = @cityNameTest

Nbefore the parameter means not UNICODE, but a function, and what is indicated in the error.
If the uncertainty remains, you need to look at the sorting options Cities.Name, which affects the comparison of strings. COLLATE .
Addition. Since you are passing the city not by code, but by name, you need to handle its absence in the database:
SELECT @CityID = Id FROM Cities WHERE [Name] = @cityName
IF @CityID IS NULL BEGIN
  INSERT Cities([Name]) VALUE (@cityName)
  SET @CityID = IDENT_CURRENT('Cities')
END

There INSERT [Hotels]is no area code field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question