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 2000 Forums
 SQL Server Development (2000)
 Select statement w/ Exec() function?

Author  Topic 

rpieszak
Starting Member

11 Posts

Posted - 2002-02-27 : 14:05:35
What would be the correct syntax for this (I'm getting a syntax error):

Select @myVar = Exec(@SQL)

The var @SQL is dynamically created and executed and I want @myVar set equal to the result. It's similar to this line:

Select @myVar = (Select top 1 myColumn From myTable)

but with an Exec() function. Any ideas? Thanks for any help.
Ryan



Edited by - rpieszak on 02/27/2002 14:06:39

Nazim
A custom title

1408 Posts

Posted - 2002-02-28 : 00:19:02
If you are using Sql 2000.

something like this should help you


create table #t(a int)
declare @sql as varchar(300)
declare @m int
select @sql='select max(job_id) from jobs'
insert into #t exec(@sql)
select @m=a from #t
drop table #t


--------------------------------------------------------------


Edited by - Nazim on 02/28/2002 01:12:59
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-02-28 : 04:00:05
Try sp_executesql

declare @myVar int
exec sp_executesql N'select @myVar = (Select top 1 myColumn From myTable)', N'@myVar int output', @myVar output
select @myVar


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

rpieszak
Starting Member

11 Posts

Posted - 2002-02-28 : 13:05:56
Thank you both, your suggestions worked, this is a huge step in what I need to get done. Thanks again!

Go to Top of Page
   

- Advertisement -