oracle存储过程wm_concat()函数⽆法使⽤distinct
SELECT A,B,WM_CONCAT(DISTINCT sn) sn,SUM(COUNTER)
FROM (
SELECT A,B,1 sn,COUNT(1) COUNTER
FROM SS1 S1
GROUP BY A,B
UNION ALL
SELECT A,B,2 sn,COUNT(1) COUNTER
FROM SS2 S2
GROUP BY A,B
)
GROUP BY A,B
HAVING SUM(COUNTER)>=15;
直接查询语句可以,但是将这个语句放到存储过程中insert则报错:
ora-30482:distinct option not allowed for this function
动态语句:
dingjun123@ORADB> declare
2 v_sql varchar2(100);
3 begin
4 insert into a select wm_concat(distinct name) from trademark;
5 end;
6 /
insert into a select wm_concat(distinct name) from trademark;
*
ERROR at line 4:
ORA-06550: line 4, column 22:
PL/SQL: ORA-30482: DISTINCT option not allowed for this function
ORA-06550: line 4, column 1:
PL/SQL: SQL Statement ignored
Elapsed: 00:00:00.00
dingjun123@ORADB> ed
Wrote file afiedt.buf
1 declare
2 v_sql varchar2(100);
3 begin
4 execute immediate ‘insert into a select wm_concat(distinct name) from trademark’;
5* end;
dingjun123@ORADB> /
PL/SQL procedure successfully completed.
11gR2版本的话,可考虑⽤ listagg 代替 wm_concat
如SELECT l, LISTAGG(distinct name, '; ')
WITHIN GROUP (ORDER BY l DESC) b
FROM (select trunc(level/2) l,‘a’||trunc(level/2) name from dual connect by level<=10)
GROUP BY l;
SQL> SELECT l, LISTAGG(name, '; ')
2 WITHIN GROUP (ORDER BY l DESC) b
3 FROM (select trunc(level/2) l,‘a’||trunc(level/2) name from dual connect by level<=10)
4 GROUP BY l;
0 a0
1 a1; a1
2 a2; a2
3 a3; a3
4 a4; a4
5 a5
==============================================
ORACLE EXECUTE IMMEDIATE ⼩结EXECUTE IMMEDIATE 代替了以前Oracle8i中DBMS_SQL package包.
它解析并马上执⾏动态的SQL语句或⾮运⾏时创建的PL/SQL块.动态创建和执⾏SQL语句性能超前,EXECUTE IMMEDIATE的⽬标在于减⼩企业费⽤并获得较⾼的性能,较之以前它相当容易编码.尽管DBMS_SQL仍然可⽤,但是推荐使⽤EXECUTE IMMEDIATE,因为它获的收益在包之上。
– 使⽤技巧
1. EXECUTE IMMEDIATE将不会提交⼀个DML事务执⾏,应该显式提交
如果通过EXECUTE IMMEDIATE处理DML命令,
那么在完成以前需要显式提交或者作为EXECUTE IMMEDIATE⾃⼰的⼀部分.
如果通过EXECUTE IMMEDIATE处理DDL命令,它提交所有以前改变的数据
oracle中trunc函数用法2. 不⽀持返回多⾏的查询,这种交互将⽤临时表来存储记录(参照例⼦如下)或者⽤REF cursors.
3. 当执⾏SQL语句时,不要⽤分号,当执⾏PL/SQL块时,在其尾部⽤分号.
4. 在Oracle⼿册中,未详细覆盖这些功能。
下⾯的例⼦展⽰了所有⽤到Execute immediate的可能⽅⾯.希望能给你带来⽅便.
5. 对于Forms开发者,当在PL/SQL 8.0.
6.3.版本中,Forms 6i不能使⽤此功能.
EXECUTE IMMEDIATE – ⽤法例⼦
1. 在PL/SQL运⾏DDL语句
begin
execute immediate ‘set role all’;
end;
2. 给动态语句传值(USING ⼦句)
declare
l_depnam varchar2(20) := ‘testing’;
l_loc varchar2(10) := ‘Dubai’;
begin
execute immediate ‘insert into dept values (:1, :2, :3)’
using 50, l_depnam, l_loc;
commit;
end;
3. 从动态语句检索值(INTO⼦句)
l_cnt varchar2(20);
begin
execute immediate ‘select count(1) from emp’
into l_cnt;
dbms_output.put_line(l_cnt);
end;
4. 动态调⽤例程.例程中⽤到的绑定变量参数必须指定参数类型.
认为IN类型,其它类型必须显式指定
declare
l_routin varchar2(100) := ‘_rowcnt’;
l_tblnam varchar2(20) := ‘emp’;
l_cnt number;
l_status varchar2(200);
begin
execute immediate 'begin ’ || l_routin || ‘(:2, :3, :4); end;’
using in l_tblnam, out l_cnt, in out l_status;
if l_status != ‘OK’ then
dbms_output.put_line(‘error’);
end if;
end;
5. 将返回值传递到PL/SQL记录类型;同样也可⽤%rowtype变量
declare
type empdtlrec is record (empno number(4),
ename varchar2(20),
deptno number(2));
empdtl empdtlrec;
begin
execute immediate 'select empno, ename, deptno ’ ||
‘from emp where empno = 7934’
into empdtl;
end;
6. 传递并检索值.INTO⼦句⽤在USING⼦句前
declare
l_dept pls_integer := 20;
l_nam varchar2(20);
l_loc varchar2(20);
begin
execute immediate ‘select dname, loc from dept where deptno = :1’into l_nam, l_loc
using l_dept ;
end;
7. 多⾏查询选项.对此选项⽤insert语句填充临时表,
⽤临时表进⾏进⼀步的处理,也可以⽤REF cursors纠正此缺憾.
l_sal pls_integer := 2000;
begin
execute immediate ‘insert into temp(empno, ename) ’ ||
’ select empno, ename from emp ’ ||
’ where sal > :1’
using l_sal;
commit;
end;
对于处理动态语句,EXECUTE IMMEDIATE ⽐以前可能⽤到的更容易并且更⾼效.
当意图执⾏动态语句时,适当地处理异常更加重要.应该关注于捕获所有可能的异常.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论