all groups > sql server programming > december 2003 >
You're in the

sql server programming

group:

How To make a REFERENCING to Old value or New Value in a Triggers



RE: How To make a REFERENCING to Old value or New Value in a Triggers Wolfgang
12/31/2003 4:06:08 AM
sql server programming: I'm new to SQLServer, what I understood is

The (only) UPDATE/INSERT/DELETE trigger in SQLServer is like the Oracle AFTER UPDATE/INSERT/DELETE statement trigger; row-based trigger not available. Within the trigger in SQLServer, the modified table is accessable in the following ways

SELECT * FROM myTable -- the values after the UPDATE/INSERT/DELETE comman
SELECT * FROM INSERTED -- the new values after an UPDATE/INSERT command,
SELECT * FROM DELETED -- the old values after an UPDATE/DELETE comman

Unlike Oracle, you can do anything with your table (myTable) within the trigger
For example you can 'undelete' items with SELECT * FROM DELETED INTO myTable

Hopefully it helps
--
Wolfgan

Re: How To make a REFERENCING to Old value or New Value in a Triggers David Portas
12/31/2003 11:43:04 AM
Within a trigger you can reference two virtual tables, "Inserted" and
"Deleted" which contain the before and after states of the changed rows.
Using these it should be possible to implement whatever logic you require.
In SQLServer a trigger fires once per UPDATE/INSERT/DELETE statement.

If I've correctly understood the code you posted it doesn't appear to do
anything more than assign the old and new values to two variables. The exact
TSQL solution obviously depends on what you then want to do with those
values.

--
David Portas
------------
Please reply only to the newsgroup
--

Re: How To make a REFERENCING to Old value or New Value in a Triggers David Portas
12/31/2003 12:17:27 PM
[quoted text, click to view]

AFTER triggers are the default type but in SQL2000 you can also specify an
INSTEAD OF trigger which means you can implement business logic before the
update happens if you need to.

[quoted text, click to view]

You could implement row-based processing within a trigger if you wanted to
but that should rarely be necessary. As is usually the case in SQL, it's
generally better to utilise set-based operations.

--
David Portas
------------
Please reply only to the newsgroup
--

How To make a REFERENCING to Old value or New Value in a Triggers JANE
12/31/2003 7:05:10 PM
Dear All:

I can reference old value and new value in a trigger using Oracle as
belews:

CREATE OR REPLACE TRIGGER trigger_1
AFTER INSERT OR UPDATE
ON table1
REFERENCING NEW AS n OLD AS o
FOR EACH ROW
WHEN (n.organization_id IN (82, 83))

DECLARE
v_neworgzid VARCHAR2 (30);
v_oldorgzid VARCHAR2 (30);

BEGIN

v_neworgzid := :n.organization_id
v_oldorgzid := :o.organization_id

END



HOW CAN I DO THIS IN MSSQL???


Thanks for advices

AddThis Social Bookmark Button