SQL global variable example

If you have some application development experience in any language, then you probably familiar with global variable, global means we can access the value of that variable from any part of that application.

Here we learn how to declare global variable in SQL server, then how to access them.

sql global variable

SQL global variable always starts with double @@ sign

There are many system defined global variable, which we can access to see the values.

Global variable in SQL

To test some global variable values, simply access following variables in your sql query analyser.

  • @@SERVERNAME
  • @@LANGUAGE
  • @@ERROR
  • select @@VERSION
  • select @@CPU_BUSY - returns time in miliseconds.

Here is how you can use a global variable in your code.

Select * from [dbo].[tbClient]
if (@@ERROR<>0)
	print 'There is some error'
else
	print 'no error'

If anything goes wrong in sql server, the @@ERROR variable will be populated with error code.

 
Hire SQL Developer
sql global variable
SQL Training: For any group or corporate training, please contact us at webtrainingroom(at)gmail.
SQL job Interview Questions Answers
Course in Demand
SQL database development tutorials for learning sql query, data manipulation language, working with MS SQL Server.
MS SQL Examples | Join MS SQL Course