• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Using Global Variables in Transact-SQL

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
balintawak
Asked:
balintawak
1 Solution
 
lauszCommented:
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
 
DexstarCommented:
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
 
nmcdermaidCommented:
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
 
nmcdermaidCommented:
clarification... Transact SQL global variables have nothing to do with DTS global variables.
0
 
balintawakAuthor Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now