Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
538 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
The first thing to know about global variables in SQL is that they aren't variables (for these, read my separate tutorial). Instead, they are useful bits of infomation made available by SQL Server, which you can use.
All global variables start with @@; all normal variables start with @.
The most useful global variables are the following:
Global variable | What it holds / notes |
---|---|
@@IDENTITY | Holds the id number of the last record added. I've covered the use of this in a separate blog (which explains that SCOPE_IDENTITY() is often a better alternative). |
@@ROWCOUNT | Holds the number of rows affected by an SQL command - covered in the next part of this blog. |
@@ERROR | The last error number generated by SQL (or 0 if there isn't one). I'll be covering this in a separate blog on error-handling and error messages, and will link to this when it's complete. |
@@SERVERNAME | The name of the current SQL Server. |
@@TRANCOUNT | The number of currently open transactions (covered in a later separate blog). |
You can see a full list of all of the global variables in SQL here.
Here's an example of the use of the main global variables in SQL:
-- insert a film
INSERT INTO tblFilm (FilmName) VALUES ('E.T.')
SELECT
@@SERVERNAME AS 'Server name',
@@ROWCOUNT AS 'Row count',
@@IDENTITY AS 'Id field value',
@@ERROR AS 'Error message',
@@TRANCOUNT AS 'Open transactions'
And here's what this could show:
5 global variable values for this query.
Now let's look in more detail at one particular global variable: @@ROWCOUNT.
If you've just selected, inserted, deleted or updated rows in SQL, @@rowcount will tell you how many. However, you have to access it immediately!
Here are a couple of examples: one which will work, and one which won't. Let's start with the one which works. The following SQL takes all films winning 10 or more Oscars, and puts them in a new table:
-- put all of the films winning 10
-- or more Oscars into a new table
SELECT
FilmName,
FilmReleaseDate
INTO
DoubleDigitOscars
FROM
tblFilm
WHERE
FilmOscarWins >= 10
-- show how many rows were inserted
SELECT 'Created ' +
CAST(@@rowcount AS varchar(3)) +
' rows in new table'
Here's the output I got when I just ran this query:
The query put two rows in the new table.
This works because I interrogated the row count immediately. If you add in another statement, however, things go awry:
-- put all of the films winning 10
-- or more Oscars into a new table
SELECT
FilmName,
FilmReleaseDate
INTO
DoubleDigitOscars
FROM
tblFilm
WHERE
FilmOscarWins >= 10
-- any command here resets row count
PRINT 'Output follows'
-- show how many rows were inserted
SELECT 'Created ' +
CAST(@@rowcount AS varchar(3)) +
' rows in new table'
This would show:
This time, the query would show that no rows had been added.
The reason is that @@rowcount holds the number of rows affected by the last executed statement - which in the above example is 0, because the PRINT command doesn't affect rows in a table!
An easy way round this is to store the number of rows affected, then use this subsequently:
-- will hold number of rows added
DECLARE @NumRowsAdded int
-- put all of the films winning 10
-- or more Oscars into a new table
SELECT
FilmName,
FilmReleaseDate
INTO
DoubleDigitOscars
FROM
tblFilm
WHERE
FilmOscarWins >= 10
-- store number rows inserted
SET @NumRowsAdded = @@ROWCOUNT
-- any command here resets row count
PRINT 'Output follows'
-- show how many rows were inserted
SELECT 'Created ' +
CAST(@NumRowsAdded AS varchar(3)) +
' rows in new table'
Here the use of the @NumRowsAdded variable gets round the problem.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.