Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Using stringquery for cursor.

Author  Topic 

Passero
Starting Member

12 Posts

Posted - 2008-01-21 : 06:16:32
I have this query that is dynamicall build:
'SELECT MAX(' + @table_name + '_id) FROM ' + @table_name + ' WHERE ' + @table_name + '_id <100000;'
I need the max(...) value in another var but i don't know how to extract it.

When i use exec ('SELECT....') i can see the result in my query browser but i want something lik this:

declare @val int;
select @val = max(...) from...

but i don't know how to do this...

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-21 : 06:22:56
sp_executesql

Read all about dynamic sql here
http://www.sommarskog.se/dynamic_sql.html



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

ns_nataly
Starting Member

13 Posts

Posted - 2008-01-21 : 06:38:35
declare @sql varchar(1000)
declare @var int
set @sql = 'SELECT MAX(' + @table_name + '_id) FROM ' +
@table_name + ' WHERE ' + @table_name + '_id <100000;'

declare @t table (nn int)
Insert into @t (nn) exec (@sql)
select @var=nn from @t

Natalia
Go to Top of Page
   

- Advertisement -