Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get Current Identity(Procedure)

2 views
Skip to first unread message

Gard T?nnesland

unread,
May 10, 2004, 9:46:41 AM5/10/04
to
Hi everyone,
How can I retrieve the current identity used in a procedure from a
trigger.

What I am trying to do is:

The procedure takes a @username (nvarchar) as a parameter, and the
procedure identifies the @username's identity and tries to delete the
identity's row. An Instead Of trigger fires, and updates the specific
row.

But how can the trigger identify the targeted row?
Which built-in function can I use?

Thanks

gardmt

Simon Hayes

unread,
May 10, 2004, 1:41:10 PM5/10/04
to

"Gard T?nnesland" <gardm...@hotmail.com> wrote in message
news:3d6ad642.04051...@posting.google.com...

You can use @@IDENTITY - see Books Online. Assuming you have MSSQL 2000,
there are also SCOPE_IDENTITY(), and IDENT_CURRENT(), but the functionality
is slightly different in each case. To get the new value while in the stored
procedure you would use SCOPE_IDENTITY() instead.

Simon


Erland Sommarskog

unread,
May 10, 2004, 6:01:39 PM5/10/04
to
[posted and mailed, please reply in news]

In a trigger, both INSTEAD OF and AFTER triggers, you have access to
the virtual tables "inserted" and "deleted". They hold the rows that
were inserted and deleted. (So in an INSERT trigger you have only
data in "inserted" and in a DELETE trigger only in "deleted", and
in UPDATE trigger, you have data in both.)

Note that since a trigger fires once per statment, you should write
the trigger so that it handles this situation. For instance:

CREATE TRIGGER Gard_tri ON tbl INSTEAD OF DELETE AS
UPDATE tbl
SET active = 0
FROM tbl t
JOIN deleted d ON t.keycol = d.keycol


--
Erland Sommarskog, SQL Server MVP, som...@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

Gard mt

unread,
May 11, 2004, 4:07:42 AM5/11/04
to
Erland Sommarskog <som...@algonet.se> wrote in message news:<Xns94E63B5...@127.0.0.1>...

> [posted and mailed, please reply in news]
>
> Gard T?nnesland (gardm...@hotmail.com) writes:
> > How can I retrieve the current identity used in a procedure from a
> > trigger.
> >
> > What I am trying to do is:
> >
> > The procedure takes a @username (nvarchar) as a parameter, and the
> > procedure identifies the @username's identity and tries to delete the
> > identity's row. An Instead Of trigger fires, and updates the specific
> > row.
> >
> > But how can the trigger identify the targeted row?
> > Which built-in function can I use?
>
> In a trigger, both INSTEAD OF and AFTER triggers, you have access to
> the virtual tables "inserted" and "deleted". They hold the rows that
> were inserted and deleted. (So in an INSERT trigger you have only
> data in "inserted" and in a DELETE trigger only in "deleted", and
> in UPDATE trigger, you have data in both.)
>
> Note that since a trigger fires once per statment, you should write
> the trigger so that it handles this situation. For instance:
>
> CREATE TRIGGER Gard_tri ON tbl INSTEAD OF DELETE AS
> UPDATE tbl
> SET active = 0
> FROM tbl t
> JOIN deleted d ON t.keycol = d.keycol

Thanks for your great reply. Takker og bukker (sjefen blir fornøyd).

Gard Tønnesland

0 new messages