内联SQL不更新

时间:2015-06-21 17:54:30

标签: sql-server-2008

我的print语句每次都失败,我的exec也是如此,每当我尝试运行此语句时,它只会告诉我命令已成功完成。我添加了print语句以试图查看实际执行的内容,但我仍然对此感到惊讶。有人可以帮助我吗?

不应该打印语句至少告诉我我想要运行的是什么?如果我在尝试运行更新之前单独打印每个变量,它会显示正确的值,所以我只能假设我的更新语句有问题吗?

Declare @fulldata varchar(30), @rsi varchar(50), @employeename varchar(50), @email varchar(50), @rsi2 varchar(50), @email2 varchar(50),
    @rsiID varchar(50), @calldate datetime, @calltime datetime, @orderdate datetime, @email3 varchar(50), @uniqueID int, @sql varchar(max)  


Set @fullData = 'tvdb'
Set @rsi = 'Alphabet'
Set @employeename = 'Mike Jones'
Set @email = '123abc@gmail.com'
Set @rsi2 = 'Broccoli'
Set @email2 = 'abc123@gmail.com'
Set @rsiID = 'alt16bc'
Set @calldate = '06/15/2015'
Set @calltime = '12:15:00'
Set @orderdate = '06/16/2015'
Set @email3 = 'pineapple1841@gmail.com'
Set @uniqueID =     172855  

    Set @sql = 
    'update '+@fulldata+' '
    + 'set rsi = COALESCE('''+@rsi+''',''''), '
    + 'employeename = COALESCE('''+@employeename+''',''''), '
    + 'email = COALESCE('''+@email+''',''''), '
    + 'rsi2 = COALESCE('''+@rsi2+''',''''), '
    + 'email2 = COALESCE('''+@email2+''',''''), '
    + 'rsiID = COALESCE('''+@rsiID+''',''''), '
    + 'calldate = COALESCE('''+CAST(@calldate As Varchar)+''',''''), '
    + 'calltime = COALESCE('''+CAST(@calltime As Varchar)+''',''''), '
    + 'orderdate = COALESCE('''+CAST(@orderdate As Varchar)+''',''''), '
    + 'email3 = COALESCE('''+@email3+''','''') '
    + 'where uniqueID = '+CAST(@uniqueID As Varchar)+' and '+CAST(@uniqueID As Varchar)+' > 0 '
    Print @sql
    exec (@sql)

编辑--- 如果我尝试将我的语句插入表中以检查它是否为空。这导致我为什么没有分配@sql?

Insert Into #SqlStatement (sql12) VALUES (@sql)

Select * FROM #SqlStatement

1 个答案:

答案 0 :(得分:1)

您确定此处包含所有行吗?你可以运行它没有错误?

至少你没有声明@sql。

即使你声明@sql,这一行也会给你错误:

  Set uniqueID =  172855  

应该是

 Set @uniqueID =  172855  

如果没有为@uniqueID赋值,则整个@sql为NULL,print将生成NO输出。

  update tvdb set rsi = COALESCE('Alphabet',''), employeename = COALESCE('Mike Jones',''), email = COALESCE('123abc@gmail.com',''), rsi2 = COALESCE('Broccoli',''), email2 = COALESCE('abc123@gmail.com',''), rsiID = COALESCE('alt16bc',''), calldate = COALESCE('Jun 15 2015 12:00AM',''), calltime = COALESCE('Jan  1 1900 12:15PM',''), orderdate = COALESCE('Jun 16 2015 12:00AM',''), email3 = COALESCE('pineapple1841@gmail.com','') where uniqueID = 172855 and 172855 > 0 
  Msg 208, Level 16, State 1, Line 1
  Invalid object name 'tvdb'.

要调试代码,您可以注释掉一些行:

Set @sql = 
'update '+@fulldata+' '
+ 'set rsi = COALESCE('''+@rsi+''',''''), '
---+ 'employeename = COALESCE('''+@employeename+''',''''), '
----+ 'email = COALESCE('''+@email+''',''''), '
----+ 'rsi2 = COALESCE('''+@rsi2+''',''''), '
----+ 'email2 = COALESCE('''+@email2+''',''''), '
----+ 'rsiID = COALESCE('''+@rsiID+''',''''), '
----+ 'calldate = COALESCE('''+CAST(@calldate As Varchar)+''',''''), '
----+ 'calltime = COALESCE('''+CAST(@calltime As Varchar)+''',''''), '
----+ 'orderdate = COALESCE('''+CAST(@orderdate As Varchar)+''',''''), '
----+ 'email3 = COALESCE('''+@email3+''','''') '
----+ 'where uniqueID = '+CAST(@uniqueID As Varchar)+' and '+CAST(@uniqueID As Varchar)+' > 0 '
Print @sql
exec (@sql)

并取消注释一行,直到找到有问题的行。

要捕获值并确保你有一个非null的@sql,你需要以这种方式使用COALESCE:

Set @sql = 
  'update '+@fulldata+' ' ...
  + 'email = '''+COALESCE(@email,'')+''','