Solved

Using Global Variables in Transact-SQL

Posted on 2003-10-27
5
394 Views
Last Modified: 2012-08-13
I have a VB background, but new to Transact-SQL, and am trying to decipher someone else's Transact-SQL code.

There are global variable declarations in their code, but I don't why it was used. Can someone give some examples or an explanation of why I would need to use global variables in Transact-SQL?
0
Comment
Question by:balintawak
5 Comments
 
LVL 6

Expert Comment

by:lausz
ID: 9627738
Global variables are used in DTS, not in Transact-Sql .

From BOL

DTS Global Variables in Visual Basic
Global variables that do not exist when first referenced during Data Transformation Services (DTS) package execution are created at that time. Prior to package execution, you can create global variables explicitly by adding a GlobalVariable object to the package.

Here are the basic steps for creating a global variable in a DTS package prior to package execution:

Use the New method of the GlobalVariables collection of the Package2 object.


Set the Value property of the created GlobalVariable object to the initial value of the global variable.


Add the object to the package with the Add method of the GlobalVariables collection.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9627745
balintawak:

> There are global variable declarations in their code, but I don't why it was
> used. Can someone give some examples or an explanation of why I would need to
> use global variables in Transact-SQL?

Why don't you post the T-SQL that you are trying to decipher, and let us help you with it?  There are lots of reasons that people use global variables, although they are usually used unnecessarily.

Hope that helps,
Dex*
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 50 total points
ID: 9627812
Global variables are used in Transact-SQL

They use the identifier @@ rather than @

However you can't declare global variables, but you can declare global temp tables!

You can declare local variables with a single @ symbol,

ie

DECLARE @iCount INT

you can reference global variables if you need to:

SET @iCount = @@ROWCOUNT
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9627826
clarification... Transact SQL global variables have nothing to do with DTS global variables.
0
 

Author Comment

by:balintawak
ID: 9627860
I think I found my answer in the book "Microsoft SQL Server 2000 Unleashed" by the publisher SAMS.

It states in p732:
 "The name global was apparently confusing to some, implying that the scope of the variable was beyond that of a local variable. Global variables were apparently mistaken by some as variable that a user could declare and the scope of the variable would extend across batches, which is not the case. You can name a variable starting with two or more at signs (@@), but it will still behave as a local variable"

It seems that the original author of the T-SQL code I am deciphering used 2 @@ signs just to confuse me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

12 Experts available now in Live!

Get 1:1 Help Now