Ryan Ryan - 2 months ago 7
SQL Question

What is N'@date referring to?

Can anybody please help me to identify what the

N'@date
is referring to between the two set 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;

Answer

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

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

In your case,

  1. @sql refers to your SQL statement
  2. N'@date datetime' refers to the 1st parameter name and its data type datetime
  3. @dt is the value for the 1st parameter
Comments