Obtaining the value of certain columns in a SQL database
I have created the following trigger that inserts data into a SQL table when a specific column is updated. I would also like to obtain the current value of certain columns of the updated row. To do this, I've been using the INSERT command, but I keep receiving NULL values in return. This is the code: CREATE TABLE [Main] ( [USerial] varchar(10) NOT NULL, [Status] varchar(10) NULL) ON [PRIMARY]; CREATE TRIGGER [Status_Notification] ON [Main] WITH EXECUTE AS CALLER AFTER UPDATE AS if update(status) SET NOCOUNT ON declare @xml_value xml declare @P_number varchar(10) declare @status varchar(10) -- Select data and build XML set @pnr_number = (select d.userial from main m inner join deleted d on m.userial = d.userial) set @xml_value = '<MAIN><Number>333</Number><Status>Cancel</Status></MAIN>' -- Insert an entry into the table insert into q_out (bi,bo,xml,mid,pnr_nr, msg_status) values ('a', 'b', @xml_value, 1, @pnr_number, 'N'); GO Do you have any idea why this is happening?

Software/Hardware used:
ASKED: November 10, 2008  7:27 PM
UPDATED: November 10, 2008  10:52 PM

Answer Wiki:
The first thing that I notice is that you have an the IF UPDATE(STATUS) at the top, but with no BEGIN END statements. This means that only the line directly under the IF command is what happens if the IF is valid. In this case that is the SET NOCOUNT ON. You would be better off writing the trigger like this so that the trigger can handle more than one record being processed at a time. <pre>CREATE TRIGGER [Status_Notification] ON [Main] WITH EXECUTE AS CALLER AFTER UPDATE AS if update(status) BEGIN SET NOCOUNT ON declare @xml_value xml -- Select data and build XML set @xml_value = '<MAIN><Number>333</Number><Status>Cancel</Status></MAIN>' -- Insert an entry into the table insert into q_out (bi,bo,xml,mid,pnr_nr, msg_status) SELECT 'a', 'b', @xml_value, 1, userial, 'N' FROM deleted END GO</pre>
Last Wiki Answer Submitted:  November 10, 2008  10:52 pm  by  Denny Cherry   64,550 pts.
All Answer Wiki Contributors:  Denny Cherry   64,550 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _