How to use global variables in SQL Server ?

Posted on 2002-07-18
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 .

Question by:rasaraja
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 25 total points
ID: 7161727
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



Assisted Solution

niklausj earned 20 total points
ID: 7164733
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.

Author Comment

ID: 7184245
Both of you have given correct answers. How can I divide the point between you two ?
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7184538
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.

Expert Comment

ID: 9280099
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 38
SQL Backup Question 2 29
SSRS: Why is Visual Studio stripping these properties? 2 21
SQL Recursion 6 18
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

856 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