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)
 Trigger help

Author  Topic 

tech_1
Posting Yak Master

129 Posts

Posted - 2010-10-26 : 00:45:22
I am trying to create a trigger for SQL 2005.
I only want the trigger to execute when I know a certain source has "issued" the trigger.

so there will be a SQL scheduled job running. This will be executing a SPROC.

The SPROC will be updating a table based on certain criterias.

now, only when this SPROC will be executed, will I want the trigger to execute.

I want the trigger to be able to insert a record into another table but also taking some parameters (FK parameters) from the table which is being updated.

is this possible? How?

Kristen
Test

22859 Posts

Posted - 2010-10-26 : 02:24:00
You could add a column to the Table as a "Flag" that the Trigger should do some "extra" processing (including clearing the flag column)

Or perhaps the User making the change will isolate it to a specific process? I have forgotten the system function, but its something like SUSER_NAME() - it will be in the DOCs
Go to Top of Page

tech_1
Posting Yak Master

129 Posts

Posted - 2010-10-26 : 04:24:08
:-)

adding the column would unfortunately require alot of work for the client side applications (don't ask!). so ideally I'd like to use everything that SQL has to offer with minimum impact on the client side.

I heard, but could be wrong, that you are able to query in the trigger on "who" or "what" the function/trigger has been called from. is this true? if so - I need to then obtain this value.

but going to the original problem - how is it possible to insert values into another table when the trigger is been triggered, obtaining the table that is being updated, to use some of its values (which are not being updated)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-10-26 : 04:58:35
quote:
Originally posted by tech_1
I heard, but could be wrong, that you are able to query in the trigger on "who" or "what" the function/trigger has been called from. is this true? if so - I need to then obtain this value.


Yeah, SUSER_NAME() - did you look it up?

"but going to the original problem - how is it possible to insert values into another table when the trigger is been triggered, obtaining the table that is being updated, to use some of its values (which are not being updated)"

In a trigger you have access to the record(s) being changed (before and after versions of the row(s)) and you can join those to any other tables you need to.
Go to Top of Page
   

- Advertisement -