I came across a piece of T-SQL I was trying to convert into Oracle. It looks like this:
SET @local variable=
CASE
when exists (select field from table where value=0) then 0
when exists (select same field from same table where value=1) then 1
when exists (select same fieldfrom same table where value=2) then 1
else @local variable
END
The @local variable was set at the result of a query earlier in the procedure.
Now, I tried to convert this to Oracle in the following fashion:
BEGIN
SELECT CASE
WHEN EXISTS (
SELECT field
FROM table
WHERE value = 0
)
THEN 0
WHEN EXISTS (
SELECT same field
FROM same table
WHERE value = 1
)
THEN 1
WHEN EXISTS (
SELECT same field
FROM same table
WHERE value = 2
)
THEN 1
ELSE localvariable
END
INTO localvariable
FROM DUAL;
END;
However, PL/SQL doesn't seem to like me using the @localvariable in the else statement and in the into statement. I'm aware this may not be a well written query to begin with, but what is the solution here? How do I write this in legal PL/SQL?
The
CASE
andEXISTS
cannot be used in the way you expect.You can do something like this.