我想执行一个包含两列连接的查询,所以我已经这样做了:
Select
Id, PtName + ' ('+Investigation+')' as PtName, Y, M, D, PtCode
From
DiagMain
但是当我尝试参数化此查询时,它无法正常工作。
像这样:
declare @Query nvarchar(MAX)
set @Query = 'Select Id, PtName + ''( +''Investigation''+ )'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'
Exec(@Query)
我在这里做错了什么?
答案 0 :(得分:4)
您的单引号放错了位置,它们应围绕括号(
& )
:
set @Query = 'Select Id, PtName + ''('' +Investigation+ '')'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'
您可以使用print命令调试它:
print @Query
答案 1 :(得分:1)
DECLARE @Query NVARCHAR(MAX)
SET @Query ='Select Id, PtName + '' (''+Investigation+'')'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'
PRINT @Query
Exec(@Query)
答案 2 :(得分:0)
如果您需要执行上述查询,请将exec sp_executesql @Query
替换为declare @Query nvarchar(MAX)
set @Query = 'Select Id, PtName + ''( +''Investigation''+ )'' as PtName, Y, M, D, Sex, PtCode FROM DiagMain'
exec sp_executesql @Query
因为要运行动态查询,您需要调用sql内置过程。因此,您需要运行查询
$>./file.sh | cat -e
快乐编码: - )