插入触发器问题
我正在使用一个简单的触发器 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何知道 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 orCreatedDateTime
column with default constraint, this will be a better indicator of the insertion order.