Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Pass value from SQL to Trigger

Author  Topic 

jbreslow
Starting Member

16 Posts

Posted - 2011-03-02 : 13:37:23
Hello,

How can I pass a value that was used in a query's where clause into a Trigger?

If I run the following query in SQL Manger, how do I get the ID (590) that was passed to the query into the Trigger? I need this ID so I know which record the Trigger is going to update, right?

update user_address
set address1 = '1234 Some Street'
where id = 590

What I am trying to do with the Trigger is to copy any changes that were made in one table to another table.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 13:39:30
You can't grab the value from the DML query, but you really don't need it as the deleted/inserted trigger tables contain the data that you'll need to copy it to another table.

The deleted trigger table will contain the "before" image of the affected rows.
The inserted trigger table will contain the "after" image of the affected rows.

Read up on these trigger tables for more information.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-02 : 14:53:44
Hi tkizer,

Thank you for the quick reply. So, if I don't need to pass these values in, then how do I update the correct row in the second table? Do you have sample code you can share?

Also, I am trying to trigger this on UPDATE not DELETE of INSERT.

I have been reading up on triggers and I am still lost, which is why I posted to these forums.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 14:59:15
Here's an example for your UPDATE trigger code:

CREATE ...

AS


UPDATE t2
SET ColumnZ = i.ColumnZ, ColumnR = i.ColumnR, ...
FROM inserted i
JOIN Table2 t2
ON i.ColumnPK = t2.ColumnPK

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-02 : 15:05:51
Hello,

I added that code to my Trigger, ran it, and it seemed to work. Although, when it ran it said that all 3000 records in my table were updated (but only one record actually changed). Running through each record seems like a lot of over-head when only one record was updated. Is there a way to only update the record that was changed when the Trigger was executed?

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 15:09:48
The code I posted only updates those rows that match the inserted trigger table rows. Whatever is in the inserted trigger table is what was updated. If 3000 rows were updated by the trigger, then you've either got a problem with your source update query or you've got multiple rows matching in Table2.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-02 : 15:19:18
Table 1 and Table 2 are exact copies of each other (don't ask). So yes, every row in Table 1 matches a row in Table 2 but that doesn't mean I want to update each and every record in the table. Just the record in Table 1 that was updated is all that needs to be updated in Table 2.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 15:22:35
And that's what my code does, it updates the data in Table2 that was updated in Table1. If you are seeing something else happen, then perhaps your JOIN clause wasn't setup correctly. It needs to reference the PK columns.

Show us your trigger code and some sample data if you need additional help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-02 : 15:55:52
Here is my code for the Trigger...

ALTER TRIGGER [dbo].[trigg_user_address] ON [dbo].[USER_ADDRESS]
AFTER UPDATE AS
DECLARE @ua int;
DECLARE @uam int;
-- count columns in user_address table
SELECT @ua = (
select count(col.name) as num_columns
from sysobjects OBJ, syscolumns COL
where obj.id = col.id
and obj.name = 'USER_ADDRESS')

-- count columns in user_address_multi table
select @uam = (
select count(col.name) as num_columns_multi
from sysobjects OBJ, syscolumns COL
where obj.id = col.id
and obj.name = 'USER_ADDRESS_MULTI')

-- make sure the columns from the two tables match
IF @ua = @uam
-- if they match, then update the second table
BEGIN
UPDATE user_address_multi
SET uam.address1 = ua.address1
FROM user_address ua
JOIN user_address_multi uam
ON ua.user_addr_id = uam.user_addr_id
END
ELSE
BEGIN
-- if they don't match, raise error and rollback update
RAISERROR ('Columns do not match, change was not committed', 11,1)
ROLLBACK
END




The two tables have the exact same columns...
user_addr_id (PKID)
user_fkid (FKID)
address1
address2
address3
city
state
zip
address_type_id
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 16:00:03
Your update statement is incorrect. Please see the code that I posted which includes the inserted trigger table. You must use the inserted trigger table for this to work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-03 : 10:29:46
Sorry about that. I read that line as just an example table name. I changed it back and all worked fine, one record updated.

Do you happen to have a good resource for learning about Triggers and things like 'inserted'?

UPDATE t2
SET address1 = i.address1
FROM inserted i
JOIN user_address_multi t2
ON i.user_addr_id = t2.user_addr_id


Thank you so much for your help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-03 : 11:22:03
I don't have a good resource for you except Books Online. The trigger tables are covered in there.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-09 : 13:39:32
Do you happen to have an example of a Delete query for a trigger? I am trying the following and I am getting an error...


delete from user_address_multi
where user_address_multi.user_addr_id = deleted.user_addr_id


Error:
The multi-part identifier "deleted.user_addr_id" could not be bound.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-09 : 13:46:31
You can't reference the deleted table in your query unless you join to it.

delete u
from user_address_multi u
join deleted d
on u.user_addr_id = d.user_addr_id

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jbreslow
Starting Member

16 Posts

Posted - 2011-03-09 : 14:01:21
Once again, you are my savior!!!!

Thank you
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-09 : 14:38:15


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -