Obtaining the value of certain columns in a SQL database

Tags:
columns
SQL Server
SQL Server databases
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?
ASKED: November 10, 2008  7:27 PM
UPDATED: November 10, 2008  10:52 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following