在TSQL中生成唯一id并转换唯一id
我试图通过创建两个新列值来更新 SQL 表:一个唯一 ID,然后是刚刚创建的相同唯一 ID 的缩短整数版本。
使用我在此处找到的技巧(请参阅“NEWID () Way' 朝向底部),这就是我认为可行的方法:
Update Customer Set [UniqueId] = NEWID(), [UniqueIntegerId] = ABS(CAST(CAST([UniqueId] AS VARBINARY) AS INT))
但这会生成类似
[UniqueId] [UniqueIntegerId]
3C79...5A4DEB2 的 内容754988032
1FD6...828B943 754988032
1F48...E80F511 754988032 <---重复!不想!
尝试完成此操作的正确语法是什么?
I am trying to update a SQL table by creating two new column values : a unique Id, and then a shortened integer version of the same unique id just created.
Using a trick I found here (see 'The NEWID() Way' towards bottom), this is what i thought would work :
Update Customer Set [UniqueId] = NEWID(), [UniqueIntegerId] = ABS(CAST(CAST([UniqueId] AS VARBINARY) AS INT))
but this generates something like
[UniqueId] [UniqueIntegerId]
3C79...5A4DEB2 754988032
1FD6...828B943 754988032
1F48...E80F511 754988032 <--- repeating! do not want!
What syntax is correct for trying to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不会按预期工作,因为表达式中的
[UniqueId]
会在更新之前计算为值。您可以尝试这样做:为@uid
变量分配与[UniqueId]
相同的值,然后使用它来代替[UniqueId]
在另一列的表达式中。That won't work as expected, because
[UniqueId]
in the expression is evaluated to a value before the update. This is what you could try instead:The
@uid
variable is assigned the same value as[UniqueId]
and is then used instead of[UniqueId]
in the expression for the other column.