Solved

Using Global Variables in Transact-SQL

Posted on 2003-10-27
5
396 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

13 Experts available now in Live!

Get 1:1 Help Now