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
 Transact-SQL (2000)
 Subquery in Where clause . . .

Author  Topic 

carriehoff
Starting Member

29 Posts

Posted - 2009-03-19 : 09:49:52
Hi all,

Can the following query work? The subquery in the where clause actually returns the text for the where clause. Is there a way for the main query to evaluate the results of the subquery literally?

select count(*) As Total from DB1 where (select top 1 SQLWhere from DB2 where groupID = 1305)

For example, the subquery would return the result
zip = 33304, hopefully causing the main query to read like so:
select count(*) As Total from DB1 where zip = 33304

Can something like this be done?

Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-19 : 10:02:23
Try

declare @sqlwhere varchar(1000)
select top 1 @sqlwhere = SQLWhere from DB2 where groupID = 1305
EXEC('select count(*) As Total from DB1 where '+@sqlwhere)


Also read www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -