Unix Technical Forum

Help for Trigger

This is a discussion on Help for Trigger within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello I would like to get the content of a field based in the field Name. Suppose a table ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:35 AM
Thierry Marneffe
 
Posts: n/a
Default Help for Trigger


Hello

I would like to get the content of a field based in the field Name.
Suppose a table with a field Named 'LastName' for wich there is a trigger
after update
I store the field name in a local variable

Set @ColName = 'LastName'

How can I retrieve the value of the @ColName from the inserted table using
the @Colname variable ?

I tried this:

Set @Cmd = 'DECLARE @DataValue varchar(100) Set @DataValue = (Select i.' +
@ColName + ' from inserted i) print @DataValue'
exec (@Cmd)

print @Cmd gives

DECLARE @DataValue varchar(100) Set @DataValue = (Select i.LastName from
inserted i) print @DataValue

But I got the following error

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'inserted'.

Any idea why ?

Thanks for your help

Thierry



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:36 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Help for Trigger

On Fri, 23 Apr 2004 20:57:10 +0200, Thierry Marneffe wrote:

>
>Hello
>
>I would like to get the content of a field based in the field Name.
>Suppose a table with a field Named 'LastName' for wich there is a trigger
>after update
>I store the field name in a local variable
>
>Set @ColName = 'LastName'
>
>How can I retrieve the value of the @ColName from the inserted table using
>the @Colname variable ?
>
>I tried this:
>
>Set @Cmd = 'DECLARE @DataValue varchar(100) Set @DataValue = (Select i.' +
>@ColName + ' from inserted i) print @DataValue'
>exec (@Cmd)
>
>print @Cmd gives
>
>DECLARE @DataValue varchar(100) Set @DataValue = (Select i.LastName from
>inserted i) print @DataValue
>
>But I got the following error
>
>Server: Msg 208, Level 16, State 1, Line 1
>Invalid object name 'inserted'.
>
>Any idea why ?
>
>Thanks for your help
>
>Thierry


Hi Thierry,

The inserted and deleted pseudo-tables can only be used in the
trigger. Invoking dynamic SQL creates a new environment, so you can't
use the inserted and deleted tables there.

If you don't use dynamic SQL, all will be swell:

CREATE TRIGGER TestIt
ON MyTable AFTER UPDATE
AS
DECLARE @DataValue varchar(100)
SET @DataValue =
(SELECT LastName
FROM inserted)
PRINT @DataValue
go

Of course, this trigger will still result in an error if you perform
an update that affects more than one row - always remember that a
trigger is fired exactly once for each update statement, regardless of
the number of rows that match the search criteria (can be anything
from 0 up to the complete table) and regardless of whether the data
was actually chaged or not (ie UPDATE MyTable SET MyColumn = MyColumn
will fire the trigger and will have the complete table in the inserted
ande deleted pseudo-tables).


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com