Solved

Using Global Variables in Transact-SQL

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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