动态tsql错误

时间:2013-09-18 20:09:23

标签: sql sql-server tsql

下面的SQL失败并给我一个错误:

Incorrect syntax near '-'.

变量valu1是一个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     

3 个答案:

答案 0 :(得分:2)

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

注意额外的报价。另请注意,这非常容易受到SQL注入攻击。您应该使用sp_executesql进行调查,而不是使用连接。

答案 1 :(得分:1)

请尝试在变量周围添加更多引号:

''' + @valu1 + '''  as valu1

答案 2 :(得分:0)

当你SET @sqlStr字符串的结尾缺少一个结尾的单引号时。 '之后应该有Order By a.col1

我在 SQL Fiddle 上尝试了这个并且它有效:

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)  

您可能还需要将@ valu1和@ valu2括在单引号中,因为它们是字符串。您将需要使用双单引号来在字符串中转义它们。

''' + @valu1 + '''  as valu1,
''' + @valu2 + ''' as valu2