Groups | Blog | Home
all groups > sql server (microsoft) > march 2006 >

sql server (microsoft) : variables local to a transaction



HSA
3/24/2006 12:52:47 PM
Does SQL server support transcation-scoped variables? I need to save a
particular value into a variable (say inside a stored procedure) and
then access that variable inside triggers that are run in the context
of the same transaction. I need the scope of variable to be limitted
to a transaction, because the value I save in that variable is
different in each transaction. I'll be grateful if somebidy can help.

Thanks,
HSA
Jens
3/25/2006 12:31:30 AM
See the below example to see how to share data between these
procedures:


DROP TABLE SomeTable

GO

CREATE TABLE SomeTable
(
SomeColumnInt INT
)

GO

CREATE TRIGGER TRN_SomeTable_Ins ON SomeTable
FOR INSERT
AS
BEGIN
SELECT 'From within the trigger -->' + SomeColumn
FROM #Temptest
END

GO

DROP PROCEDURE SomeProc

GO

CREATE PROCEDURE SomeProc
AS
BEGIN
CREATE TABLE #Temptest (SomeColumn VARCHAR(200))
INSERT INTO #Temptest VALUES ('Testdata Accessible')

INSERT INTO SomeTable VALUES(1)
END


EXEC SomeProc

HTH; Jens Suessmeyer.

---
http://www.sqlerver2005.de
---
HSA
3/26/2006 7:51:29 AM
Thanks for your reply. But are these temp tables private to the
transaction? If I have 2 transactions running concurrently, and each of
them calls the procedure and create the temp table, and then do
something that runs the trogger, will their temp tables be separate an
isolated? Since I'm cretaing my transaction in an app server with
connection pooling, I have no control over the connection or batch. I
can only control transactions, so this methos can help only if temp
tables are private to the transaction within which they are crated.

Thanks,
HSA

[quoted text, click to view]
AddThis Social Bookmark Button