What are Global variables in SQL Server and how to create them?

In SQL Server, there is NO concept of Global variables. In the initial versions of SQL Server, the System defined functions which begin with ‘@@’ are referred as Global variables. But their actual behavior is not same as variables in SQL Server. We can’t create them.

SQL Server supports only local variables which can be created by using DECLARE keyword. Most of the people are still in a misconception that the names in SQL Server which start with ‘@@’ are known as Global variables. But, they are not actual Global variables. They are SQL server System defined functions. As a database folk, we always think that all the System/User defined functions contains parenthesis ‘()’ at the end of its name. This is 100% correct for User defined functions. But this is not going to happen every time in the context of System defined functions.

Most of the System defined functions are always postfix with parenthesis. But few functions like @@Error, @@ROWCOUNT etc… always prefix with @@ and does not postfix with parenthesis. The alternative solution for this is creating a Global temporary table which returns a single record and can be accessed by everyone who is working on the same server.

Find more info on BOL

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.