Using Variables and Parameters

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Using Variables and Parameters

Transact-SQL has several ways to pass data between Transact-SQL statements. Among these are:

  • Transact-SQL local variables.

    A Transact-SQL variable is an object in Transact-SQL batches and scripts that can hold a data value. After the variable has been declared, or defined, one Transact-SQL statement in a batch can set the variable to a value and a later statement in the batch can get the value from the variable. For example:

    DECLARE @EmpIDVar INT
    
    SET @EmpIDVar = 1234
    
    SELECT *
    FROM Employees
    WHERE EmployeeID = @EmpIDVar
    
  • Transact-SQL parameters.

    A parameter is an object used to pass data between a stored procedure and the batch or script that executes the stored procedure. Parameters can be either input or output parameters. For example:

    CREATE PROCEDURE ParmSample @EmpIDParm INT AS
    SELECT *
    FROM Employees
    WHERE EmployeeID = @EmpIDParm
    GO
    
    EXEC ParmSample @EmpIDParm = 1234
    GO
    

Applications use application variables and parameter markers to work with the data from Transact-SQL statements.

  • Application variables

    The application programming languages such as C, C++, Basic, and Java have their own variables for holding data. Applications using the database APIs must move the data returned by Transact-SQL statements into application variables before they can work with the data. This is typically done using a process called binding. The application uses an API function to bind the result set column to a program variable. When a row is fetched the API provider or driver moves the data from the column to the bound program variable.

  • Parameter markers

    Parameter markers are supported by the ADO, OLE DB, and ODBC-based database APIs. A parameter marker is a question mark (?) placed in the location of an input expression in a Transact-SQL statement. The parameter marker is then bound to an application variable. This allows data from application variables to be used as input in Transact-SQL statements. Parameter markers also let stored procedure output parameters and return codes be bound to application variables. The output data is then returned to the bound variables when the procedure is executed. The DB-Library API also supports binding stored procedure parameter and return codes to program variables.

See Also

DECLARE @local_variable

SELECT

Functions

SET @local variable