Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > sqlserver server > february 2006 >

sqlserver server : Passing variable to trigger


Rick
2/28/2006 4:37:26 PM
I have add/edit/delete triggers on a SQL Server 2005 table.

Before updating that table, I'd like to be able to set a variable that
the trigger can read and that only has a lifetime of that connection or
transaction.

I cannot include the variable in the update stored procedure.

I tried creating a #temptable to hold the value, but it doesn't seem to
exist from one stored procedure to the next.

Any suggestions.

Thanks,
Rick
Rick
2/28/2006 6:02:45 PM
My triggers create a row in another table.
I have a column in this other table that gives information
about the source of the update. This is not known until the
time of the update. Therefore, I envision a two step process,

1) first I set a variable inside a transaction or connection
2) My triggers read the variable and set the column value in the
new row they are adding.

What I am asking is what value can I set, that will be limited in scope
to the transaction or connection, that can be read by the triggers.

Hope this clarifies things.
Rick
David Gugick
2/28/2006 8:06:28 PM
[quoted text, click to view]

Please explain in more detail what you are trying to accomplish.
Triggers work off the inserted/deleted tables to access the changed
data. You don't pass variabels to them since they are called
automatically by SQL Server. You can probably do what you want from the
stored procedure, but let's wait to hear what it is you're actually
trying to do.

--
David Gugick - SQL Server MVP
Quest Software
David Browne
2/28/2006 9:56:02 PM

[quoted text, click to view]

Not that this is a terribly good idea, but here's how to do it:

If you create a #temptable in a stored procedure it will be destroyed at the
end of the procedure call.

If you create the #temptable in a SQL batch, it will live for the lifetime
of the connection.

EG

create table t(id int primary key, data char(50))
go
create trigger t_insert on t for insert
as
begin
if object_id('tempdb..#session') is not null
begin
declare @msg varchar(50)
select @msg = msg from #session
print @msg
end
end
go

--on login issue this batch
create table #session
(
onerow int primary key check (onerow = 0) default 0,
msg varchar(50) --add whatever session variables you want
)
insert into #session default values
go


--later
update #session set msg = 'hello'
go


--later still
insert into t(id, data)
values (1, 'whatever')
go


David

jrpm
3/1/2006 10:08:33 AM
There is a lot of information available in environmental variables that are
automatically available. The info may already be available. What data are
you trying to pass?

--
Joseph R.P. Maloney, CSP,CCP,CDP


[quoted text, click to view]
Rick
3/1/2006 1:06:39 PM
Thanks David, but the triggers exist and I can't include them in a
batch.

I have created a workaround with a physical table, a global table
variable and locks inside the updating transaction.
It isn't pretty, but it works.
Rick
Rick
3/1/2006 1:11:02 PM
If you know of an environment variable that I can set to a string
value,
that has a lifetime of the transaction, that is visible globally to the
triggers
firing inside a current transaction, let me know.

Thanks,
Rick
jrpm
3/2/2006 7:29:03 AM
If each transaction is a distinct session, then you could use @@SPID
environmental variable to identify the entry in sysprocesses that can give
you the times, such as

select logoin_time,last_batch,getdate() from sysprocesses where spid=@@spid


--
Joseph R.P. Maloney, CSP,CCP,CDP


[quoted text, click to view]
jrpm
3/2/2006 7:56:29 AM
He wanted to get the lifetime of a transaction.

With given that each transaction is a distinct connection or session, then
he can get the elapsed time (lifetime) from sysprocesses for his session
(spid) from the logon-time field and/or the last_batch (command) field and
the current datetime. I admit the calculation could be in the select, but I
am not sure about all his transaction details, ie, whether it is a pooled
connection or not. If pooled then logon-time won't work and last_batch might
not.

This is all oriented around his trigger. With more information about his
requirements, it is possible that he may be better off using SQL Profiler
rather than a trigger, with transaction event monitoring. But this may not
be the right venue to go to that detail level.


--
Joseph R.P. Maloney, CSP,CCP,CDP


[quoted text, click to view]
David Browne
3/2/2006 8:35:31 AM

[quoted text, click to view]

Huh? What does that mean?

David

AddThis Social Bookmark Button