获取插入语句的最大序列ID
我目前正在使用两步过程创建插入
语句,我试图将其凝结成一个步骤。
现在,我有一个乏味但有效的过程:
首先,我运行此查询以获取下一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会考虑使用序列,但是如果您想按照自己的方式进行操作,则需要将完整子查询放入
select> select
:当您只有
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
: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 samemax
and conflict. Even if you know that isn't currently possible, it's a safer design to avoid even potential concurrency issues.