What does N '@date mean?

Can someone help me determine what it means N'@datebetween the two installed measures in the following exec statement?

declare @sql nvarchar(4000), @dt datetime;

set @dt = '2015-10-01';

set @sql = N'select * from dim_person

where person_sid
in
(
select max(person_sid)  from dim_person

where [Effective_To] <= @date

group by person_id_number
)';

exec sp_executesql @sql, N'@date datetime',@dt;
+4
source share
3 answers

According to MSDN - sp_executesql (Transact-SQL) , the syntax for sp_executesql:

sp_executesql [ @stmt = ] statement  
[   
  { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }   
     { , [ @param1 = ] 'value1' [ ,...n ] }  
]

In your case

  • @sql refers to your SQL statement
  • N'@date datetime' refers to the first parameter name and its data type datetime
  • @dt - value for the first parameter
+2
source

It is not just N '@date, it is N' @date datetime ', and it passes the @date variable declaration parameter to sp_executesql as nvarchar value.

PS: , , SQL- . sp_executesql . Datetime, , . , "20151001". .

+2

sp_executesqlexpects an input parameter in a unicode string. So you need to pass NVARCHAR(x)-string

SELECT 'hello'  --creates the string "hello" as VARCHAR

SELECT N'hello' --create the string "hello" as unicode string (NVARCHAR)

in your line

exec sp_executesql @sql, N'@date datetime',@dt;

... N'@date datetime'is one input parameter.

Here you can learn more about sp_executesql

+2
source

Source: https://habr.com/ru/post/1655295/


All Articles