Link to home
Start Free TrialLog in
Avatar of cooking
cooking

asked on

MSSQL how to get value from table into variable ?

hi .. let's say i have a table and a variable.

SELECT email from parent where username = @pusername;  

will return the value that i need to have in @mail variable..

how can i set the @mail variable to get the value of the above sql query ? i also know that it will return only one result .
thanks in advance
Avatar of RiteshShah
RiteshShah
Flag of India image

declare @mail varchar(50)

select @mail=email from parent where username=@parentname

select @mail
you are sure about that your query will return only one value so there is no problem at all, since there is no concept like array in SQL Server so if your query will return more than one row in above query, only last email address will be stored in @mail variable.
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
generally in the situation Author has, I would go for SELECT not for SET, nothing much different but just wanted to stick with ANSI standard, basically SET is used to assign static value and SELECT is for getting value in variable from QUERY. have a look.

http://www.sqlhub.com/2009/03/set-and-select-in-sql-server-2005.html