Click here to Skip to main content
15,902,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to create a trigger after update , I have 2 tables Buytable and Selltable both have same fields (id,transId,symbol,qty,date,transtype), I want to update the buytable transtype every time i update only the selltable transtype which is (true, false) for only the same transid on both table

i have tried the below cod but its not working, pls help

Alter trigger [dbo].[transtypr_Update] on [dbo].[SellTable]

What I have tried:

Create trigger [dbo].[transtypr_Update] on [dbo].[SellTable] 
 
AFTER UPDATE

AS


IF UPDATE(TransType)

begin


declare @TransType as Bit
declare @Buy_TransID as Int
declare @Sell_TransID as Int
declare @Type as Bit



select @TransType= TransType from BuyTable
select @Sell_TransID= TransID from SellTable
select @Buy_TransID= TransID from BuyTable
select @Type= TransType from SellTable



UPDATE [dbo].[BuyTable]

set [TransType]  = @Type 
where @Buy_TransID=@Sell_TransID
end
Posted
Updated 17-Jan-20 20:15pm
Comments
OriginalGriff 18-Jan-20 1:57am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.

1 solution

To fetch the updated data you need to use Inserted table. So the trigger would be something like
SQL
CREATE TRIGGER [dbo].[transtypr_Update] on [dbo].[SellTable] 
AFTER UPDATE AS
BEGIN
   IF UPDATE(TransType) BEGIN
      UPDATE BuyTable
      SET    BuyTable.TransType = inserted.TransType
      FROM   BuyTable
      INNER JOIN Inserted ON BuyTable.TransID = Inserted.TransID
   END
END
 
Share this answer
 
Comments
qulaitks 18-Jan-20 3:52am    
Thanks it works .
Wendelius 18-Jan-20 4:47am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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