MSSQL how to get value from table into variable ?

Posted on 2009-05-11
Last Modified: 2012-08-13
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
Question by:cooking
  • 4
LVL 31

Expert Comment

ID: 24352999
declare @mail varchar(50)

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

select @mail
LVL 31

Expert Comment

ID: 24353020
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.
LVL 31

Accepted Solution

RiteshShah earned 400 total points
ID: 24353029
sorry I did typo in WHERE clause in above example, it should be like below snippet, even check your table and field name before you run.

declare @mail varchar(50)

select @mail=email from parent where username = @pusername;

select @mail

Assisted Solution

bull_rider earned 100 total points
ID: 24353047
You can do this in two ways. One which RiteshShah has suggested and the other way is to use the Set option to store a value into a variable.

Declare @mail nvarchar(255)

Set @mail = (SELECT email from parent where username = @pusername)

Select @mail

You can use anyone of them accotding to your convenience.
LVL 31

Expert Comment

ID: 24353082
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.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question