DECLARE Statement

Defines the name and type of local variables for a batch or procedure and defines cursors.

Syntax

DECLARE @variable_name datatype
    [, @variable_name datatype...]

where

@variable_name
Is the name of the variable. Variable names must conform to the rules for identifiers, except variable names use the "at" symbol (@) to signify a variable name.
datatype
Is a system or user-defined datatype. For information on available system datatypes, see the Datatypes topic. For information on creating user-defined datatypes, see the sp_addtype system stored procedure.

Example

This example is a stored procedure (reptq3) from the pubs database. This stored procedure uses three variables declared as input parameters.

CREATE PROCEDURE reptq3 @lolimit money, @hilimit money,
@type char(12)
AS
SELECT pub_id, type, title_id, price
    FROM titles
        WHERE price > @lolimit 
        AND price < @hilimit 
        AND type = @type OR type LIKE '%cook%'
            ORDER BY pub_id, type
            COMPUTE count(title_id) BY pub_id, type

See Also

Batches PRINT
Control-of-Flow Language RAISERROR
CREATE PROCEDURE SELECT
Cursors UPDATE
Datatypes Variables
EXECUTE