我正在对 SQL Server 临时表进行简单更新。当我使用三个单独的语句更新单个列时,我得到不一致的结果,但是当我在单个 SQL 语句中更新同一列时,我得到了预期的期望。请有人指出两者之间的区别。我错过了什么吗?
这是SQL:
CREATE TABLE #EmailChanges
(
OldEmail varchar(100),
NewEmail varchar(100)
)
INSERT INTO #EmailChanges(OldEmail)
VALUES
('no.body@ccc.abc.com'),
('foo.bar@abc.com'),
('any.body@xyz.com')
UPDATE #EmailChanges SET NewEmail = replace(OldEmail, '@ccc.abc.com', '@new.domain.com')
UPDATE #EmailChanges SET NewEmail = replace(OldEmail, '@xyz.com', '@new.domain.com')
UPDATE #EmailChanges SET NewEmail = replace(OldEmail, '@abc.com', '@new.domain.com')
-- If I uncomment below sql and comment above three updates, I get desired output.
-- UPDATE #EmailChanges SET NewEmail = replace(replace(replace(OldEmail, '@ccc.abc.com', '@new.domain.com'), '@xyz.com', '@new.domain.com'), '@abc.com', '@new.domain.com')
SELECT * FROM #EmailChanges
这给了我以下结果:
OldEmail NewEmail
--------------------- --------------------------
no.body@ccc.abc.com no.body@ccc.abc.com
foo.bar@abc.com foo.bar@new.domain.com
any.body@xyz.com any.body@xyz.com
有任何想法吗?