Escaping single quote in SQL Server

前端 未结 4 975
栀梦
栀梦 2020-12-03 10:34

I was trying to execute the below statement to escape single quotes (i.e. using two single quotes):

declare @year varchar(max)
set @year = \'111,11\';
exec (         


        
4条回答
  •  心在旅途
    2020-12-03 11:32

    Ok... you want to take this string:

    SELECT * FROM SplitValues(@year , ',')
    

    And make it a string like this:

    'SELECT * FROM SplitValues('111,11' , '','')'
    

    So, your final code would be:

    declare @year varchar(max), @sql varchar(max)
    set @year = '111,11';
    set @sql = 'SELECT * FROM SplitValues(''' + @year + ''' , '''','''')'
    
    select @sql
    

    Actually, finally select you would use exec() instead. However you really should probably use sp_sqlexecute for stuff like this since you can use paramaterized queries.

提交回复
热议问题