Is there a way to make a TSQL variable constant?

前端 未结 12 1059
别那么骄傲
别那么骄傲 2021-02-01 11:31

Is there a way to make a TSQL variable constant?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 12:24

    If you are interested in getting optimal execution plan for a value in the variable you can use a dynamic sql code. It makes the variable constant.

    DECLARE @var varchar(100) = 'some text'
    DECLARE @sql varchar(MAX)
    SET @sql = 'SELECT * FROM table WHERE col = '''+@var+''''
    EXEC (@sql)
    

提交回复
热议问题