Good Day,
I have a PL-SQL query as follows. I’m trying to execute a query and store the results into a variable. So far I have this query which works:
SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
FROM
(SELECT
COUNT(*) AS TOTAL_COUNT
FROM DEV.BASEOBJECT INNER JOIN
DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
GROUP BY DEV.BASEOBJECT.ID,
DEV.BASEOBJECT.FIRST_NAME,
DEV.BASEOBJECT.LAST_NAME,
DEV.BASEOBJECT.CITY,
DEV.BASEOBJECT.STATE,
DEV.BASEOBJECT.ZIP,
DEV.ANIMAL.ID,
DEV.ANIMAL.NAME,
DEV.ANIMAL.BREED,
DEV.ANIMAL.DATE_OF_BIRTH,
DEV.ANIMAL.GENDER,
DEV.ANIMAL.SPECIES
HAVING (COUNT(*) > 1));
But when I try to save the results into a variable with this query:
DECLARE
v_name VARCHAR2(2);
BEGIN
SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
INTO v_name
FROM
(SELECT
COUNT(*) AS TOTAL_COUNT
FROM DEV.BASEOBJECT INNER JOIN
DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
GROUP BY DEV.BASEOBJECT.ID,
DEV.BASEOBJECT.FIRST_NAME,
DEV.BASEOBJECT.LAST_NAME,
DEV.BASEOBJECT.CITY,
DEV.BASEOBJECT.STATE,
DEV.BASEOBJECT.ZIP,
DEV.ANIMAL.ID,
DEV.ANIMAL.NAME,
DEV.ANIMAL.BREED,
DEV.ANIMAL.DATE_OF_BIRTH,
DEV.ANIMAL.GENDER,
DEV.ANIMAL.SPECIES
HAVING (COUNT(*) > 1));
END
I get an error:
ERROR
ORA-06550: line 1, column 8:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "" was ignored.
Eventually, this is going to be a stored procedure, but I don’t have that part ready yet, because I wanted to verify that what I have is working so far.
TIA,
coson
How are you running this? What tools are you using? I think that the problem is your tool/IDE is trying to compile this as a function/procedure. This code looks ok and would probably run with no problems if you ran it as a script in SQL*Plus. If you run this as a script, you should add a
dbmbs_output.put_line(v_name)after the query to see the result.