如何更新表中的值

时间:2010-01-28 03:07:25

标签: sql sql-server tsql

我有以下select语句。 我想将ContactAssociate的值从'Bob'更新为'Jane'。有可能这样做吗?

SELECT TOP (1500) ContactID, ContactNotes, 
  ContactAssociate, ContactAppointment
FROM  
  tb_Contact
WHERE 
 (ContactAssociate = 'Bob') AND 
 (ContactAppointment = 0)

2 个答案:

答案 0 :(得分:5)

使用:

UPDATE TB_CONTACT
   SET contactassociate = 'Jane'
 WHERE contactassociate = 'Bob'

您提供的contactappointment检查的查询为零 - 如果您想要包含该检查,则查询将为:

UPDATE TB_CONTACT
   SET contactassociate = 'Jane'
 WHERE contactassociate = 'Bob'
   AND contactappointment = 0

UPDATE语句不支持TOP关键字,因此您的更新语句将为:

UPDATE TB_CONTACT
   SET contactassociate = 'Jane'
 WHERE contactid IN (SELECT TOP (1500)
                            contactid 
                       FROM TB_CONTACT
                      WHERE contactassociate = 'Bob'
                        AND contactappointment = 0)

...但是这将提供随机联系 - 您应该在子查询中指定ORDER BY子句以获得一致的结果。

参考:

答案 1 :(得分:1)

UPDATE tb_Contact SET ContactAssociate='Jane'
 WHERE ContactID IN
      (
           SELECT TOP (1500) ContactID
           FROM  tb_Contact
           WHERE (ContactAssociate = 'Bob') AND (ContactAppointment = 0)
      )
  • 有关UPDATE的一般教程,请参阅here
  • 有关MSSQL的具体参考,请参阅here