Declaring and Working with Variables

Just as you can create variables within the subroutines and functions that you build, you can declare variables in your stored procedures. You use the keyword DECLARE to create a variable. The syntax looks like this:

DECLARE @VariableName DataType [(length)], @VariableName DataType [(length)]

Here's an example:

DECLARE @FirstName VarChar(35)

Uninitialized variables are assigned the value Null. You use a SELECT statement to assign a value to a variable. It looks like this:

SELECT @FirstName = 'Alexis'

The following is a stored procedure that illustrates the use of a variable:

 DECLARE @strCompany varchar (50) SELECT @strCompany = Upper(CompanyName) FROM Customers WHERE CustomerID = 'ALFKI' SELECT @strCompany ...

Get Alison Balter's Mastering Access 2002 Enterprise Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.