---
title: "PL/SQL执行动态SQL"
author: "lanrikey"
date: "2016-12-21T15:26:51Z"
tags: []
categories: ["数据库"]
slug: "pl-sql-sql"
draft: false
lastmod: "2016-12-21T16:55:24Z"
id: 2346557
url: "https://lanrikey.iteye.com/blog/2346557"
---

PL/SQL动态SQL 

依据：   
1.使用EXECUTE IMMEDIATE可以来执行动态SQL   
2.open cursor可以打开一个sql的查询，fetch cursor可以挨个获取查询记录 

示例   
1.使用EXECUTE IMMEDIATE查询一张数据表的记录数 

Sql代码  [![收藏代码](http://ajita.iteye.com/images/icon_star.png)]()

1. DECLARE
2. get\_count\_sql varchar2(100);
3. row\_count INT;
4. BEGIN
5. get\_count\_sql:='select count(\*) from bizbusinessbaseinfo';
6. EXECUTE IMMEDIATE get\_count\_sql INTO row\_count;
7. dbms\_output.put\_line(row\_count);
8. END;

2.使用EXECUTE IMMEDIATE执行动态删除语句 

Sql代码  [![收藏代码](http://ajita.iteye.com/images/icon_star.png)]()

1. DECLARE
2. delete\_sql varchar2(100);
3. BEGIN
4. delete\_sql:='delete from bizbusinessbaseinfo where bizname=''1''';
5. execute immediate delete\_sql;
6. END;

3.执行动态DDL语句 

Sql代码  [![收藏代码](http://ajita.iteye.com/images/icon_star.png)]()

1. DECLARE
2. drop\_sql varchar2(100);
3. BEGIN
4. drop\_sql:='DROP TABLE logrec\_action';
5. execute immediate drop\_sql;
6. END;

4.使用open cursor进行简单的动态查询 

Sql代码  [![收藏代码](http://ajita.iteye.com/images/icon_star.png)]()

01. DECLARE
02. query\_sql varchar2(100);
03. queryresult varchar2(50);
04. bizcursor SYS\_REFCURSOR;
05. BEGIN
06. query\_sql:='select bizname from bizbusinessbaseinfo';
07. OPEN bizcursor FOR query\_sql;
08. loop
09. fetch bizcursor into queryresult;
10. --判读是否提取到值，没取到值就退出
11. --取到值c\_job%notfound 是false
12. --取不到值c\_job%notfound 是true
13. exit when bizcursor%notfound;
14. dbms\_output.put\_line(queryresult);
15. end loop;
16. --关闭游标
17. close bizcursor;
18. END;

5.动态查询出多个字段(需要先定义好输出结果的类型，不方便用在动态查询中) 

Sql代码  [![收藏代码](http://ajita.iteye.com/images/icon_star.png)]()

01. DECLARE
02. query\_sql varchar2(100);
03. queryresult varchar2(50);
04. bizcursor SYS\_REFCURSOR;
05. type my\_record is record(
06. bizname varchar2(64),
07. bizstatus char(1));
08. my\_rec my\_record;
09. BEGIN
10. query\_sql:='select bizname,bizstatus from bizbusinessbaseinfo';
11. OPEN bizcursor FOR query\_sql;
12. loop
13. fetch bizcursor into my\_rec;
14. --判读是否提取到值，没取到值就退出
15. --取到值c\_job%notfound 是false
16. --取不到值c\_job%notfound 是true
17. exit when bizcursor%notfound;
18. dbms\_output.put\_line(my\_rec.bizname|| '    ' ||my\_rec.bizstatus);
19. end loop;
20. --关闭游标
21. close bizcursor;
22. END;

 

 

查询count

 

         --查询理赔次数

       

```
get_count_sql:=  'select  count(case  g.status when ''A'' then ''A'' end ) as countA ,
		count(case g.status when ''P'' then ''P'' end ) as countP  
		from gcAdjustmentPersonFee g ,GcAdjustmentPerson p
		where  g.lossNo=p.lossno and g.lossseqno=p.lossseqno and  g.paidserialno=p.paidserialno
		and  g.policyno='''||rec_selectInfoArgs.Policyno||''' and g.clientcode='''||rec_selectInfoArgs.Clientcode||'''
		and g.liabcode in (select * from table(str_split('''||rec_selectInfoArgs.Liabcode||''','',''))) and g.kindcode='''||rec_selectInfoArgs.Kindcode ||''''|| n_sql;

EXECUTE IMMEDIATE get_count_sql INTO n_countA,n_countP;  

n_remainlimit:=  rec_findGcPolicyLimitTemp.Shortlimit-n_countA;
n_predictlimit:=  rec_findGcPolicyLimitTemp.Shortlimit-n_countA-n_countP;
```

 

 

 

--查询多次条记录

 --记录表into类型

   

```
   bizcursor SYS_REFCURSOR; 
type my_record is record(  
      n_lossno gcpolicypaidhistorytemp.lossno%type,
      n_lossseqno gcpolicypaidhistorytemp.lossseqno%type,
      n_upload gcpolicypaidhistorytemp.upload%type); 
my_rec my_record;
n_historyId gcpolicypaidhistorytemp.historyid%type:=1;
 
 
get_detail_sql:='select  g.lossNo,g.lossSeqNo,m.dataUploadFlag
    from gcAdjustmentPersonFee g ,GcAdjustmentPerson p,gcAdjustmentMain m
     where  g.lossNo=p.lossno and g.lossseqno=p.lossseqno and  g.paidserialno=p.paidserialno and p.lossno=m.lossno and g.lossseqno=m.lossseqno
      and  g.policyno='''||rec_selectInfoArgs.Policyno||''' and g.clientcode='''||rec_selectInfoArgs.Clientcode||'''
      and g.liabcode in (select * from table(str_split('''||rec_selectInfoArgs.Liabcode||''','',''))) and g.kindcode='''||rec_selectInfoArgs.Kindcode ||''''|| n_sql;

OPEN bizcursor FOR get_detail_sql; 
loop fetch bizcursor into my_rec;  
 EXIT WHEN bizcursor%NOTFOUND; 
     insert into gcpolicypaidhistorytemp (historyId,limitId,policyno,endorseqno,endorno,lossNo,lossSeqNo,upload) 
	    values (n_historyId,rec_findGcPolicyLimitTemp.Limitid,rec_findGcPolicyLimitTemp.Policyno,rec_findGcPolicyLimitTemp.Endorseqno,rec_findGcPolicyLimitTemp.Endorno,
		   my_rec.n_lossno, my_rec.n_lossseqno, my_rec.n_upload);
	   n_historyId:=n_historyId+1;        
end loop;  
--关闭游标  
close bizcursor; 
```