我有一个临时表,其中包含多个不同的客户记录,如果它们具有相同的电子邮件地址,我想要更新同一列中的所有行:SQL Server - 在UPDATE语句中加入
CustomerID | Email | Pref1 | Pref2 | Pref3
-----------------------------------------------------
1234 [email protected] 1 0 0
1235 [email protected] 1 1 0
1236 [email protected] 0 0 1
1237 [email protected] 0 0 0
1238 [email protected] 1 0 0
应该改为:
CustomerID | Email | Pref1 | Pref2 | Pref3
-----------------------------------------------------
1234 [email protected] 1 1 1
1235 [email protected] 1 1 1
1236 [email protected] 1 1 1
1237 [email protected] 1 0 0
1238 [email protected] 1 0 0
目前,我有一个while循环(循环比那些尚未更新的行),包含一个内部光标遍历我想更新的列(PREF1 ,Pref2,Pref3等)。这是有效的,但是会在更大的记录集上永久存在。
我如何修改如下:
UPDATE #table
SET Pref1 =
(
SELECT MAX(IsNull(Pref1,0))
FROM #table
WHERE Email = '[email protected]'
)
WHERE Email = '[email protected]'
,这样,而不是传递每一个电子邮件地址,在某种程度上是指它的更新记录的电子邮件地址?
UPDATE #table
SET Pref1 =
(
SELECT MAX(IsNull(Pref1,0))
FROM #table
WHERE Email = #table.email
)
(上面的查询不起作用,它只是更新了所有记录的是整列1,如果该列存在)。我应该使用某种更新连接吗?
完美,正是我一直在寻找。谢谢! – TheTor 2012-02-29 10:42:22