Solved

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

Posted on 2006-07-18
6
474 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
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 200 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 200 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 100 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

777 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