如何使用触发器将两个表组合在另一个表中。 [英] How to two table combine in another table using trigger.

查看:222
本文介绍了如何使用触发器将两个表组合在另一个表中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨....

在我的数据库两个表中

1)InvoiceDetails 2)AppliedTax

in invoiceDetails三个属性Invoice_ID,InvoiceDetails_ID ,TaxName和In AppliedTax三个属性AppliedID,AppliedTaxName,Grand Total。

这两个表属性在单个表(InvoiceMD)中结合两个触发器的使用方式。



当单表可行时但问题是插入数据时所有数据再次重复。



  ALTER  触发器 [dbo]。[trg_InvoiceMD] 
开启 [dbo]。[InvoiceDetails]
Insert
As
开始
声明 @ InvoiceDetails_ID int ;
声明 @ Invoice_ID int ;
声明 @ TaxName nvarchar 100 );

插入 进入 InvoiceMD 选择 InvoiceDetails_ID,Invoice_ID,TaxName
from InvoiceDetails
- < span class =code-comment>其中Invoicedetails_ID = @ InvoiceDetails_ID
end

解决方案

尝试以下触发器。它只会插入新添加的记录。如果你想组合多个表,那么使用连接相应地修改触发器 -





  ALTER  触发器 [dbo]。[trg_InvoiceMD] 
On [dbo]。[InvoiceDetails]
插入
As
开始
声明 @ InvoiceDetails_ID int ;
声明 @ Invoice_ID int ;
声明 @ TaxName nvarchar 100 );

插入 进入 InvoiceMD 选择 InvoiceDetails_ID,Invoice_ID,TaxName
来自 INSERTED
- < span class =code-comment>其中Invoicedetails_ID = @ InvoiceDetails_ID


end


Hi....
In my databse two table
1)InvoiceDetails 2)AppliedTax
in invoiceDetails Three attribute Invoice_ID,InvoiceDetails_ID,TaxName and In AppliedTax Three Attribute AppliedID,AppliedTaxName,Grand Total.
these two table Attribute In single table(InvoiceMD) combine how two trigger use.

when single table is possible But probl is when data inserted all data are repeated again again.

ALTER trigger [dbo].[trg_InvoiceMD]
On [dbo].[InvoiceDetails]
After Insert
As
Begin
declare @InvoiceDetails_ID int;
declare @Invoice_ID int;
declare @TaxName nvarchar(100);

insert into InvoiceMD Select InvoiceDetails_ID,Invoice_ID,TaxName
from InvoiceDetails 
--where Invoicedetails_ID=@InvoiceDetails_ID
end

解决方案

Try the below trigger. It will insert only newly added records. If you want to combine multiple tables then modify the trigger accordingly using joins -


ALTER trigger [dbo].[trg_InvoiceMD]
On [dbo].[InvoiceDetails]
After Insert
As
Begin
declare @InvoiceDetails_ID int;
declare @Invoice_ID int;
declare @TaxName nvarchar(100);

insert into InvoiceMD Select InvoiceDetails_ID,Invoice_ID,TaxName
from INSERTED
--where Invoicedetails_ID=@InvoiceDetails_ID


end


这篇关于如何使用触发器将两个表组合在另一个表中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆