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.
| Author |
Topic |
|
abarsami
Yak Posting Veteran
68 Posts |
Posted - 2004-02-18 : 20:40:03
|
| I want to use the following in stored proc.It works in vb -- but not in sql. Can it be done. Select case @strint case 1 select * from table a case 2 select * from table bend select |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2004-02-18 : 22:47:23
|
Can do it as dynamic sql in a CASE function such asDECLARE @ExecStr VARCHAR(500)SELECT @ExecStr = (CASE @StrIntWHEN 1 THEN 'SELECT * FROM tableA'WHEN 2 THEN 'SELECT * FROM TableB'ELSE somethingEND)EXEC(@ExecStr) Or with IF statements.Sarah Berger MCSD |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-02-18 : 23:37:09
|
| Sarah's solution will work, but this may work without dynamic SQL:The CASE syntax in SQL is similar to VB, BUT it's actually an expression that returns scalar values, not recordsets.You would need to build an IF statement to do the equivalent function in SQLIF @strint = 1 BEGIN SELECT * FROM TABLEAEND ELSE BEGIN SELECT * FROM TABLEBENDIt's possible to pretzel other solutions (if the table return similar columns)SELECT * FROM TABLEA WHERE @strint = 1UNION ALLSELECT * FROM TABLEB WHERE @strint <> 1 |
 |
|
|
|
|
|