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)
 dynamic query in CTE definition

Author  Topic 

netedk
Starting Member

11 Posts

Posted - 2007-08-08 : 11:56:31
Hello,

Is there a way to define a CTE with a dynamic query? What I want to do is somethign like this:

declare @strg varchar(1000), @where varchar(1000)
set @strg = 'select email, id from employees'
set @strg = @strg + 'where id between 1000 and 2000';

WITH table1 (rid, email)
AS
(
exec (@strg)
)

select *
from table1


Any help is welcome.

Thanks

Marketing Communications: http://www.realmagnet.com
Software Development:http://www.daksatech.com

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-08 : 12:31:24
no you can't do that.
you have to have the whole CTE dynamic.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -