我的问题是我想在我的更新查询中使用这个局部变量,有没有办法做到这一点?
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+''',''' , '') + cast(int_guid as varchar(max))
FROM ex_in
SELECT '''' + @listStr + ''''
更新查询
update dbo.ex_in
SET int_action = CASE WHEN int_action = 120 THEN 110 WHEN int_action = 220 THEN 210 ELSE int_action END
WHERE int_action IN (120,220)
AND int_guid in(@listStr)
答案 0 :(得分:0)
不,因为字符串变量上的in
没有达到预期效果。
但是,为什么要为变量烦恼呢?只是做:
update dbo.ex_in
SET int_action = (CASE WHEN int_action = 120 THEN 110 WHEN int_action = 220 THEN 210 ELSE int_action END)
WHERE int_action IN (120, 220) AND
int_guid in (SELECT int_guid FROM ex_in);
当然,现在我写这个,in
逻辑是不必要的,因为这两个查询都在同一个表上并且没有过滤。只需使用:
update dbo.ex_in
SET int_action = (CASE WHEN int_action = 120 THEN 110 WHEN int_action = 220 THEN 210 ELSE int_action END)
WHERE int_action IN (120, 220);
答案 1 :(得分:0)
我能想到的唯一方法是使用动态SQL,如下所示:
DECLARE @sql varchar(MAX);
SET @sql = '
UPDATE dbo.ex_in
SET int_action = CASE WHEN int_action = 120 THEN 110
WHEN int_action = 220 THEN 210
ELSE int_action
END;
WHERE int_action IN (120,220)
AND int_guid IN (' + @listStr + ')
';
EXEC @sql;