?
Solved

Using Global Variables in Transact-SQL

Posted on 2003-10-27
5
Medium Priority
?
401 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
[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
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 100 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

770 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