• Carlo Romagnano (6/30/2016)


    The second option is useless, but it also is correct.

    SELECT @p = N'@m varchar(200)="event message", @d datetime=getdate(), @U varchar(10)="Steve" '

    I.e. this code returns 1 despite @p is initialized with 1234.

    declare @p int = 1

    exec sp_executesql N'select @p',N'@p int = 1234',@p

    No error is raised.

    Your example works, but the second option does not. You are correct that ="xxx" does not raise an error, but =getdate() does:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '('.

    Msg 137, Level 15, State 2, Line 1

    Must declare the scalar variable "@d".