问题
Below query gets me
invalid sql statement
in Toad
IF EXISTS (SELECT T_BASIS_ACCESS_ID FROM ECKERNEL_MCA.T_BASIS_ACCESS WHERE ROLE_ID LIKE 'MCA.GFS.LEAD')
BEGIN
SELECT OBJECT_ID, NAME FROM ECKERNEL_MCA.OV_AREA WHERE END_DATE IS NULL AND OBJECT_ID IN
(SELECT DISTINCT REPLACE(REPLACE(REPLACE(ATTRIBUTE_TEXT, '(', '' ),')',''), '''', '')
FROM ECKERNEL_MCA.T_BASIS_OBJECT_PARTITION WHERE T_BASIS_ACCESS_ID IN
(SELECT T_BASIS_ACCESS_ID FROM ECKERNEL_MCA.T_BASIS_ACCESS WHERE ROLE_ID LIKE 'MCA.GFS.LEAD') )
END
ELSE
BEGIN
SELECT OBJECT_ID, NAME FROM ECKERNEL_MCA.OV_AREA WHERE END_DATE IS NULL
END
回答1:
This code really is invalid, as far as I can tell. The one that follows might compile. Note that:
- I created local variables which will hold the result of SELECT statements. You need them in PL/SQL
- I tried to show you that exceptions should be handled. There's no much use in "IF-THEN-ELSE" because - if the first SELECT (I named it "A") fails, it won't return NULL but raise NO_DATA_FOUND exception. I don't know your tables nor data you store in there, so - maybe this code is exception-free, I can't tell.
- I couldn't test it as I don't have your tables, and didn't feel like creating them myself.
declare
l_t_basis_access_id eckernel_mca.t_basis_access.t_basis_access_id%type;
l_object_id eckernel_mca.ov_area.object_id%type;
l_name eckernel_mca.ov_area.name%type;
begin
-- SELECT "A"
select t_basis_access_id
into l_t_basis_access_id
from eckernel_mca.t_basis_access
where role_id like 'MCA.GFS.LEAD';
-- SELECT "A" returned a single value - proceed with the next SELECT "B"
-- SELECT "B"
select object_id, name
into l_object_id,l_name
from eckernel_mca.ov_area
where end_date is null
and object_id in (select distinct replace(replace(replace(attribute_text,'(',''),')',''),'''','')
from eckernel_mca.t_basis_object_partition
where t_basis_access_id in (select t_basis_access_id
from eckernel_mca.t_basis_access
where role_id like 'MCA.GFS.LEAD'
)
);
exception
when no_data_found then
-- SELECT "A" didn't return anything and raised no_data_found; but, SELECT "B" might
-- raise it as well
select object_id, name
into l_object_id,l_name
from eckernel_mca.ov_area
where end_date is null;
when too_many_rows then
-- Any SELECT involved in this code could return it, so - handle it, if necessary
end;
来源:https://stackoverflow.com/questions/50179087/invalid-sql-statement-in-toad-oracle-db