Using Recursive Subquery Factoring in a function
963814Sep 23 2012 — edited Sep 24 2012I need to use Recursive Subquery Factoring for a project of mine, specifically I need to use some code I have found in a function. The code is for finding solutions for sudokus and is from here.
http://technology.amis.nl/2009/10/13/oracle-rdbms-11gr2-solving-a-sudoku-using-recursive-subquery-factoring/
The code I have so far is this:
create or replace
function recursive
(
pattern in raw
, solution in raw
) return number as
t varchar;
begin
with x( s, ind ) as
( select sud, instr( sud, '' '' )
from ( select ''53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79'' sud from dual )
union all
select substr( s, 1, ind - 1 ) || z || substr( s, ind + 1 )
, instr( s, '' '', ind + 1 )
from x
, ( select to_char( rownum ) z
from dual
connect by rownum <= 9
) z
where ind > 0
and not exists ( select null
from ( select rownum lp
from dual
connect by rownum <= 9
)
where z = substr( s, trunc( ( ind - 1 ) / 9 ) * 9 + lp, 1 )
or z = substr( s, mod( ind - 1, 9 ) - 8 + lp * 9, 1 )
or z = substr( s, mod( trunc( ( ind - 1 ) / 3 ), 3 ) * 3
+ trunc( ( ind - 1 ) / 27 ) * 27 + lp
+ trunc( ( lp - 1 ) / 3 ) * 6
, 1 )
)
)
select s
from x
into t
where ind = 0;
return 1;
end recursive;
But when trying to compile it in Oracle SQL Developer, I get the following two errors:
Error(9,1): PL/SQL: SQL Statement ignored
Error(10,30): PL/SQL: ORA-00907: missing right parenthesis
The errors refer to these two lines:
with x( s, ind ) as
( select sud, instr( sud, '' '' )
I have looked over and over the code again, and I do not see any missing parenthesis. Furthermore I tried using with in a smaller test function:
create or replace function test
(
p in number
)
return number as
v number;
begin
with t(a) as
(
select p as a from dual
union all
select a+1 from t where a< 10
)
select sum(a) into v from t;
return v;
end test;
Which compiles just fine, so I'm not sure why that line is being ignored in this function. The whole idea here is to replace the sudoku string in the function with a variable that I built from the two parameters.
Edited by: 960811 on 23-Sep-2012 18:07