【问题标题】:change a query to an update将查询更改为更新
【发布时间】:2014-09-16 14:44:09
【问题描述】:

我有以下疑问:

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

两个问题:

  1. 有没有办法在实际运行之前测试运行查询,例如更新或删除?
  2. 我是否正确地将其转换为更新?

谢谢!

【问题讨论】:

  • 在不运行的情况下测试代码?我不知道.. 你不能为了测试目的复制数据库并运行它吗?
  • 在不实际更新或删除记录的情况下运行。该数据库很大,位于托管服务器上。我需要很长时间才能在本地下拉并运行它。也就是说,如果我被授予拉取整个数据库的权限。 :)

标签: tsql sql-server-2012-express


【解决方案1】:
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

最好还是在 dev 而不是 prod 上这样做。在您遇到的特定情况下,我还想测试如果稍后在不同的记录上运行会发生什么。或者至少检查以确保您的 accountid 没有唯一索引。

我也喜欢这样做:

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)

这使您可以通过运行带注释的选择轻松检查。它还可以轻松地将选择更改为更新,方法是编写选择,然后将更新信息放在它上面并在 FROM statmenet 之前评论选择中的所有内容。

【讨论】:

  • 我刚刚完成评论以使用交易 :)
  • 你必须对你的数据进行测试,但对我来说它看起来没问题。但是,我不熟悉您的数据库结构或数据的含义,所以我不能说它是否正确,除非语法(查询中最不重要的部分)似乎很好。
猜你喜欢
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2020-12-29
  • 2018-06-20
相关资源
最近更新 更多