我有以下查询:
SELECT UsersAccountLink.UserId, Customer.Account.AccountNumber, Web.Customer.Name, Web.Customer.Street, UsersAccountLink.AccountId
FROM UsersAccountLink INNER JOIN
Web.Customer ON UsersAccountLink.AccountId = Web.Customer.AccountId LEFT OUTER JOIN
Customer.Account ON UsersAccountLink.AccountId = Customer.Account.AccountId
WHERE (Customer.Account.AccountNumber IS NULL)
Order by Name
我需要将选择更改为并更新。如果Customer.Account.AcountNumber为NULL,我需要将UsersAccountLink.UserId设置为空guid。
我怀疑我有这个权利,所以这就是我想出来的:
UPDATE
UsersAccountLink
SET
UsersAccountLink.AccountId = '00000000-0000-0000-0000-000000000000'
FROM
UsersAccountLink
INNER JOIN
Web.Customer ON UsersAccountLink.AccountId = Web.Customer.AccountId
LEFT OUTER JOIN
Customer.Account ON UsersAccountLink.AccountId = Customer.Account.AccountId
WHERE
(Customer.Account.AccountNumber IS NULL)
我的帖子基于How do I UPDATE from a SELECT in SQL Server?
UPDATE
Table
SET
Table.col1 = other_table.col1,
Table.col2 = other_table.col2
FROM
Table
INNER JOIN
other_table
ON
Table.id = other_table.id
两个问题:
谢谢!
答案 0 :(得分:3)
This is what you can do to test
insert a select statment here to view the record(s) before the insert
BEgin tran
UPDATE
UsersAccountLink
SET
UsersAccountLink.AccountId = '00000000-0000-0000-0000-000000000000'
FROM
UsersAccountLink
INNER JOIN
Web.Customer ON UsersAccountLink.AccountId = Web.Customer.AccountId
LEFT OUTER JOIN
Customer.Account ON UsersAccountLink.AccountId = Customer.Account.AccountId
WHERE
(Customer.Account.AccountNumber IS NULL)
insert a select statment here to view the record(s) after the insert
Rollback tran
最好不要在开发时这样做。在您遇到的特定情况下,我还想测试如果稍后在不同记录上运行会发生什么。或者至少检查一下,确保你没有一个独特的指数。
我也喜欢这样做:
UPDATE
UsersAccountLink
SET
UsersAccountLink.AccountId = '00000000-0000-0000-0000-000000000000'
--SELECT * (or you can specify columns you specifically want to see)
FROM
UsersAccountLink
INNER JOIN
Web.Customer ON UsersAccountLink.AccountId = Web.Customer.AccountId
LEFT OUTER JOIN
Customer.Account ON UsersAccountLink.AccountId = Customer.Account.AccountId
WHERE
(Customer.Account.AccountNumber IS NULL)
这使您可以通过运行注释选择轻松检查。通过编写select,然后将更新信息放在其上并在FROM statmenet之前评论select中的所有内容,可以轻松地将select更改为更新。