在 Sql Server 中交换 ID
我看了一下这个问题: sql 交换主键值
所以我得到了以下内容script:
UPDATE t, t as t2
SET t.id = t2.id, t2.id = t.id
WHERE t.id = 1 AND t2.id = 2
但我无法将其转换为有效的 SQL Server 语法。
请帮助我:)
提前致谢!
I took a look at the question: sql swap primary key values
So I got the following script:
UPDATE t, t as t2
SET t.id = t2.id, t2.id = t.id
WHERE t.id = 1 AND t2.id = 2
But i can not translate this to a valid SQL Server syntax.
Please help me :)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样
What about
看起来您正在尝试将表与自身连接起来,以便可以一次更新两条记录。我不确定这是否可能,但肯定没有必要。
只需找到两条记录,然后从两个 id 之和中减去另一条记录即可计算出一条 id:(
注意:这要求键是数字。虽然这是最常见的情况,但某些非数字类型也可以用作键。)
It looks like you are trying to join the table against itself so that you can update two records at once. I'm not sure if it's possible, but it's certainly not needed.
Just locate the two records, and calculate one id from the other by subtracting it from the sum of the two ids:
(Note: This requires that the key is numeric. While this is the most common case, some non-numeric types can also be used as keys.)