一句拆成两句,引号翻倍增加
DECLARE @zong nvarchar(4000), @fen1 nvarchar(4000), @fen2 nvarchar(4000) --一句 sql用两个单引号包围,即''xx'' SET @zong = 'SELECT @a=count(*) FROM OPENQUERY([xx.xx.xx.xx],'' sql '')' EXEC sp_executesql @zong, N'@a int out', @f out --变两句,sql内部的字符串为''''xx'''' ,变量类变为 ' ''''+@date+'''' ' SET @fen1= 'SELECT @a=count(*) FROM OPENQUERY([xx.xx.xx.xx],'' ' SET @fen2='sql '')' EXEC sp_executesql @fen1+@fen2, N'@a int out', @f out