English 中文(简体)
从插入触发器中的另外一个表格中选择
原标题:Select from another table within an Insert Trigger

I am maintaining an audit table, where in I have a parent table and it s child table.I want to insert the primary key of the parent audit table into it s child audit table.
Should I be declaring a "before insert" instead of a "for insert" trigger. Here s my code:

CREATE trigger [trgAudtblChild] On [tblChild]
for Insert 
as
BEGIN

declare @serNo  bigint 
declare @expSerNo  int 
declare @postPercent  numeric  (12, 2) 
declare @prdSAPid  varchar  (50) 
declare @lastUpdatedBy  int 
declare @lastUpdatedOn  smalldatetime 

SELECT 
 --@serno = serno,
 @expSerNo = expSerNo ,
 @postPercent = postPercent ,
 @prdSAPid = prdSAPid ,
 @lastUpdatedBy = lastUpdatedBy ,
 @lastUpdatedOn = lastUpdatedOn
FROM INSERTED

select @serno = max(at_serno) from AT_tblParent

insert into AT_tblChild(serNo, expSerNo, postPercent
                       , prdSAPid, lastUpdatedBy, lastUpdatedOn
                       , change_column_index) values(
                                  @serNo, @expSerNo, @postPercent
                          , @prdSAPid, @lastUpdatedBy, @lastUpdatedOn
                          ,  INSERTED )

End
Return

上述代码不起作用,将表格放入交易中。

最佳回答

before trigger - 当您想要在数据被插入到表格之前截获数据时 。

- 您的记录已经插入,但仍可以修改。

唯一的区别是,关于记录,实际上是否插入。

Back to the original Query

在上述您所提到的情况下,您不应使用 < kbd> Before Trigger 。当您的父表格记录在一些交易中插入时,您不应使用 < kbd> Before Trigger 。当您的父表格记录在一些交易中插入时,您会考虑一个案例,而同一交易会被回放。在这种情况下,它会为外国密钥限制而崩溃。因为您会尝试在插入时将外国密钥的父母表格记录引用到父表格中不存在的子表格中 。

问题回答

暂无回答




相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签