Share via

Need Trigger for Updating .

Analyst_SQL 3,576 Reputation points
2021-06-27T17:15:14.82+00:00

i have data in table

Create table #tbl_GRN_Detail (D_ID int,GRN_ID int,item_code int,Issue_ID int)
Insert into #tbl_GRN_Detail values (11001,2001,22,Null)

Now i want ,when i insert row into #tbl_issuance ,then Issue_ID value get updated into #tbl_GRN_Detail col Issue_ID

Create table #tbl_issuance  (issue_ID int,item_code int,Issue_Date int,D_ID int)


Insert into #tbl_issuance values (101,22,'2021-06-28',11001)
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
2021-06-27T22:03:32.08+00:00
Create table tbl_GRN_Detail (D_ID int,GRN_ID int,item_code int,Issue_ID int)
     Insert into tbl_GRN_Detail values (11001,2001,22,Null)
Create table tbl_issuance  (issue_ID int,item_code int,Issue_Date date,D_ID int)
go
CREATE TRIGGER issuance_tri ON tbl_issuance AFTER INSERT AS
UPDATE tbl_GRN_Detail
SET    Issue_ID = i.issue_ID
FROM   tbl_GRN_Detail G
JOIN   inserted i ON G.D_ID = i.D_ID
go
Insert into tbl_issuance values (101,22,'2021-06-28',11001)
go
SELECT * FROM tbl_GRN_Detail
go
DROP TABLE tbl_GRN_Detail
DROP TABLE tbl_issuance

In a trigger, you have access to the virtual tables inserted and deleted. inserted holds the inserted rows by an INSERT statement and an afterimage of the rows for an UPDATE statement.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.