Solved

How to use global variables in SQL Server ?

Posted on 2002-07-18
6
314 Views
Last Modified: 2006-11-17
Hi All,
  How can I use global variables in SQL Server stored procedures ?

My aim is to make a value available throughout the duration of connection, so that  once the variable is initialised in a stored procedure,the variable can be accessed by any trigger associated with the tables used by the stored procedure .

Thanks,
Ras
0
Comment
Question by:rasaraja
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 25 total points
Comment Utility
This is not possible as such.
A workaround is to have a table where you store this value, with a keyindication of the @@spid value.

CREATE TABLE GlobalValues ( spid as int, value as varchar (100) )


in your stored procedure, you do this:

<...>
UPDATE GlobalValues SET value = 'somevalue' WHERE spid = @@spid
IF @@rowcount = 0
INSERT INTO GlobalValues (spid, value) VALUES (@@spid, 'somevalue')

<...>

in the other stored procedures or triggers, you can read the value:
DECLARE @val varchar(100)
SELECT @val = value from GlobalValues WHERE spid = @@spid
<...>

CHeers

0
 
LVL 2

Assisted Solution

by:niklausj
niklausj earned 20 total points
Comment Utility
Another Workaround is to define a global cursor, this cursortype is visible to and valid during a connection.
you can set the cursor ( ... for select VALUE ) to return a single value, or set it to a temp-table-variable which will go out of scope when the cursor closes and that is either the close/deallocate statement or the closing of the connection.
0
 

Author Comment

by:rasaraja
Comment Utility
Hi,
Both of you have given correct answers. How can I divide the point between you two ?
Thanks,
Ras
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Technically, you can either choose...  
* the first one
* the one you use
...or you ask for a point split (for 45 points?)
* post a 0-point question in the Community support area.
CHeers
0
 

Expert Comment

by:CleanupPing
Comment Utility
rasaraja:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Restore Procedure question 4 28
SQL 2012 and SQL 2014 in memory database 11 28
Powershell SMO script not working. 18 97
SQL JOIN 6 27
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now