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)
 Using CASE in a FROM clause to dynamically build query.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-09-25 : 07:28:57
John writes "I read your article about using Case to dynamically build a select statement within a stored procedure. Worked on the basic select, but fails sytax check when I try to apply a CASE to build an IN condition for filtering in the from clause. Syntax checker points to the 'CASE @StatementType' line. I says "Incorrect syntax near the word case".


select AcctPeriod,
MasterAcctNbr,
Case @CombDiv -- this works great, thanks!!!
When 'C' then 0
When 'D' then Divnbr
End as DivNbr
from pvw_GL_Totals_By_Acct
where AcctType in
Case @StatementType --error occurs here
When 'BS' then ('A','L','C')
When 'PL' then ('E','R')
End


My goal is to pass two parameters to my procedure, @CombDiv and @StatementType, and dymically build the sql select from that. The resulting from clause would be either


Where AcctType in ('A','L','C')
or
Where AcctType in ('E','R')



Thanks,

John Farmer
Windows XPPro sp2
MSSQL 2000 (developer) sp4"

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-25 : 07:40:29


select AcctPeriod,
MasterAcctNbr,
Case @CombDiv -- this works great, thanks!!!
When 'C' then 0
When 'D' then Divnbr
End as DivNbr
from pvw_GL_Totals_By_Acct
where (AcctType in ('A','L','C') And @StatementType =
'BS') or (AcctType in ('A','L','C') And @StatementType = 'PL')


Chirag
Go to Top of Page
   

- Advertisement -