如何使用唯一值更新表中的列,但不更新所有行,而不是递增

时间:2012-10-11 12:44:05

标签: sql

  

可能重复:
  Is it possible to perform multiple updates with a single UPDATE SQL statement?

我正在更新一个包含特定列的新值的表,如下所示,所以我在prepayId指示的某些fows上更新表Prepay,列PrepayTransactionDesc

update Prepay
set PrepayTransactionDesc = 'Funded Repeat Voucher 153429'
where prepayId = 58045

它工作正常,但我想做多个更新。 我尝试了几种不同的方法,但都失败了。是我试图这样做的时候偶然使用CASE。

例如,我有以下prepayid和prepaytransactiondesc字段

57770   Funded Repeat Voucher 153118
57771   Funded Repeat Voucher 153119
57772   Funded Repeat Voucher 153120
57773   Funded Repeat Voucher 153121
57774   Funded Repeat Voucher 153122
57775   Funded Repeat Voucher 153123
57776   Funded Repeat Voucher 153124
57894   Funded Repeat Voucher 153276
57895   Funded Repeat Voucher 153277
57896   Funded Repeat Voucher 153278

我将如何一次性完成它们?它不能那么困难但我似乎在尝试完成任务时遗漏了一些东西。

4 个答案:

答案 0 :(得分:1)

你可以尝试

UPDATE Prepay
SET PrepayTransactionDesc = 
   CASE
     WHEN prepayID = 57770 THEN 'Funded Repeat Voucher 153118'
     WHEN prepayID = 57771 THEN 'Funded Repeat Voucher 153119'
     WHEN prepayID = nextID THEN 'Next Value' 
   END
WHERE  prepayID in (1,2,4,57770... all your ids here)

答案 1 :(得分:0)

将数据更新为临时变量表​​。然后将其与要更新的表一起加入更新语句的FROM子句中。这将允许您在一个语句中更新多行。

DECLARE @data TABLE (id INT, desc VARCHAR(200))
INSERT @data VALUES (57770, 'Funded Repeat Voucher 153118')
INSERT @data VALUES (57771, 'Funded Repeat Voucher 153119')

UPDATE Prepay
SET PrepayTransactionDesc = @data.desc
FROM Prepay
    INNER JOIN @data ON Prepay.prepayID = @data.id

答案 2 :(得分:0)

update Prepay
  set PrepayTransactionDesc = 
    case
      when prepayId = 58045 then 'Funded Repeat Voucher 153429'
      when prepayId = 57770 then 'Funded Repeat Voucher 153118'
    end
where
  prepayId in (58045 , 57770)

显然添加其他值

答案 3 :(得分:0)

Update Prepay SET PrepayTransactionDesc = 'Funded Repeat Voucher 153429' WHERE  prepayID  BETWEEN yourid AND yourid