获取插入语句的最大序列ID

发布于 2025-02-06 06:36:58 字数 1183 浏览 1 评论 0原文

我目前正在使用两步过程创建插入语句,我试图将其凝结成一个步骤。

现在,我有一个乏味但有效的过程:

首先,我运行此查询以获取下一个seriesID

SELECT MAX(serialNo) + 1 FROM Table1

假设它返回54。然后,下一步,我将54手动输入54插入/选择:

INSERT INTO Table1
    SELECT 54, NULL, 1, 'NEW', GETDATE(), Table2.TrackingID
    FROM Table2 
    LEFT JOIN Table3 ON Table3.pid = Table2.pid
    LEFT JOIN Table1 ON Table1.CustID = Table1.CustID
    WHERE Table3.packageNumber = '123456'

这有效,但是必须进行第一个查询,然后每次手动更改序列号,这是非常烦人的。因此,我试图将查询合并到insert中,但是我没有运气:

INSERT INTO Table1
    SELECT (MAX(serialNo) + 1), NULL, 1, 'NEW', GETDATE(), Table2.TrackingID
    FROM Table2 
    LEFT JOIN Table3 ON Table3.pid = Table2.pid
    LEFT JOIN Table1 ON Table1.CustID = Table1.CustID
    GROUP BY Table2.TrackingID, Table3.packageNumber
    HAVING Table3.packageNumber = '123456'

从技术上讲,它可以正常施加错误,而是没有丢失错误,而是用54插入第一列希望它以无效插入。

预期结果:

   54|NULL|1|NEW|2022-06-10|98765

实际结果:

 NULL|NULL|1|NEW|2022-06-10|98765

如何从Table1获取最大序列号,以便我可以在下一个条目中使用max(serialno) + 1对序列号?

I am currently creating an INSERT statement using a two-step process, which I am trying to condense into one step.

Right now I have a process that is tedious but works:

First, I run this query to get the next SeriesID:

SELECT MAX(serialNo) + 1 FROM Table1

Let's say it returns 54. Then next, I manually type the 54 into the INSERT/SELECT:

INSERT INTO Table1
    SELECT 54, NULL, 1, 'NEW', GETDATE(), Table2.TrackingID
    FROM Table2 
    LEFT JOIN Table3 ON Table3.pid = Table2.pid
    LEFT JOIN Table1 ON Table1.CustID = Table1.CustID
    WHERE Table3.packageNumber = '123456'

This works, but it is incredibly annoying to have to do the first query and then change the serial number manually every time. So I am trying to incorporate the query into the INSERT, but I am having no luck:

INSERT INTO Table1
    SELECT (MAX(serialNo) + 1), NULL, 1, 'NEW', GETDATE(), Table2.TrackingID
    FROM Table2 
    LEFT JOIN Table3 ON Table3.pid = Table2.pid
    LEFT JOIN Table1 ON Table1.CustID = Table1.CustID
    GROUP BY Table2.TrackingID, Table3.packageNumber
    HAVING Table3.packageNumber = '123456'

And this technically works as in doesn't throw an error, but instead of inserting the first column with 54 as I was hoping, it inserts it with a NULL.

Expected result:

   54|NULL|1|NEW|2022-06-10|98765

Actual result:

 NULL|NULL|1|NEW|2022-06-10|98765

How do I get the MAX serial number from Table1, so that I can use MAX(serialNO) + 1 for the serial number in the next entry?

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

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

发布评论

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

评论(1

无言温柔 2025-02-13 06:36:58

我会考虑使用序列,但是如果您想按照自己的方式进行操作,则需要将完整子查询放入select> select

INSERT INTO Table1
SELECT (select MAX(serialNo) +1 from Table1),NULL,1,'NEW',GETDATE(),Table2.TrackingID
  from Table2 
  left join Table3 on Table3.pid = Table2
  left join Table1 on Table1.CustID = Table1.CustID
GROUP BY Table2.TrackingID, Table3.packageNumber
HAVING Table3.packageNumber = '123456'

当您只有max(serialno)在执行连接和条件后,它获得了最大值,这与整个表中的最大值不同。

我建议序列是为了避免潜在的并发问题。如果同时运行多个插入物,他们可能都会返回相同的max和冲突。即使您知道目前是不可能的,它也是一个更安全的设计,以避免潜在的并发问题。

I would consider using a sequence for this, but if you want to do it the way you're doing it, you need to put the full subquery in your SELECT:

INSERT INTO Table1
SELECT (select MAX(serialNo) +1 from Table1),NULL,1,'NEW',GETDATE(),Table2.TrackingID
  from Table2 
  left join Table3 on Table3.pid = Table2
  left join Table1 on Table1.CustID = Table1.CustID
GROUP BY Table2.TrackingID, Table3.packageNumber
HAVING Table3.packageNumber = '123456'

When you only have MAX(serialNo) it is getting the maximum after doing your joins and conditions, which is different than the maximum in the entire table.

The reason I suggest a sequence is to avoid potential concurrency issues. If multiple inserts are run at the same time, they might all return the same max and conflict. Even if you know that isn't currently possible, it's a safer design to avoid even potential concurrency issues.

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