?
Solved

select [value] into [variable] from [table]

Posted on 2006-07-18
6
Medium Priority
?
490 Views
Last Modified: 2008-02-01
hi,

how is it possible to achieve something like this?

select min(RowId) into @minRowId from @t1

where @t1 is the table variable and @minRowId is the INT variable
0
Comment
Question by:davidlars99
[X]
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
6 Comments
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17133101
select @minRowId = min(RowId)  from @t1
0
 
LVL 20

Assisted Solution

by:Sirees
Sirees earned 800 total points
ID: 17133104
Try this

select @minRowId = min(RowId) from @t1
0
 
LVL 13

Author Comment

by:davidlars99
ID: 17133123
I did and it didn't work. I'll try again when I get home.


Thanks!
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 800 total points
ID: 17133245
I think what you mean is that @t1 is a varchar variable that holds a table name. If it is, you need a dynamic sql like this:

DECLARE @t1 VARCHAR(100)
DECLARE @minRowId INT
DECLARE @sql NVARCHAR(1000)

SET @sql = N'SELECT @minRowId = MIN(RowId) FROM ' + @t1
EXEC sp_executesql @sql ,
     N'@minRowId INT OUTPUT', @minRowId OUTPUT

SELECT @minRowId
0
 
LVL 7

Assisted Solution

by:LandyJ
LandyJ earned 400 total points
ID: 17133299
In order to get Output parameters from Dynamic SQL you have to use sp_ExecuteSQL as in:
How to specify output parameters when you use the sp_executesql stored procedure in SQL Server
http://support.microsoft.com/default.aspx?scid=kb;en-us;262499
0
 
LVL 7

Expert Comment

by:LandyJ
ID: 17133339
Forgot to add: Here's a link to MSDN example that explains Mr_Peerapol's response.  

No points for me.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

765 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