在 SQL 中用不同范围的数字替换一定范围的数字

发布于 2024-10-07 07:37:40 字数 514 浏览 6 评论 0原文

例如分别替换

TransactionID
--------
 1
 2
 3 

TransactionID
--------
 95
 96
 97

。基本上,我想用数字替换数字,但对于多个项目,无需为每个项目编写更新语句。

示例:

Update BatchItem
    set TransactionID = '95'
    where BatchItemID = 12345**6**


Update BatchItem
    set TransactionID = '96'
    where BatchItemID = 12345**7**


Update BatchItem
    set TransactionID = '97'
    where BatchItemID = 12345**8**

如何编写上面的语句来按顺序更新多个transactionID?

For example replace

TransactionID
--------
 1
 2
 3 

with

TransactionID
--------
 95
 96
 97

respectively. Basically, I want to replace numbers with numbers but for multiple items without writing update statement for each item.

Example:

Update BatchItem
    set TransactionID = '95'
    where BatchItemID = 12345**6**


Update BatchItem
    set TransactionID = '96'
    where BatchItemID = 12345**7**


Update BatchItem
    set TransactionID = '97'
    where BatchItemID = 12345**8**

How do I write above statement to update multiple transactionID in sequence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

時窥 2024-10-14 07:37:40

不能100%确定我理解你想要实现的目标。你想做这样的事情吗?

declare @offset int
set @offset = 94

update BatchItem
    set TransactionID = TransactionID + @offset
    where BatchItemID = 123456

Not 100% sure I understand what you're trying to achieve. Are you trying to do something like this?

declare @offset int
set @offset = 94

update BatchItem
    set TransactionID = TransactionID + @offset
    where BatchItemID = 123456
默嘫て 2024-10-14 07:37:40

试试这个:

UPDATE BatchItem SET TransactionId = TransactionId + 94

更新

这似乎是@Joe 答案的“快速而肮脏”的版本。如果您发现它没有他的答案那么令人困惑,我会保留它......

Try this:

UPDATE BatchItem SET TransactionId = TransactionId + 94

UPDATE

This appears to be the "quick and dirty" version of @Joe's answer. I'll leave it up in case you find it less confusing than his answer...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文