Solved

MSSQL how to get value from table into variable ?

Posted on 2009-05-11
5
532 Views
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
0
Comment
Question by:cooking
  • 4
5 Comments
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
declare @mail varchar(50)

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

select @mail
0
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
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.
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 400 total points
Comment Utility
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
0
 
LVL 6

Assisted Solution

by:bull_rider
bull_rider earned 100 total points
Comment Utility
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.
0
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now