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

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
LVL 13
davidlars99Asked:
Who is Participating?
 
Mr_PeerapolConnect With a Mentor Commented:
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
 
Mr_PeerapolCommented:
select @minRowId = min(RowId)  from @t1
0
 
SireesConnect With a Mentor Commented:
Try this

select @minRowId = min(RowId) from @t1
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
davidlars99Author Commented:
I did and it didn't work. I'll try again when I get home.


Thanks!
0
 
LandyJConnect With a Mentor Commented:
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
 
LandyJCommented:
Forgot to add: Here's a link to MSDN example that explains Mr_Peerapol's response.  

No points for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.