|
View previous topic :: View next topic
|
| Author |
Message |
Daphne
New User
Joined: 28 Aug 2006 Posts: 27 Location: NY, US
|
|
|
|
Hi all,
I am trying some cases in stored procedure CASE , END-CASE statement.
Is it possible to enter an SQL query inside a WHEN clause.
For example
| Code: |
select Name,
case
when year='2006' then
select year from db2t.student_mark where year='2006' fetch first 1 rows only;
else
'2005'
end as year
from db2t.student_table
fetch first 10 rows only; |
I tried this, but got an error and I couldn't get any example with an SQL statement.
Appreciate your help on this,
Thanks,
Daphne |
|
| Back to top |
|
 |
wanderer
Active User

Joined: 05 Feb 2007 Posts: 199 Location: Sri Lanka
|
|
|
|
| What error did you get? I don't think a ';' at the end of first WHEN is needed. |
|
| Back to top |
|
 |
ashwinreddy
Active User
.jpg)
Joined: 16 Sep 2004 Posts: 106 Location: Hyderabad
|
|
|
|
Hi,
This is Not possible, as the case function doesn't recognise the inner query. I think you must have got an error like invalid select expected some other variable.
I have tried to use it long back and failed, instead i have changed the query.
Cheers
Ashwin |
|
| Back to top |
|
 |
Daphne
New User
Joined: 28 Aug 2006 Posts: 27 Location: NY, US
|
|
|
|
Hi Wanderer, Ashwin,
Thanks for your replies.
Wanderer,
I tried to remove the ";" but again I got the same. When I try to run in QMF, I get syntax error. Whereas when I just use a constant in place of the query it works fine.
Ashwin,
I thought the same, but I see lot of examples in books with "UPDATE" SQL statement. I haven't tried one, but I would think that if we can use UPDATE, why not SELECT?
Daphne |
|
| Back to top |
|
 |
wanderer
Active User

Joined: 05 Feb 2007 Posts: 199 Location: Sri Lanka
|
|
|
|
The CASE that you have used inside a SELECT statement, is a CASE expression. This is not the CASE procedural statement where ';' is needed at the end of THEN.
And, when you use CASE expressions, the thing that follows a THEN or ELSE is a result-expression containing arithmatic, date or other such operands. Not a statement like SELECT or UPDATE.
When you say that you have seen CASE with THEN followed by UPDATE, those must have been CASE procedural statements. But even in procedural CASE statements, the only SELECT that is allowed is a SELECT INTO(not a normal SELECT).
hope it helps. |
|
| Back to top |
|
 |
|
|