oracle 执行sql,Oracle动态执行SQL

方式1

CREATE OR REPLACE PROCEDURE demo(salary IN NUMBER) AS

cursor_name INTEGER;

rows_processed INTEGER;

BEGIN

cursor_name := dbms_sql.open_cursor;

dbms_sql.parse(cursor_name, 'DELETE FROM emp WHERE sal > :x',

dbms_sql);

dbms_sql.bind_variable(cursor_name, ':x', salary);

rows_processed := dbms_sql.execute(cursor_name);

dbms_sql.close_cursor(cursor_name);

EXCEPTION

WHEN OTHERS THEN

dbms_sql.close_cursor(cursor_name);

END;

CREATE OR REPLACE PROCEDURE exec(STRING IN varchar2) AS

cursor_name INTEGER;

ret INTEGER;

BEGIN

cursor_name := DBMS_SQL.OPEN_CURSOR;

--DDL statements are executed by the parse call, which

--performs the implied commit

DBMS_SQL.PARSE(cursor_name, string, DBMS_SQL);

ret := DBMS_SQL.EXECUTE(cursor_name);

DBMS_SQL.CLOSE_CURSOR(cursor_name);

END;

execute immediate "drop table tab_temp";

方式2

CREATE OR REPLACE PROCEDURE copy(source      IN VARCHAR2,

destination IN VARCHAR2) is

-- This procedure copies rows from a given source table to a

-- given destination table assuming that both source and

-- destination tables have the following columns:

--   - ID of type NUMBER,

--   - NAME of type VARCHAR2(30),

--   - BIRTHDATE of type DATE.

id                 NUMBER;

name               VARCHAR2(30);

birthdate          DATE;

source_cursor      INTEGER;

destination_cursor INTEGER;

ignore             INTEGER;

BEGIN

-- prepare a cursor to select from the source table

source_cursor := dbms_sql.open_cursor;

DBMS_SQL.PARSE(source_cursor,

'SELECT id, name, birthdate FROM ' || source,

DBMS_SQL);

DBMS_SQL.DEFINE_COLUMN(source_cursor, 1, id);

DBMS_SQL.DEFINE_COLUMN(source_cursor, 2, name, 30);

DBMS_SQL.DEFINE_COLUMN(source_cursor, 3, birthdate);

ignore := DBMS_SQL.EXECUTE(source_cursor);

-- prepare a cursor to insert into the destination table

destination_cursor := DBMS_SQL.OPEN_CURSOR;

DBMS_SQL.PARSE(destination_cursor,

'INSERT INTO ' || destination ||

' VALUES (:id, :name, :birthdate)',

DBMS_SQL);

-- fetch a row from the source table and

-- insert it into the destination table

LOOP

IF DBMS_SQL.FETCH_ROWS(source_cursor)>0 THEN

-- get column values of the row

DBMS_SQL.COLUMN_VALUE(source_cursor, 1, id);

DBMS_SQL.COLUMN_VALUE(source_cursor, 2, name);

DBMS_SQL.COLUMN_VALUE(source_cursor, 3, birthdate);

-- bind the row into the cursor that inserts into the

-- destination table

-- You could alter this example to require the use of

-- dynamic SQL by inserting an if condition before the

-- bind.

DBMS_SQL.BIND_VARIABLE(destination_cursor, 'id', id);

DBMS_SQL.BIND_VARIABLE(destination_cursor, 'name', name);

DBMS_SQL.BIND_VARIABLE(destination_cursor, 'birthdate',

birthdate);

ignore := DBMS_SQL.EXECUTE(destination_cursor);

ELSE

-- no more row to copy

EXIT;

END IF;

END LOOP;

-- commit and close all cursors

COMMIT;

DBMS_SQL.CLOSE_CURSOR(source_cursor);

DBMS_SQL.CLOSE_CURSOR(destination_cursor);

EXCEPTION

WHEN OTHERS THEN

IF DBMS_SQL.IS_OPEN(source_cursor) THEN

DBMS_SQL.CLOSE_CURSOR(source_cursor);

END IF;

IF DBMS_SQL.IS_OPEN(destination_cursor) THEN

DBMS_SQL.CLOSE_CURSOR(destination_cursor);

END IF;

RAISE;

END;

方式3

declare

stmt varchar2(200);

dept_no_array dbms_sql.Number_Table;

c number;

dummy number;

begin

dept_no_array(1) := 10; dept_no_array(2) := 20;

dept_no_array(3) := 30; dept_no_array(4) := 40;

dept_no_array(5) := 30; dept_no_array(6) := 40;

stmt := 'delete from emp where deptno = :dept_array';

c := dbms_sql.open_cursor;

dbms_sql.parse(c, stmt, dbms_sql.native);

dbms_sql.bind_array(c, ':dept_array', dept_no_array, 1, 4);

dummy := dbms_sql.execute(c);

dbms_sql.close_cursor(c);

exception when others then

if dbms_sql.is_open(c) then

dbms_sql.close_cursor(c);

end if;

raise;

end;

declare

stmt varchar2(200);

empno_array dbms_sql.Number_Table;

empname_array dbms_sql.Varchar2_Table;

c number;

dummy number;

begin

for i in 0..9 loop

empno_array(i) := 1000 + i;

empname_array(I) := get_name(i);

end loop;

stmt := 'insert into emp values(:num_array, :name_array)';

c := dbms_sql.open_cursor;

dbms_sql.parse(c, stmt, dbms_sql.native);

dbms_sql.bind_array(c, ':num_array', empno_array);

dbms_sql.bind_array(c, ':name_array', empname_array);

dummy := dbms_sql.execute(c);

dbms_sql.close_cursor(c);

exception when others then

if dbms_sql.is_open(c) then

dbms_sql.close_cursor(c);

end if;

raise;

end;

declare

stmt varchar2(200);

emp_no_array dbms_sql.Number_Table;

emp_addr_array dbms_sql.Varchar2_Table;

c number;

dummy number;

begin

for i in 0..9 loop

emp_no_array(i) := 1000 + i;

emp_addr_array(I) := get_new_addr(i);

end loop;

stmt := 'update emp set ename = :name_array

where empno = :num_array';

c := dbms_sql.open_cursor;

dbms_sql.parse(c, stmt, dbms_sql.native);

dbms_sql.bind_array(c, ':num_array', empno_array);

dbms_sql.bind_array(c, ':name_array', empname_array);

dummy := dbms_sql.execute(c);

dbms_sql.close_cursor(c);

exception when others then

if dbms_sql.is_open(c) then

dbms_sql.close_cursor(c);

end if;

raise;

end;

方式4

declare

c       number;

d       number;

n_tab   dbms_sql.Number_Table;

indx    number := -10;

begin

c := dbms_sql.open_cursor;

dbms_sql.parse(c, 'select n from t order by 1', dbms_sql);

dbms_sql.define_array(c, 1, n_tab, 10, indx);

d := dbms_sql.execute(c);

loop

d := dbms_sql.fetch_rows(c);

dbms_sql.column_value(c, 1, n_tab);

exit when d != 10;

end loop;

dbms_sql.close_cursor(c);

exception when others then

if dbms_sql.is_open(c) then

dbms_sql.close_cursor(c);

end if;

raise;

end;

declare

c       number;

d       number;

n_tab  dbms_sql.Number_Table;

d_tab1 dbms_sql.Date_Table;

v_tab  dbms_sql.Varchar2_Table;

d_tab2 dbms_sql.Date_Table;

indx number := 10;

begin

c := dbms_sql.open_cursor;

dbms_sql.parse(c, 'select * from multi_tab order by 1', dbms_sql);

dbms_sql.define_array(c, 1, n_tab,  5, indx);

dbms_sql.define_array(c, 2, d_tab1, 5, indx);

dbms_sql.define_array(c, 3, v_tab,  5, indx);

dbms_sql.define_array(c, 4, d_tab2, 5, indx);

d := dbms_sql.execute(c);

loop

d := dbms_sql.fetch_rows(c);

dbms_sql.column_value(c, 1, n_tab);

dbms_sql.column_value(c, 2, d_tab1);

dbms_sql.column_value(c, 3, v_tab);

dbms_sql.column_value(c, 4, d_tab2);

exit when d != 5;

end loop;

dbms_sql.close_cursor(c);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/525999.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle 满足条件取第一行,sql – 如何只选择满足条件的第一行?

您可以使用分析查询:select *from (select p.*, v.*,row_number() over (partition by p.id order by v.userid) as rnfrom prmprofile pjoin user v on v.profile p.idwhere p.language 0and v.userid like %TEST%)where rn 1;内部查询获取所有数据(但使用*不理…

Linux怎么把目录设置群组,linux设置目录和文件使用权限

ext2fs文件系统中的目录及文件,可依实际需要来设置可读取、可写入与可执行等权限。以下我们就来了解文件和目录的权限设置。ext2fs文件系统中的目录及文件,可依实际需要来设置可读取、可写入与可执行等权限。以下我们就来了解文件和目录的权限设置&#…

linux异常关机内存,linux关机及问题解决

一般linux关机会用到的命令就是shutdown命令shutdown命令在执行的时候会自动去调用sync这个工具,这个工具的作用是把内存里面的东西写入硬盘。不过为了安全,一般我们在执行shutdown命令的时候都用去执行几次sync这个命令。shutdown -k noticesshutdown -…

linux查看redis内存,Linux查看redis占用内存的方法

redis-cliauth 密码info# Memoryused_memory:13490096 //数据占用了多少内存(字节)used_memory_human:12.87M //数据占用了多少内存(带单位的,可读性好)used_memory_rss:13490096 //redis占用了多少内存used_memory_peak:15301192 //占用内存的峰值(字节)used_memor…

linux子进程父进程例子,linux 子进程访问父进程

问题分析ECS Linux 系统下 Apache 的默认工作模式是 prefork MPM,使用多个子进程,每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接,效率高,但内存占用量比较大。如果不做调整,访问量增大可能造成 A…

linux 查看进程的信号,Linux 进程信号查看与控制

Linux 进程信号查看与控制1) SIGHUP 本信号在用户终端连接 (正常或非正常) 结束时发出通常是在终端的控制进程结束时通知同一 session 内的各个作业这时它们与控制终端不再关联?2) SIGINT 程序终止 (interrupt) 信号在用户键入 INTR 字符 (通常是 Ctrl-C) 时发出?3) SIGQUIT …

linux查看发起ddos攻击的ip,在Linux上使用netstat命令查证DDOS攻击的方法

导读DOS攻击或者DDOS攻击是试图让机器或者网络资源不可用的攻击。这种攻击的攻击目标网站或者服务通常是托管在高防服务器比如银行,信用卡支付网管,甚至根域名服务器。服务器出现缓慢的状况可能由很多事情导致,比如错误的配置,脚本…

linux还原windows,双系统如何删除Linux,恢复Windows从  MBR引导启动?

双系统如何删除Linux,恢复Windows从MBR引导启动?现在愿意尝试Linux的人越来越多了。通常,如果一台电脑里已经装有了Windows,再装Linux,安装时,Linux的grub引导程 序就会覆盖掉保存在MBR当中的原来的Windows…

linux ftp下载函数函数,FTP下载的函数

最近写的一个到指定FTP下载服务器上下载的函数,主要用到类有CInternetSession,CFtpConnection具体内容看代码吧,这个不能的!虽然很简单,不值得往外写,但我想收藏起来留着以后用也是很有意义的。/*********************…

linux应用程序逆向,Linux下查看并下载命令源码包(根据命令/应用程序逆向获取并且安装其所属源码包)...

使用linux的过程中,我们会熟悉各种命令,偶尔我们不禁会问,这些命令是怎么实现的,学习他们其实是学习linux高级系统编程很快捷的方法。这些命令的源码肯定是存放在相应的包里面,但是是哪些包呢?发行版的包管…

linux路由信息预览为空,route - 显示并设置Linux中静态路由表

补充说明route命令 用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中设置路由通常是为了解决以下…

linux maven自动构建,Centos7.3+Jenkins+Git+Maven 自动化构建部署项目

第一步 禁止 SELINUX 访问控制修改配置之前先备份(良好习惯)sudo cp /etc/selinux/config /etc/selinux/config.bak备份后,修改selinux配置sudo vi /etc/selinux/config将SELINUXenforcing改为SELINUXdisabled第二步 卸载系统自带的OpenJDK以及相关的java文件 安装J…

linux windows变色龙,体验开源变色龙SUSE Linux Enterprise Server 11

体验开源变色龙SUSE Linux Enterprise Server 11SUSE Linux Enterprise Server 11主要针对的是企业用户,SUSE产品分为SUSE Linux Enterprise Desktop(SLED)和SUSE Linux Enterprise Server(SLES)两个版本。笔者测试的版本为Server版本,主要面向SMB甚至是…

c语言简单编程题模板,C语言编程题,比较简单

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include int main(){int mark0,falut0;float answer0,k1;char m;int n;int i1;float timu(char q);float re(float a,float b,char h);printf("please input the number of the question you want to answer and th…

c语言建立顺序表 存储并输出,请教数据结构课程中怎么建立顺序表,显示,输出,从头到尾详细点最好加讲解,希望大家能帮这个小忙...

#include#define MAXSIZE 20typedef int ElemType;typedef struct{ElemType elem[MAXSIZE];int length;}SqList;//顺序表结构定义void InitList(SqList &L){//初始化函数//操作结果:构造一个空在顺序线性表 L.length0;return;}void ClearList(SqList &L){//…

杭州师范大学c语言程序设计机试,2016年杭州师范大学杭州国际服务工程学院程序设计基础考研复试题库...

一、选择题1. C 语言源程序名的后缀是( )A.C B.exe C.obj D.cp 答:A 【解析】C 语言源程序名的后缀为.C2. 以下选项中不合法的标识符是A.printB.FORD._00 答:C【解析】标识符是由若干个字符组成的字符序列,用来命名程序的一些实体。语法规则为…

在c语言中关于静态变量的说法正确的有,关于static变量,请选择下面所有说法正确的内容。...

static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念。被static修饰的成员变量和成员方法独立于该类的任何对象。也就是说,它不依赖类特定的实例&…

c语言数组下标越界检查程序,数组下标越界

已结贴√问题点数:20 回复次数:11数组下标越界题目是一个有10个元素的数组,存有10个考生的分数,写5个函数,分别计算总分,最高分,最低分,平均分,分数升序排列。我写了5函数…

c语言标准整形,C语言整形数值范围问题

有符号二进制数的表示是这样的&#xff1a;如果计算机的字长为n位&#xff0c;n位二进制数的最高位为符号位。其余n-1位为数值位&#xff0c;采用补码表示法时&#xff0c;可表示的数X的范围是 -2的(n-1)次幂 < X < 2的(n-1)次幂-1。如果字长是16位&#xff0c;补码表示的…

c语言switch comiti,国际经济学作业复习资料第三章.docx

Chapter 3 Labor Productivity and Comparative Advantage —The Ricardian Model■ Multiple Choice QuestionsCountnes trade with each other because they areand because of.different, costssinular, scale economiesdifferent, scale economiessimilar, costsNone of th…