I have the following query:
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
I need to change the select to and update. I need the UsersAccountLink.UserId to be set to an empty guid if Customer.Account.AcountNumber IS NULL.
I doubt I have this right so here is what I came up with:
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)
I based that off this post 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
Two questions:
- Is there a way to test run a query, such as an update or delete, before actually running it?
- Did I convert it to an update correctly?
Thanks!