Solved

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

Posted on 2006-07-18
6
468 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
Comment Utility
select @minRowId = min(RowId)  from @t1
0
 
LVL 20

Assisted Solution

by:Sirees
Sirees earned 200 total points
Comment Utility
Try this

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

Author Comment

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


Thanks!
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Forgot to add: Here's a link to MSDN example that explains Mr_Peerapol's response.  

No points for me.
0

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.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now