Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use global variables in SQL Server ?

Posted on 2002-07-18
6
Medium Priority
?
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Accepted Solution

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

CHeers

0
 
LVL 2

Assisted Solution

by:niklausj
niklausj earned 80 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.
0
 

Author Comment

by:rasaraja
ID: 7184245
Hi,
Both of you have given correct answers. How can I divide the point between you two ?
Thanks,
Ras
0
 
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.
CHeers
0
 

Expert Comment

by:CleanupPing
ID: 9280099
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

662 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