Global variables in SQL Server start with @@. The scope of global variables is for the connection session.

Here are some that I've bothered to take notes for:

@@ERROR

If an error occurs while statements are executing, this variable will change from 0 to an error number. This is useful in transactions.

@@IDENTITY

This variable is used to get the ID last generated:

  • An INSERT, SELECT INTO, or bulk copy statement sets this variable to the last identity value generated by the statement.
  • If the statement didn't affect any identity columns (columns that automatically generate ID numbers), then this variable will contain NULL.

@@ROWCOUNT

This variable returns the number of rows affected by the last statement.

@@TRANCOUNT

This variable is used in transactions:

  • BEGIN TRANSACTION statement increments this variable by 1.
  • COMMIT TRANSACTION statement decrements this variable by 1.
  • ROLLBACK TRANSACTION statement decrements this variable to 0.
  • ROLLBACK TRANSACTION SavePoint statement has no affect on this variable.


GeorgeHernandez.comSome rights reserved