SQL 通过 SELECT 语句更新多个字段 FROM
这可行,但我想删除冗余。 有没有办法将更新与单个选择语句合并,这样我就不必使用变量?
DECLARE
@OrgAddress1 varchar,
@OrgAddress2 varchar,
@OrgCity varchar,
@OrgState varchar,
@OrgZip varchar,
@DestAddress1 varchar,
@DestAddress2 varchar,
@DestCity varchar,
@DestState varchar,
@DestZip varchar
SELECT
@OrgAddress1 = OrgAddress,
@OrgAddress2 = OrgAddress2,
@OrgCity = OrgCity,
@OrgState = OrgState,
@OrgZip = OrgZip,
@DestAddress1 = DestAddress,
@DestAddress2 = DestAddress2,
@DestCity = DestCity,
@DestState = DestState,
@DestZip = DestZip
FROM
ProfilerTest.dbo.BookingDetails
WHERE
MyID=@MyID
UPDATE SHIPMENT
SET
OrgAddress1 = @OrgAddress1,
OrgAddress2 = @OrgAddress2,
OrgCity = @OrgCity,
OrgState = @OrgState,
OrgZip = @OrgZip,
DestAddress1 = @DestAddress1,
DestAddress2 = @DestAddress2,
DestCity = @DestCity,
DestState = @DestState,
DestZip = @DestZip
WHERE
MyID2=@ MyID2
This works, but i would like to remove the redundancy.
Is there a way to merge the update with a single select statement so i don't have to use vars?
DECLARE
@OrgAddress1 varchar,
@OrgAddress2 varchar,
@OrgCity varchar,
@OrgState varchar,
@OrgZip varchar,
@DestAddress1 varchar,
@DestAddress2 varchar,
@DestCity varchar,
@DestState varchar,
@DestZip varchar
SELECT
@OrgAddress1 = OrgAddress,
@OrgAddress2 = OrgAddress2,
@OrgCity = OrgCity,
@OrgState = OrgState,
@OrgZip = OrgZip,
@DestAddress1 = DestAddress,
@DestAddress2 = DestAddress2,
@DestCity = DestCity,
@DestState = DestState,
@DestZip = DestZip
FROM
ProfilerTest.dbo.BookingDetails
WHERE
MyID=@MyID
UPDATE SHIPMENT
SET
OrgAddress1 = @OrgAddress1,
OrgAddress2 = @OrgAddress2,
OrgCity = @OrgCity,
OrgState = @OrgState,
OrgZip = @OrgZip,
DestAddress1 = @DestAddress1,
DestAddress2 = @DestAddress2,
DestCity = @DestCity,
DestState = @DestState,
DestZip = @DestZip
WHERE
MyID2=@ MyID2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
像这样的东西应该可以工作(现在无法测试它 - 根据记忆):
这有帮助吗?
Something like this should work (can't test it right now - from memory):
Does that help?
您可以使用:
You can use:
您应该能够按照以下
FROM 语句进行一些操作,可以使其更加优化(使用更具体的连接),但上面的方法应该可以解决问题。另外,以这种方式编写的一个很好的附带好处是,可以预览 UPDATE 更改
UPDATE s SET
来读取SELECT
!然后,您将看到更新发生后出现的数据。You should be able to do something along the lines of the following
FROM statement can be made more optimial (using more specific joins), but the above should do the trick. Also, a nice side benefit to writing it this way, to see a preview of the UPDATE change
UPDATE s SET
to readSELECT
! You will then see that data as it would appear if the update had taken place.您可以使用来自...的更新,
例如:
更新装运集...
从
运输
内部加入 ProfilerTest.dbo.BookingDetails 上...
you can use update from...
something like:
update shipment set....
from
shipment
inner join ProfilerTest.dbo.BookingDetails on ...
我会这样写
这种方式连接是显式的,因为隐式连接是一件坏事恕我直言。您可以运行注释掉的选择(通常我会指定要更新彼此相邻的旧值和新值的字段),以确保我要更新的内容正是我想要更新的内容。
I would write it this way
This way the join is explicit as implicit joins are a bad thing IMHO. You can run the commented out select (usually I specify the fields I'm updating old and new values next to each other) to make sure that what I am going to update is exactly what I meant to update.
排序的序列(并且大概用户可以更改序列号来更改排序)。
我只需要解决一个类似的问题,我添加了一个序列号(以便按父 ID 分组的项目有一个可以 我而言,这对患者来说是保险,用户可以设置分配的顺序,因此仅使用主键对于长期而言没有用,但对于设置默认值很有用。
就 其他解决方案是在 SELECT 之外不允许某些聚合函数
此 SELECT 为您提供新的序列号:
此更新命令很简单,但不允许:
有效的解决方案(我刚刚做到了),并且与 eKek0 的解决方案类似:
这让我可以选择需要匹配的 ID,以及我需要为该 ID 设置的值,如果我没有使用 Row_Number() 的话,其他解决方案就可以了。 。
考虑到这是 1 次操作,它的编码仍然很简单,并且对于 4000 多行来说运行速度足够快
I just had to solve a similar problem where I added a Sequence number (so that items as grouped by a parent ID, have a Sequence that I can order by (and presumably the user can change the sequence number to change the ordering).
In my case, it's insurance for a Patient, and the user gets to set the order they are assigned, so just going by the primary key isn't useful for long-term, but is useful for setting a default.
The problem with all the other solutions is that certain aggregate functions aren't allowed outside of a SELECT
This SELECT gets you the new Sequence number:
This update command, would be simple, but isn't allowed:
The solution that worked (I just did it), and is similar to eKek0's solution:
this lets me select the ID I need to match things up to, and the value I need to set for that ID. Other solutions would have been fine IF I wasn't using Row_Number() which won't work outside of a SELECT.
Given that this is a 1 time operation, it's coding is still simple, and run-speed is fast enough for 4000+ rows