Wiki Home

Transact - SQL Variables

Namespace: SQL
Transact - SQL supports variables, and these are local in scope, meaning they maintain their value for the lifetime of the batch or query in which they are declared. Transact - SQL variables are designated with a @ symbol.

Once a variable is declared, it can be assigned a value with either a SELECT or SET statement.

/*Example using SET to assign a variable:*/
DECLARE @GivenName varchar(25)
set @GivenName= 'Ratface'

/*Example using SELECT to assign a variable:*/
DECLARE @GivenName varchar(25)
SELECT @GivenName= NickName from Thugs WHERE ThugId="23434"

Note that if the SELECT returns more than one row, the variable gets the value from the last row.
Category SQL Language
( Topic last updated: 2000.06.13 10:11:12 PM )