如何在EXEC中包含'(引用)

时间:2012-08-20 06:55:50

标签: sql-server tsql

在以下T-SQL代码中,如何将@NameP括在引号中?

EXEC('xp_cmdshell ''echo IF EXISTS(SELECT * FROM sys.objects WHERE name = ' + @Name + ' AND type = P)>> C:\test.txt'', NO_OUTPUT')`

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

declare @Name varchar(50)='test'

EXEC('xp_cmdshell ''echo IF EXISTS(SELECT * FROM sys.objects 
WHERE name = ''''' + @Name + ''''' AND type = ''''P'''')>> 
C:\test.txt'', NO_OUTPUT')