Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Assign result set to scalar?

Posted on 2013-06-05
2
287 Views
Last Modified: 2013-06-05
I have a standard procedure where  MyNumber is passed in as a parameter.
Using it, I want to get the value of tblMyTable.MyId and assign it to @MyId
How do I do this?
 
DECLARE @Myid as int
select [Myid] from [MyDB].[dbo].[tblMyTable]
where [MyNumber] = @MyNumber
-- @MyId = [Myid] ???
-- use @Myid at this point

Open in new window

0
Comment
Question by:allelopath
2 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 39222125
DECLARE @Myid as int
select  @Myid = [Myid] from [MyDB].[dbo].[tblMyTable]
where [MyNumber] = @MyNumber
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39222136
You have two variables used in your query, @Myid and @MyNumber. If you want to select from the table based on the passed-in variable only, then you don't need both.

DECLARE @MyNumber INT
SET @MyNumber = 12345

SELECT MyID, col1, col2, ..... FROM MyTable
WHERE MyID = @MyNumber

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access SQL Server instance by NONE-admin Windows user 12 26
CDC and AOG on MS SQL 2012 13 23
SQL query and VBA 5 41
SQL Server How-To Show Notes In First Row of Results 4 30
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

792 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