使用变量运行动态SQL时的单引号数量

时间:2019-01-10 16:08:01

标签: sql-server tsql dynamic-sql

我很难确定下面的SQL语句中应该包含的'的数量:

declare @sql varchar(max)
declare @LetterID varchar(max) = 'c01as1'
set @sql = 
'SELECT fltr.tency_seq_no FROM OPENQUERY(loopback, 
    ''SET FMTONLY OFF; EXEC BST.[LET].[LETTERBUILD] @LetterCode = 
        ''''\\SVR-QL4APPLIVE\QLSHAREPOINT\LETTERS\DATAFILES\
            '''+@LetterID+'''
        .csv''''
    WITH RESULT SETS (tency_seq_no VARCHAR(255))''
) AS fltr'
exec (@sql)

当前错误:

  

第15级状态1行3的消息102
  'c01as1'附近的语法不正确

仅需说明,错误是在动态SQL中引用@LetterID变量,而不是在声明参数时

@ SQL的打印

SELECT fltr.tency_seq_no 
FROM OPENQUERY(loopback, 'SET FMTONLY OFF; EXEC BST.[LET].[LETTERBUILD] @LetterCode = ''\\SVR-QL4APPLIVE\QLSHAREPOINT\LETTERS\DATAFILES\'c01as1'.csv''
     WITH RESULT SETS (tency_seq_no VARCHAR(255));'
    ) AS fltr

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

我认为该错误与原始代码中不需要的换行符等有关。尝试以下方法:

DECLARE @sql VARCHAR(MAX);
DECLARE @LetterID VARCHAR(MAX) = 'c01as1';

SET @sql = 'SELECT fltr.tency_seq_no FROM OPENQUERY(loopback, 
    ''SET FMTONLY OFF; EXEC BST.[LET].[LETTERBUILD] @LetterCode = ''''\\SVR-QL4APPLIVE\QLSHAREPOINT\LETTERS\DATAFILES\'
           + @LetterID + '.csv''''
    WITH RESULT SETS (tency_seq_no VARCHAR(255))''
) AS fltr';

EXEC (@sql);

答案 1 :(得分:1)

几个月前,当我遇到这个困难时,我创建了一个对动态SQL的引号进行双重处理的函数。该标量函数可以执行此任务,而不是每次都手动搜索需要加倍报价的字符串。这样可以防止在执行将来的修改(例如添加其他变量)以及提高可读性时可能使脚本混乱。

功能如下:

CREATE FUNCTION dbo.fn_duplicateQuotes
    (@string varchar(max),
    @level int)
RETURNS varchar(max)
AS
BEGIN
    /*Doubles-up quotation marks for nested dynamic SQL
      level can be set greater than 1 to add additional doubled-up quotes
      for further nested dynamic SQL*/

    /*Double up quotes*/
        set @string = REPLACE(@string, '''', REPLICATE('''', (@level) * 2))

    /*Return Value*/
        return @string
END

动态SQL如下:

declare @SQL nvarchar(max)
declare @LetterID varchar(max) = 'c01as1'

set @SQL = 'SET FMTONLY OFF; EXEC BST.[LET].[LETTERBUILD] @LetterCode = 
              ''\\SVR-QL4APPLIVE\QLSHAREPOINT\LETTERS\DATAFILES\' + @LetterID + '.csv''
             WITH RESULT SETS (tency_seq_no VARCHAR(255));'

set @SQL = 'SELECT fltr.tency_seq_no FROM OPENQUERY(loopback,
        ''' + dbo.fn_duplicateQuotes(@SQL, 1) + '''
        ) AS fltr'

print @SQL
exec (@SQL)

@SQL的打印返回:

SELECT fltr.tency_seq_no FROM OPENQUERY(loopback,
        'SET FMTONLY OFF; EXEC BST.[LET].[LETTERBUILD] @LetterCode = 
              ''\\SVR-QL4APPLIVE\QLSHAREPOINT\LETTERS\DATAFILES\c01as1.csv''
             WITH RESULT SETS (tency_seq_no VARCHAR(255));'
        ) AS fltr