Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jackal
Starting Member
2 Posts |
Posted - 2014-02-24 : 05:22:39
|
hi I was wondering how to perform calculations of a particular field value in a table using After Insert Trigger in sql server 2008.I was able to create After Insert Trigger for my tables PurchaseOrder_master and PurchaseOrder_trans in sql server 2008.Given below is structure of my tablesTable1: name: PurchaseOrder_masterColumn Name DataTypeagn Int(IDENTITY Primary Key not null)PO_NO varchar(50)supplier_id bigintPO_Date dateTAX bigintrow_upd_date datetimeuser_id Inttechnician_id bigintstatus bitTable2: name: PurchaseOrder_transColumn Name DataTypepo_id bigintitem_id bigintPO_NO varchar(50)qty Intprice floatPO_Date datestatus bituser_id introw_upd_date datetimeGiven Below is my After Insert Trigger:CREATE TRIGGER trgAfterInsert4 ON [dbo].[PurchaseOrder_master] FOR INSERTASdeclare @ag int;declare @pno varchar(50);declare @supplier bigint;declare @pdate date;declare @tax int;declare @row_date datetime;declare @user int;declare @technician int;declare @pid int;declare @item int;declare @stat bit;select @ag=i.agn from inserted i;select @pno=i.PO_NO from inserted i;select @supplier=i.supplier_id from inserted i;select @pdate=i.PO_Date from inserted i;select @tax=i.TAX from inserted i;select @row_date=i.row_upd_date from inserted i;select @user=i.[user_id] from inserted i;select @technician=i.technician_id from inserted i;insert into PurchaseOrder_trans(PO_NO,PO_Date,po_id,item_id,status,[user_id],row_upd_date)values(@pno,@pdate,@pid,@item,@stat,@user,getdate()); Now my trigger executes well but my problem is i dont get item_id,price and qty values at all when i execute the trigger.Should i need to include price,qty and item_id in my PurchaseOrder_master_table? So that the values i expect will come? Second my question is suppose i want to perform calculations in qty and price field values how should i do it using After Insert Trigger in sql server 2008? Can anybody help me please with problem. Any help or guidance in solving this problem would be greatly appreciated.vishal77 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-02-24 : 13:29:08
|
A trigger executes the local batch one one step, not by a row-by-row basis.CREATE TRIGGER dbo.trgAfterInsert4ON dbo.PurchaseOrder_masterAFTER INSERTASSET NOCOUNT ON;INSERT dbo.PurchaseOrder_Trans ( PO_NO, PO_Date, po_id, item_id, [status], [user_id], row_upd_date )SELECT PO_NO, PO_Date, po_id, item_ID, [status], [user_id], row_upd_dateFROM inserted; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|