Michael Spiegel (mspiegel@sccs.swarthmore.edu) writes:[color=blue]
> Yes, my apologies, I was referring to a trigger. And you are correct
> in that it's possible to always statically determine what table is in
> my context, based on the trigger. But I wish to write a series of
> triggers that all perform nearly identical tasks, except they act
> w.r.t. the table which called them. Like so:
>
>
> CREATE TRIGGER trg_Table1 ON Table1 FOR INSERT
> AS
> INSERT INTO foo (invocation) VALUES (this)
> GO
>
> CREATE TRIGGER trg_Table2 ON Table2 FOR INSERT
> AS
> INSERT INTO foo (invocation) VALUES (this)
> GO[/color]
A trigger is always bound to table, so, yes, you always know in the table
of which table you are.
One way to go, would be to generate the triggers with some tool, that
would take the table name as parameter.
But if you want to dig out the table name from the trigger, this is
actually possible:
CREATE TABLE nisse (a int NOT NULL)
go
CREATE TRIGGER nisse_tri ON nisse FOR insert AS
SELECT parent_table = object_name(parent_obj)
FROM sysobjects
WHERE id = @@procid
go
INSERT nisse VALUES (21)
go
DROP TABLE nisse
--
Erland Sommarskog, SQL Server MVP,
esquel@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp