How to Set Variable from a SQL Query

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:

  1. SET is the ANSI standard for variable assignment, SELECT is not.
  2. SET can only assign one variable at a time, SELECT can make multiple assignments at once.
  3. 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)
  4. 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)
  5. 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



Leave a reply



Submit