DECLARE Statements in SQL Server Transact-SQL

Q

How to declare a variable in SQL Server Transact-SQL? How to use the DECLARE statements?

✍: FYIcenter.com

A

In Transact-SQL, you must use DECLARE statements to declare variables.

Declaring a variable is to define the variable name and the variable data type.

In Transact-SQL, you can:

  • Declare a single variable in a single DECLARE statement.
  • Declare multiple variables in a single DECLARE statement using comma (,) as the delimiter.
  • Declare variables with initial values using the assignment operator =.

Here are some example on how to declare variables in Transact-SQL:

DECLARE @country CHAR(3);
DECLARE @name NVARCHAR(80), @city NVARCHAR(80);
DECLARE @price MONEY = 9.99;
DECLARE @now DATETIME = GETDATE();

 

SET Statements in SQL Server Transact-SQL

Binary String Data Types in SQL Server Transact-SQL

Variables and Data Types in SQL Server Transact-SQL

⇑⇑ SQL Server Transact-SQL Tutorials

2017-04-04, 1691🔥, 0💬