Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

How are parameters passed in Triggers?

Question posted by: nitindel (Member) on June 26th, 2008 04:57 AM
Hi All,
May i know how do we pass parameters in Triggers and if no parameters are passed then how they get fired...??

Thanks & Regards
Nitin Sharma
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
debasisdas's Avatar
debasisdas
Moderator
6,448 Posts
June 27th, 2008
12:58 PM
#2

Re: How are parameters passed in Triggers?
trigger do not require parameters. They execute automatically on the related database events.

Reply
ck9663's Avatar
ck9663
Expert
1,250 Posts
June 29th, 2008
05:16 AM
#3

Re: How are parameters passed in Triggers?
Trigger does not dictate whether a trigger will fire or not. Triggers are based on events that happen to your table, not the parameters inside it.

-- CK

Reply
coolminded's Avatar
coolminded
Member
106 Posts
June 30th, 2008
06:28 AM
#4

Re: How are parameters passed in Triggers?
Quote:
Originally Posted by ck9663
Trigger does not dictate whether a trigger will fire or not. Triggers are based on events that happen to your table, not the parameters inside it.

-- CK

hi,
what if i need to send some extra field to be inserted when a trigger gets fired.
i have a table and when a trigger is fired, the content of that table is inserted in the next table with the same table structure but with one more field 'modified_by'. how can i insert the field 'modified_by' when that trigger is fired. modified_by can be any name through which the application is logged in.
thanx

Reply
debasisdas's Avatar
debasisdas
Moderator
6,448 Posts
July 2nd, 2008
09:34 AM
#5

Re: How are parameters passed in Triggers?
yes you can do that .

but what is the problem ?

Reply
coolminded's Avatar
coolminded
Member
106 Posts
July 2nd, 2008
11:10 AM
#6

Re: How are parameters passed in Triggers?
Quote:
Originally Posted by debasisdas
yes you can do that .

but what is the problem ?


Hi Debasisdas,

let me clear my confusion,
i have 2 tables tbl_1, tbl_2.
tbl_1 contains id,name and address. and tbl_2 contains old_id, old_name, old_address, new_id, new_name, new_address and modified_by.
i wrote one trigger which inserts the old values as well as the new values in tbl_2 when there is any updation or deletion in tbl_1.

now my problem is how can i insert 'modified_by' in tbl_2, which is the 'user name' in any other table through which the application is logged in. i want to insert the 'username' as the 'modified_by' in tbl_2. how can i pass the username in the trigger so that when ever it gets fired, it also insert the 'username' as 'modified_by' in tbl_2.

now i think it is clear.
with regards,

Reply
ck9663's Avatar
ck9663
Expert
1,250 Posts
July 4th, 2008
08:58 AM
#7

Re: How are parameters passed in Triggers?
During insert to tbl2, identify the field names on the t-sql. Harcode the modified_by part. Something like,

Code: ( text )
  1. INSERT INTO (field1, field2, field3, modified_by) tbl2
  2. select field1, field2, field3, user_name() from inserted (or deleted whichever case).


Happy coding...

-- CK

Reply
coolminded's Avatar
coolminded
Member
106 Posts
July 6th, 2008
06:57 AM
#8

Re: How are parameters passed in Triggers?
Quote:
Originally Posted by ck9663
During insert to tbl2, identify the field names on the t-sql. Harcode the modified_by part. Something like,

Code: ( text )
  1. INSERT INTO (field1, field2, field3, modified_by) tbl2
  2. select field1, field2, field3, user_name() from inserted (or deleted whichever case).


Happy coding...

-- CK


i think you don't understand what i am trying to say.

i want to pass the parameter at the run time, and it's the name of the person who has logged in. how can i know who has logged in by using the select statement? and the username comes from different table, not the same table
thanx

Reply
ck9663's Avatar
ck9663
Expert
1,250 Posts
July 8th, 2008
02:12 PM
#9

Re: How are parameters passed in Triggers?
You're saying there's another table?

You got tbl_1 which is your transaction table. Anything happened there, you inserted the record in tbl_2. Where do you get the modified_by field?

-- CK

Reply
coolminded's Avatar
coolminded
Member
106 Posts
July 13th, 2008
06:31 AM
#10

Re: How are parameters passed in Triggers?
Quote:
Originally Posted by ck9663
You're saying there's another table?

You got tbl_1 which is your transaction table. Anything happened there, you inserted the record in tbl_2. Where do you get the modified_by field?

-- CK


another table tbl_3 contains modified_by field.

Reply
ck9663's Avatar
ck9663
Expert
1,250 Posts
July 13th, 2008
07:27 AM
#11

Re: How are parameters passed in Triggers?
You might just need to use JOIN


-- CK

Reply
coolminded's Avatar
coolminded
Member
106 Posts
July 14th, 2008
12:17 PM
#12

Re: How are parameters passed in Triggers?
Quote:
Originally Posted by ck9663
You might just need to use JOIN


-- CK



how to join the tables in the trigger. i want to know that
i wrote a trigger
Code: ( text )
  1. on update or delete on tbl_1
  2.                  call trigger trigger_name


how can i join tbl_1 and tbl_2 in the trigger
thanx

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,080 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Microsoft SQL Server Contributors