插入触发器问题

发布于 2024-11-10 09:16:44 字数 1390 浏览 0 评论 0原文

我正在使用一个简单的触发器 ins_process 来捕获表 process 上的插入/更新。此 ins_process 将数据插入到表 process_audit 中。

process 最初按顺序插入了以下数据:

id    name    description
---   ----    -----------
1     proc1   sql
2     proc2   scripting
3     proc3   java

这将在 process_audit 中创建数据,如下所示:

id    name    description    insert_dt
---   ----    -----------    ---------
3     proc3   java           [a minute ago]
2     proc2   scripting      [a minute ago]
1     proc1   sql            [a minute ago]

为什么记录以相反的顺序插入?。当我从前端编辑一行时,process_audit中的数据如下:

id    name    description    insert_dt
---   ----    -----------    ---------
3     proc3   java           [a minute ago]
2     proc2   scripting      [a minute ago]
1     proc1   sql            [a minute ago]
2     proc2   scripting      [now]

这对我来说并不是什么大问题。我想知道这是否是标准行为,或者我创建触发器的方式是否有问题。

以下是我创建触发器的方法:

create trigger [dbo].[ins_process]  
on [dbo].[process] for insert, update    
as
set nocount on    
begin    
insert into process_audit
    (id, name, description, insertdate)    
select 
    id, name, description,current_timestamp
from inserted ins    
end  

此外,表 process_audit 中的 id 列不是 Identity 列。

I am using a simple trigger ins_process for capturing inserts/updates on a table process. This ins_process inserts data into a table process_audit.

process has the following data originally inserted, in that order:

id    name    description
---   ----    -----------
1     proc1   sql
2     proc2   scripting
3     proc3   java

This creates the data in process_audit as follows:

id    name    description    insert_dt
---   ----    -----------    ---------
3     proc3   java           [a minute ago]
2     proc2   scripting      [a minute ago]
1     proc1   sql            [a minute ago]

Why do the records get inserted in the reverse order?. When I edit a row from the front-end, the data in process_audit is as follows:

id    name    description    insert_dt
---   ----    -----------    ---------
3     proc3   java           [a minute ago]
2     proc2   scripting      [a minute ago]
1     proc1   sql            [a minute ago]
2     proc2   scripting      [now]

This is not so much of a problem for me. I am wondering if this is a standard behaviour or if there is something wrong in the way I have created the trigger.

Here is how I created the trigger:

create trigger [dbo].[ins_process]  
on [dbo].[process] for insert, update    
as
set nocount on    
begin    
insert into process_audit
    (id, name, description, insertdate)    
select 
    id, name, description,current_timestamp
from inserted ins    
end  

Also, the id column in table process_audit is not an Identity column.

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

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

发布评论

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

评论(1

初懵 2024-11-17 09:16:44

您如何知道 SQL 以什么顺序向表中插入行?

我假设您查询 select * from process_audit 并期望看到与插入顺序相同的行。这不是一个有效的期望。

如果未指定order by,SQL Server 不保证任何顺序。
此外,SQL Server 不维护行插入的顺序。

返回的结果可能会按聚集索引顺序排序,但仍不能保证。

如果您添加自己的身份 Id 列或具有默认约束的 CreatedDateTime 列,这将更好地指示插入顺序。

How do you know in which order SQL inserted rows to the table?

I assume you query select * from process_audit and expect to see rows in the same order as they were inserted. This is not a valid expectation.

SQL server does not guarantee any order if order by is not specified.
Also, SQL server does not maintain the order of rows insertion.

Likely the returned results will be order by clustered index order, but still not guaranteed.

If you add own identity Id column or CreatedDateTime column with default constraint, this will be a better indicator of the insertion order.

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