下面的 SQL 失败并给我一个错误:
Incorrect syntax near '-'.
变量 value1 是一个 GUID,其中包含“-”字符。我将它用作 varchar
Declare @valu1 as varchar(1000)
Declare @valu2 as varchar(200)
Declare @sqlStr as nvarchar(2000)
Select @valu1 = GUID,
@valu2= RationaleText
From dbo.tblll
Where column= 'New'
SET @sqlStr =N'Insert Into dbo.table1
Select newid() as ChangeID,
GETDATE() as DateModified,
a.col1 as col1,
c.col2 as col2,
d.col3 as col3,
' + @valu1 + ' as valu1,
' + @valu2 + ' as valu2
From table1 a
Inner Join table2 b On a.col1 = b.col7
Left Join table2 c On b.col1 = c.col2
Left Join table3 d On b.col1 = d.col3
Order By a.col1
PRINT @sqlStr
Exec(@sqlStr)
当我打印上面的 SQL - 我得到:
Insert Into dbo.table1
Select newid() as ChangeID,
GETDATE() as DateModified,
a.col1 as col1,
c.col2 as col2,
d.col3 as col3,
DD989A2A-5B9C-4369-81E1-13C638F1C555 as valu1,
New as valu2
From table1 a
Inner Join table2 b On a.col1 = b.col7
Left Join table2 c On b.col1 = c.col2
Left Join table3 d On b.col1 = d.col3
Order By a.col1