How do you DECLARE a temp variable in SQL?
What is a Variable in SQL Server?In MS SQL, variables are the object which acts as a placeholder to a memory location. Variable hold single data value. Show
In this tutorial you will learn:
Variable Types in SQL: Local, GlobalMS SQL has two types of variables:
However, the user can only create a local variable. Below figure explain two types of variable available in MS SQL server. Local variable:
Global variable:
How to DECLARE a variable in SQL
TSQL Syntax: DECLARE { @LOCAL_VARIABLE[AS] data_type [ = value ] }Rules:
Examples of Declaring a variable:Query: With ‘AS’ DECLARE @COURSE_ID AS INT;Query: Without ‘AS’ DECLARE @COURSE_NAME VARCHAR (10);Query: DECLARE two variables DECLARE @COURSE_ID AS INT, @COURSE_NAME VARCHAR (10);Assigning a value to SQL VariableYou can assign a value to a variable in the following three ways:
Let’s have a look at all three ways in detail: During variable declaration using DECLARE keywordT-SQL Syntax: DECLARE { @Local_Variable [AS] Datatype [ = value ] }Here, after datatype we can use ‘=’ followed by value to be assigned Query: DECLARE @COURSE_ID AS INT = 5 PRINT @COURSE_IDUsing SQL SET VARIABLESometimes we want to keep declaration and initialization separate. SET can be used to assign values to the variable, post declaring a variable.Below are the different ways to assign values using SET: Example: Assigning a value to a variable using SET Syntax: DECLARE @Local_VariableQuery: DECLARE @COURSE_ID AS INT SET @COURSE_ID = 5 PRINT @COURSE_IDExample: Assign a value to multiple variables using SET. Syntax: DECLARE @Local_Variable _1Rule: One SET Keyword can be used to assign a value to only one variable. Query: DECLARE @COURSE_ID as INT, @COURSE_NAME AS VARCHAR(5) SET @COURSE_ID = 5 SET @COURSE_NAME = 'UNIX' PRINT @COURSE_ID PRINT @COURSE_NAMEExample: Assigning a value to a variable with a Scalar Subquery using SET Syntax: DECLARE @Local_Variable_1Rules:
Assumption: Assume that we have the table as ‘Guru99’ with two columns as displayed below: We will use ‘Guru99’ table in the further tutorials Example 1: When subquery return one row as a result. DECLARE @COURSE_NAME VARCHAR (10) SET @COURSE_NAME = (select Tutorial_name from Guru99 where Tutorial_ID = 3) PRINT @COURSE_NAMEExample 2: When subquery returns zero row as a result DECLARE @COURSE_NAME VARCHAR (10) SET @COURSE_NAME = (select Tutorial_name from Guru99 where Tutorial_ID = 5) PRINT @COURSE_NAMEIn this particular case, the variable value is EMPTY, i.e., NULL. Using SQL SELECT VARIABLEJust like SET, we can also use SELECT to assign values to the variables, post declaring a variable using DECLARE. Below are different ways to assign a value using SELECT: Example: Assigning a value to a variable using SELECT Syntax: DECLARE @LOCAL_VARIABLEQuery: DECLARE @COURSE_ID INT SELECT @COURSE_ID = 5 PRINT @COURSE_IDExample: Assigning a value to multiple variable using SELECT Syntax: DECLARE @Local_Variable _1Rules: Unlike SET, SELECT can be used to assign a value to multiple variables separated by the comma. DECLARE @COURSE_ID as INT, @COURSE_NAME AS VARCHAR(5) SELECT @COURSE_ID = 5, @COURSE_NAME = 'UNIX' PRINT @COURSE_ID PRINT @COURSE_NAMEExample: Assigning the value to a variable with a Subquery using SELECT Syntax: DECLARE @Local_Variable_1Rules:
Example 1: When subquery return one row as a result. DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = (select Tutorial_name from Guru99 where Tutorial_ID = 1) PRINT @COURSE_NAMEExample 2: When subquery return zero row as a result DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = (select Tutorial_name from Guru99 where Tutorial_ID = 5) PRINT @COURSE_NAMEIn this particular case, the variable is to EMPTY, i.e., NULL. Example 3: Assign a value to a variable with a regular SELECT statement. Syntax: DECLARE @Local_Variable _1Rules:
Query 1: The query returns one row. DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from Guru99 where Tutorial_ID = 3 PRINT @COURSE_NAMEQuery 2: The query returns multiple rows. DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from Guru99 PRINT @COURSE_NAMEIn this special case, variable value is set to the value of the last row. Query 3: The query returns zero rows. DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from Guru99 where Tutorial_ID = 5 PRINT @COURSE_NAMEIn this particular case, the variable is EMPTY, i.e., NULL. Other SQL Variable ExamplesUsing variable in the query Query: DECLARE @COURSE_ID Int = 1 SELECT * from Guru99 where Tutorial_id = @COURSE_IDInteresting Facts About SQL Server Variables!
Summary:
How do you DECLARE a temp?The Syntax to create a Temporary Table is given below:. To Create Temporary Table: CREATE TABLE #EmpDetails (id INT, name VARCHAR(25)). To Insert Values Into Temporary Table: INSERT INTO #EmpDetails VALUES (01, 'Lalit'), (02, 'Atharva'). To Select Values from Temporary Table: SELECT * FROM #EmpDetails.. Result: id. name. Lalit.. How do I DECLARE a variable in a SQL table?To declare a table variable, you use the DECLARE statement as follows:. DECLARE @table_variable_name TABLE ( column_list ); ... . DECLARE @product_table TABLE ( product_name VARCHAR(MAX) NOT NULL, brand_id INT NOT NULL, list_price DEC(11,2) NOT NULL );. What is temp table and temp variable in SQL?Difference Between Temp Table and Table Variable. How do I create a temp table in SQL query?To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as DROP TABLE , INSERT , UPDATE , or SELECT .
|