I have this code which in SQL*Plus runs ok but in PL/SQL it doesn't.
How can I come around with it?
SQL*PLUS
--------
delete from tblworkingcontent
where idworkingcontent in
(select idworkingcontent
from tblworkingcontent wc1
where wc1.metatags='a'
and (select count(*)
from tblworkingcontent wc2
where wc2.idworkingcontent
and wc2.metatags='a') <
(select count(*)
from tblworkingcontent wc3
where wc3.metatags='a'-1))
A scalar subquery is a subquery that returns a single row, this is a
new feature in Oracle8i which can be used in the SET clause of an UPDATE
statement and the VALUES clause of an INSERT statement - but such a statement
gives an error if placed in a PLSQL block
In versions below Oracle9i 9.0.1 the SQL and PL/SQL use different parsers, so
everything that work OK and is supported in SQL will not automatically work OK
and be supported in PL/SQL
Tomaž "A common mistake that people make when trying to design something completely
foolproof is to underestimate the ingenuity of complete fools" - Douglas Adams
I think you'd best start by cleaningup the logic. The section
"and (select count(*)
from tblworkingcontent wc2
where wc2.idworkingcontent and wc2.metatags=v_metatags) <
(select count(*)
from tblworkingcontent wc3
where wc3.metatags=v_metatags-v_numerorighe)"
depends on the table as a whole and not on any row selected by the
"in (select idworkingcontent
from tblworkingcontent wc1
where wc1.metatags=v_metatags . . . ."
so, as far as I can see, it just switches the whole delete on and off, so it should not be in the sub-query.
. . and I don't see why you need the IN clause . . . and there is a ; missing after the final )), isn't there?
How about:
Code:
delete from tblworkingcontent
where metatags=v_metatags
and
(select count(*)
from tblworkingcontent wc2
where wc2.idworkingcontent and wc2.metatags=v_metatags) <
(select count(*)
from tblworkingcontent wc3
where wc3.metatags=v_metatags-v_numerorighe)
;
And, if you want pl/sql, since your scalar subquery expressions
Originally posted by DaPi depends on the table as a whole and not on any row selected by the
outer query, you can execute them separately:
Code:
declare
v_metatags tblworkingcontent.metatags%type;
v_numerorighe int;
v_cnt1 number;
v_cnt2 number;
begin
select count(*) into v_cnt1
from tblworkingcontent wc2
where wc2.idworkingcontent and wc2.metatags=v_metatags;
select count(*) into v_cnt2
from tblworkingcontent wc3
where wc3.metatags=v_metatags-v_numerorighe;
delete from tblworkingcontent
where metatags=v_metatags
and v_cnt1 < v_cnt2;
end;
/
Tomaž "A common mistake that people make when trying to design something completely
foolproof is to underestimate the ingenuity of complete fools" - Douglas Adams
delete from tblworkingcontent
where idworkingcontent in
(select idworkingcontent
from tblworkingcontent wc1
where wc1.metatags='a'
and (select count(*)
from tblworkingcontent wc2
where wc2.idworkingcontent
(select count(*)
from tblworkingcontent wc3
where wc3.metatags='a'-1))
"delete from tblworkingcontent
where idworkingcontent in
(select idworkingcontent
from tblworkingcontent wc1
where wc1.metatags=v_metatags
and (select count(*)
from tblworkingcontent wc2
where wc2.idworkingcontent
and wc2.metatags=v_metatags) <
(select count(*)
from tblworkingcontent wc3
where wc3.metatags=v_metatags-v_numerorighe))
end;"
/
"wc2.idworkingcontent
It eliminates it it seems.
but this makes a difference.
excuse the mess.
Bookmarks