Click here to Skip to main content
15,893,486 members

SQL Trigger Help when 2 update take place in one table

Member-515487 asked:

Open original thread
Let say
i have created 1 store procedure
for GRN entry

grn no
grn no  srno
1       1  
1       2

and so on 


i am pasting some code of GRN update store procedure
SQL
UPDATE [tblGRNEntry]
   SET 
[GRN_Date] =@GRN_Date,
PO_No=@PO_No,
[supl_id] = @supl_id ,
      [item_code] = @item_code
      ,[receipt_qty] = @receipt_qty
      ,[passed_qty] = @passed_qty
      ,[UOM] = @UOM
      ,[batch_no] = @batch_no
      ,[expiry_date] = @expiry_date
     
      ,[modusername] = @addusername
      ,[moddate] = @adddate
 WHERE [GRN_No] = @GRN_No 
 
 and [sr_no] = @sr_no
 if @@ROWCOUNT >0
 begin
 UPDATE [TestelERP].[dbo].[tblGRNEntry]
   SET 
[GRN_Date] =@GRN_Date , supl_id=@supl_id --because of having large no of Srno i may need to change GRN_Date of all grn_no =1
 WHERE [GRN_No] = @GRN_No 


now i am maintaining audit trail

for this

SQL
create TRIGGER trgGRNUpdate
ON dbo.tblGRNEntry AFTER UPDATE
AS 
   INSERT INTO dbo.tblAuditTrail(Doc_No,Doc_Date,Sr_No,item_code,batch_no, oldvalue, newvalue,type,modify_by,modify_date,menuname)
      SELECT i.GRN_No,i.GRN_Date,i.Sr_No,i.item_code,i.batch_no
     
      ,'GRN_Date:'+CAST(d.GRN_Date AS VARCHAR(10)) +';PO_No: '+cast(d.PO_No as varchar(9))+';Supl_Id:'+d.supl_id+';Item_Code:'+cast(d.item_code as varchar(6))+';Receipt_Qty:'+cast(d.receipt_qty as varchar(20))+';Passed_Qty:'+cast(d.passed_qty as varchar(20))+';UOM:'+d.UOM+';Batch_No:'+d.batch_no+';Expiry_Date:'+cast(d.expiry_date as varchar(30))
      ,'GRN_Date:'+CAST(i.GRN_Date AS VARCHAR(10)) +';PO_No: '+cast(i.PO_No as varchar(9))+';Supl_Id:'+i.supl_id+';Item_Code:'+cast(i.item_code as varchar(6))+';Receipt_Qty:'+cast(i.receipt_qty as varchar(20))+';Passed_Qty:'+cast(i.passed_qty as varchar(20))+';UOM:'+i.UOM+';Batch_No:'+i.batch_no+';Expiry_Date:'+cast(i.expiry_date as varchar(30))
      ,'Updated',i.modusername,i.moddate,'GRN Data Entry'
      FROM Inserted i
      INNER JOIN Deleted d
       ON i.GRN_No = d.GRN_No and i.sr_no=d.sr_no
-- and i.GRN_Date=d.GRN_Date and i.PO_No=d.PO_No and i.supl_id=d.supl_id 
-- i have tried this also 


when update runs out i should get 1 record but i am getting 3 record
including all diff Sr_no of same Grn_no

how to sort out this
2 update take place in one table but i want only 1st one carried out trigger run but not for next update
Tags: SQL

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900