What is the advantage of using @ParmDefinition in sp_executesql

前端 未结 3 990
迷失自我
迷失自我 2021-01-21 13:08
DECLARE @id int
DECLARE @name nvarchar(20)
SET @id = 5
SET @name = \'Paul\'

What is the difference between these two options:

Set @SQLQ         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-21 13:50

    You avoid having stringly-typed code - where you have to convert everything into a string so that you can shove it into the @SQLQueryInnen parameter, and then introduce issues because you have to work out how to safely and unambiguously perform the conversions to and from the strings back into the correct original data types.

    For ints, the conversion issues aren't very apparent. But if you look at the number of issues people report (here, and on other forums) where they have issues converting between datetimes and strings, you'll realise that it does cause real issues. Best to keep the data as its natural type throughout.

提交回复
热议问题