how to assign query result to local variable in T-SQL
You need to declare a table variable for that:
DECLARE @localVariable AS TABLE
(
userId int,
userName varchar(10),
email nvarchar(100),
address nvarchar(1000)
);
INSERT INTO @localVariable (userId, userName, email, address)
SELECT userId, userName, email, address
FROM [user];
Set Variable for Select Result in SQL Server
Your syntax is wrong for the SET
It should be
DECLARE @bDate DATE;
SET @bDate = (SELECT birth_date FROM person WHERE id='1');
SELECT @bDate;
OR, you can use SELECT
DECLARE @bDate DATE;
SELECT @bDate = birth_date FROM person WHERE id='1';
SELECT @bDate;
SQL Set variable to select result
You can try something like
(declare variables first...)
SELECT TOP 1 @var1=col1, @var2=col2, @var3=col3, [...] FROM YourTable WHERE YourFilter
EDIT: All together this seems not to be the best approach... With SQL you should not think in values and single rows but rather in result sets (set based programming). Your thinking leads to many tiny selects, while loops, cursors and all this stuff one should avoid.
SET versus SELECT when assigning variables?
Quote, which summarizes from this article:
- SET is the ANSI standard for variable assignment, SELECT is not.
- SET can only assign one variable at a time, SELECT can make multiple assignments at once.
- If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)
- When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from its previous value)
- As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.
How to set variable from select statement in SQL Server
You can do :
select @selected_name = name, @selected_age = age, @selected_salary = salary
from people
where id = @id;
Make sue this will need to have a single/unique entry, if that is not the case then you need top
clause :
select top (1) @selected_name = name, @selected_age = age, @selected_salary = salary
from people
where id = @id;
SQL set variable as the result of a query in a stored procedure
Another a bit safer option would be to use sp_executesql stored procedure something like this....
DECLARE @FieldName nvarchar(255);
DECLARE @RequestCode Nvarchar(50);
DECLARE @ReqSQLQuantity nvarchar(max);
DECLARE @Quantity int, @tableName SYSNAME;
SET @tableName = N'report_' + @RequestCode
Select @ReqSQLQuantity = N' select @Quantity = count(*) '
+ N' From (select distinct ' + QUOTENAME(@FieldName)
+ N' from ' + QUOTENAME(@tableName) + N' ) as number'
Exec sp_executesql @ReqSQLQuantity
,N'@Quantity INT OUTPUT'
,@Quantity OUTPUT
Select @Quantity
Related Topics
Why Does This SQL Code Give Error 1066 (Not Unique Table/Alias: 'User')
Protecting Against SQL Injection in Python
How to Handle an in Sub-Query with Linq to SQL
Selecting Data into a Postgres Array
Generate a Range of Dates Using SQL
Remove Duplicates Using Only a MySQL Query
SQL - How to Select a Row Having a Column with Max Value
Selecting Random Rows in MySQL
Why Can't I Use Alias in a Count(*) "Column" and Reference It in a Having Clause
How to Get the Last Row of an Oracle Table
Does Sparksql Support Subquery
Calculate a Sum of Type Time Using SQL
Could Not Find Stored Procedure 'Dbo.Aspnet_Checkschemaversion'
Compare Datetime and Date Ignoring Time Portion
How to Avoid Duplicate Values for Insert in SQL