网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 数据库 > SQL技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
数据库:数据库教程,数据库技巧,Oracle教程,MySQL教程,Sybase教程,Access教程,DB2教程,数据库安全,数据库文摘
本月文章推荐
.用SQL 2005的ROW_NUMBER() 实现分.
.教你轻松掌握SQL Server错误信息.
.SQL Server 2005密码安全追踪与存.
.查出最早登录的数据和最晚登录的.
.小写转大写金额.
.带你深入了解数据库设计中的英文.
.如何使用分析函数来进行行和列的.
.Sql Server 2000数据库日志日益庞.
.使用SQL Server 2008管理非结构化.
.从算法入手讲解SQL Server的典型.
.讲解数据库加密技术的功能特性与.
.存储过程中如何使用事务.
.SQL SERVER应用问题解答13例(三.
.查询表主键外键信息的SQL.
.教你轻松掌握一个纵表转横表的"S.
.SQL Server 2000的数据转换服务(.
.讲解数据库及数据仓库建模方法的.
.MS SQL Oracle MySQL查出值为NUL.
.当您安装SQL Server 2005或94810.
.把二进制的字段以字符串形式Sele.

带你轻松接触一个可以自动创建过程的脚本

发表日期:2008-3-27


本文中,我们将介绍一个可以能自动创建过程的脚本,大家直接输入源泉表名和目标表名就可以创建以下链接中的全表复制过程。

create or replace procedure hyf_create_sql

(p_sour_tab varchar2,p_dst_tab varchar2) as
v_dstfile utl_file.file_type;
v_outname varchar2(50);
v_buffer varchar2(500);
type t_cur is ref cursor;
v_cur t_cur;
v_col_num number;
begin
select count(column_name) into v_col_num
from user_tab_columns
where table_name = upper(p_dst_tab);
v_outname := p_sour_tab || '.sql';
v_dstfile := utl_file.fopen('DIR1', v_outname, 'w', 32767);
v_buffer := 'create or replace procedure cp_' || p_sour_tab || ' as';
utl_file.put_line(v_dstfile, v_buffer);

open v_cur for 
select 'type TYPE_' || column_name || ' is table of ' 

|| table_name || '.' ||column_name || '%type;' as dd
from user_tab_columns
where table_name = upper(p_dst_tab)
order by column_id asc;
for i in 1..v_col_num loop
fetch v_cur into v_buffer; 
utl_file.put_line (v_dstfile,v_buffer);
end loop;
close v_cur ; 

open v_cur for 
select 'V_' || column_name || ' TYPE_' || column_name ||';' 
from user_tab_columns
where table_name = upper(p_dst_tab)
order by column_id asc;
for i in 1..v_col_num loop
fetch v_cur into v_buffer; 
utl_file.put_line (v_dstfile,v_buffer);
end loop;
close v_cur ; 

utl_file.put_line (v_dstfile,'type t_cur is ref cursor;');
utl_file.put_line (v_dstfile,'c_table t_cur;');
utl_file.put_line (v_dstfile,'v_sql varchar2(500);');
utl_file.put_line (v_dstfile,'v_rows number := 5000;');
utl_file.put_line(v_dstfile, 'begin'); 
utl_file.put_line(v_dstfile, 'execute immediate ''truncate table '||p_dst_tab||''';');
utl_file.put_line(v_dstfile, 'open c_table for');
utl_file.put_line(v_dstfile, ' select * from '||p_sour_tab||';');
v_buffer:= 'v_sql := ''insert /*+ APPEND*/ into '||p_dst_tab||' (';
utl_file.put_line (v_dstfile,v_buffer);


open v_cur for 
select column_name 
from user_tab_columns
where table_name = upper(p_dst_tab)
order by column_id asc; 

for i in 1..v_col_num loop
fetch v_cur into v_buffer; 
if i<> v_col_num then 
v_buffer:=v_buffer||',';
else 
v_buffer:=v_buffer||')' ;
end if;
utl_file.put_line (v_dstfile,v_buffer);
end loop;
close v_cur ;
v_buffer:= 'values (' ;
for i in 1..v_col_num loop
if i<> v_col_num then 
v_buffer:=v_buffer||':'||i||',';
else 
v_buffer:=v_buffer||':'||i||')'';';
end if;
end loop;
utl_file.put_line (v_dstfile,v_buffer); 

utl_file.put_line (v_dstfile,'loop ');
utl_file.put_line (v_dstfile,' fetch c_table ');
utl_file.put_line (v_dstfile, ' bulk collect into');
open v_cur for 
select 'v_'||column_name 
from user_tab_columns
where table_name = upper(p_dst_tab)
order by column_id asc;
for i in 1..v_col_num loop
fetch v_cur into v_buffer; 
if i<> v_col_num then 
v_buffer:=v_buffer||',';
end if;
utl_file.put_line (v_dstfile,v_buffer);
end loop;
close v_cur ;
utl_file.put_line (v_dstfile, ' limit v_rows;');
v_buffer:='forall i in 1 .. '||v_buffer||'.count execute immediate v_sql using';
utl_file.put_line (v_dstfile,v_buffer);

open v_cur for 
select 'v_'||column_name||'(i)' 
from user_tab_columns
where table_name = upper(p_dst_tab)
order by column_id asc;
for i in 1..v_col_num loop
fetch v_cur into v_buffer; 
if i<> v_col_num then 
v_buffer:=v_buffer||',';
else 
v_buffer:=v_buffer||';';
end if;
utl_file.put_line (v_dstfile,v_buffer);
end loop;
close v_cur ;
utl_file.put_line(v_dstfile, ' commit;');
utl_file.put_line(v_dstfile, ' exit when c_table%notfound;');
utl_file.put_line(v_dstfile, 'end loop;');
utl_file.put_line(v_dstfile, ' close c_table;');
utl_file.put_line(v_dstfile, 'end;');
utl_file.fclose(v_dstfile);
exception
when others then
if utl_file.is_open(v_dstfile) then
utl_file.fclose(v_dstfile);
end if;
raise;
end;
上一篇:Informix中查询database和table的占用空间 人气:1273
下一篇:如何使用PL/SQL读取数据库中的BLOB对象 人气:1028
浏览全部自动创建过程的脚本的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐