百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 优雅编程 > 正文

oracle 根据系统表自动生成简单的查询,修改,新增语句

sinye56 2024-09-27 21:11 3 浏览 0 评论

执行环境:pl/sql。主要适用于java/ibatis。

创建测试表

create table task.nayi_180904_01 (
login_id varchar2(50),
user_name varchar2(50),
age number,
create_time date default sysdate,
score number
);
comment on column task.nayi_180904_01.score is 'round_2|';
insert into task.nayi_180904_01(login_id, user_name, age, score)
 values('123456', 'name', 33, 123.4567);
commit;
1
2
3
4
5
6
7
8
9
10
11
12
13
14

测试代码

begin
 --第一个参数必须是带有用户名的完整表名。存储过程的所属用户必须有all_tab_columns,all_col_comments的查询权限
 task.nayi_test_p_180904('task.nayi_180904_01', '');
end;
1
2
3
4

输出

 select 
t1.login_id "loginId", 
t1.user_name "userName", 
t1.age "age", 
to_char(t1.create_time, 'yyyy/mm/dd') "createTime", 
round(t1.score, 2) "score" 
 from task.nayi_180904_01 t1 
 ; 
 update task.nayi_180904_01 t1 set 
t1.login_id = #params.loginId#,
t1.user_name = #params.userName#,
t1.age = #params.age#,
t1.create_time = to_date(#params.createTime#, 'yyyy/mm/dd'),
t1.score = #params.score#
 where 1 = 1 
 ; 
 insert into task.nayi_180904_01( 
login_id,
user_name,
age,
create_time,
score
 ) 
 values (
#params.loginId#,
#params.userName#,
#params.age#,
to_date(#params.createTime#, 'yyyy/mm/dd'),
#params.score#
 ) 
 ; 
 private String loginId ; 
 private String userName ; 
 private String age ; 
 private String createTime ; 
 private String score ; 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

完整代码

create or replace procedure task.nayi_test_p_180904(in_full_name in varchar2, in_type in varchar2) is
 v_user_name varchar2(30000);
 v_table_name varchar2(30000);
 my_cur sys_refcursor;
 v_temp_str varchar2(500);
begin
 v_user_name := substr(in_full_name, 1, instr(in_full_name, '.') - 1);
 v_table_name := substr(in_full_name, instr(in_full_name, '.') + 1);
 if in_type = 's' or in_type = 'all' or in_type is null then
open my_cur for
select text from (
select ' select ' text from dual union all
select*from (
select case when t1.DATA_TYPE = 'DATE' then
 'to_char(' || 't1.' || lower(t1.COLUMN_NAME) || ', ''yyyy/mm/dd'')'
 when t1.DATA_TYPE = 'NUMBER' then
 case when instr(t2.comments, 'round_') > 0 then
 'round(' || 't1.' || lower(t1.COLUMN_NAME) || ', ' || regexp_replace(regexp_substr(t2.comments, 'round_\d+\|'), '[^0-9]+') || ') '
 else 't1.' || lower(t1.COLUMN_NAME)
 end
 else 't1.' || lower(t1.COLUMN_NAME)
 end ||
 ' "' ||
 substr(lower(t1.COLUMN_NAME), 1, 1) ||
 substr(replace(initcap(lower(t1.COLUMN_NAME)), '_', ''), 2) || '"' ||
 decode(count(1) over(), row_number() over(order by t1.COLUMN_ID), '', ',') ||
 ' '
 from all_tab_columns t1, all_col_comments t2
 where 1 = 1
 and t1.OWNER = t2.owner
 and t1.TABLE_NAME = t2.table_name
 and t1.COLUMN_NAME = t2.column_name
 and t1.TABLE_NAME = upper(v_table_name)
 and t1.OWNER = upper(v_user_name)
 order by t1.COLUMN_ID) 
union all
select ' from ' || lower(v_user_name) || '.' || lower(v_table_name) || ' t1 ' from dual
union all
select ' ; ' from dual
) t1;
 loop fetch my_cur into v_temp_str;
 exit when my_cur%notfound;
 dbms_output.put_line(v_temp_str);
 end loop;
 close my_cur;
 dbms_output.put_line('');
 dbms_output.put_line('');
 end if;
 if in_type = 'u' or in_type = 'all' or in_type is null then
open my_cur for
select*from (
select ' update ' || in_full_name || ' t1 set ' text from dual union all
select * from (
select 't1.' || lower(t1.COLUMN_NAME) || 
 ' = ' || 
 decode(t1.DATA_TYPE, 'DATE', 'to_date(', '') ||
 '#params.' || 
 substr(lower(t1.COLUMN_NAME), 1, 1) ||
 substr(replace(initcap(lower(t1.COLUMN_NAME)), '_', ''), 2) ||
 '#' || 
 decode(t1.DATA_TYPE, 'DATE', ', ''yyyy/mm/dd'')', '') ||
 decode(count(1) over(), row_number() over(order by t1.COLUMN_ID), '', ',')
 from all_tab_columns t1, all_col_comments t2
 where 1 = 1
 and t1.OWNER = t2.owner
 and t1.TABLE_NAME = t2.table_name
 and t1.COLUMN_NAME = t2.column_name
 and t1.TABLE_NAME = upper(v_table_name)
 and t1.OWNER = upper(v_user_name)
 order by t1.COLUMN_ID
 ) union all
 select ' where 1 = 1 ' from dual union all
 select ' ; ' from dual
 ) t1
 ;
 loop fetch my_cur into v_temp_str;
 exit when my_cur%notfound;
 dbms_output.put_line(v_temp_str);
 end loop;
 close my_cur;
 dbms_output.put_line('');
 dbms_output.put_line('');
 end if;
 if in_type = 'i' or in_type = 'all' or in_type is null then
open my_cur for
select*from ( 
select ' insert into ' || in_full_name || '( ' text from dual union all
select*from (
select lower(t1.COLUMN_NAME) || 
 decode(count(1) over(), row_number() over(order by t1.COLUMN_ID), '', ',') ||
 decode(mod(row_number() over(order by t1.COLUMN_ID), 5), 0, chr(10), '') 
 from all_tab_columns t1, all_col_comments t2 
 where 1 = 1
 and t1.OWNER = t2.owner
 and t1.TABLE_NAME = t2.table_name
 and t1.COLUMN_NAME = t2.column_name
 and t1.TABLE_NAME = upper(v_table_name)
 and t1.OWNER = upper(v_user_name)
 order by t1.COLUMN_ID
 )
union all 
select ' ) ' from dual
union all
select ' values (' from dual
union all
select*from (
select decode(t1.DATA_TYPE, 'DATE', 'to_date(', '') ||
 '#params.' || 
 substr(lower(t1.COLUMN_NAME), 1, 1) ||
 substr(replace(initcap(lower(t1.COLUMN_NAME)), '_', ''), 2) ||
 '#' || 
 decode(t1.DATA_TYPE, 'DATE', ', ''yyyy/mm/dd'')', '') ||
 decode(count(1) over(), row_number() over(order by t1.COLUMN_ID), '', ',') ||
 decode(mod(row_number() over(order by t1.COLUMN_ID), 5), 0, chr(10), '') 
 from all_tab_columns t1, all_col_comments t2 
 where 1 = 1
 and t1.OWNER = t2.owner
 and t1.TABLE_NAME = t2.table_name
 and t1.COLUMN_NAME = t2.column_name
 and t1.TABLE_NAME = upper(v_table_name)
 and t1.OWNER = upper(v_user_name)
 order by t1.COLUMN_ID
 )
 union all
select ' ) ' from dual
 union all
select ' ; ' from dual
) t1
; 
 loop fetch my_cur into v_temp_str;
 exit when my_cur%notfound;
 dbms_output.put_line(v_temp_str);
 end loop;
 close my_cur;
 dbms_output.put_line('');
 dbms_output.put_line('');
 end if;
 if in_type = 'j' or in_type = 'all' or in_type is null then
open my_cur for
select*from (
select ' private String ' || 
 substr(lower(t1.COLUMN_NAME), 1, 1) ||
 substr(replace(initcap(lower(t1.COLUMN_NAME)), '_', ''), 2) || ' ; '
 from all_tab_columns t1, all_col_comments t2
 where 1 = 1
 and t1.OWNER = t2.owner
 and t1.TABLE_NAME = t2.table_name
 and t1.COLUMN_NAME = t2.column_name
 and t1.TABLE_NAME = upper(v_table_name)
 and t1.OWNER = upper(v_user_name)
 order by t1.COLUMN_ID
 ) t1;
 loop fetch my_cur into v_temp_str;
 exit when my_cur%notfound;
 dbms_output.put_line(v_temp_str);
 end loop;
 close my_cur;
 end if;
end nayi_test_p_180904;

相关推荐

Linux在线安装JDK1.8

首先在服务器pingwww.baidu.com查看是否可以连网然后就可以在线下载一、下载安装JDK1.81、在下载安装的同时做好一些准备工作...

Linux安装JDK,超详细

1、了解RPMRPM是Red-HatPackageManager(RPM软件包管理器)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的,现在包括OpenLinux...

Linux安装jdk1.8(超级详细)

前言最近刚购买了一台阿里云的服务器准备要搭建一个网站,正好将网站的一个完整搭建过程分享给大家!#一、下载jdk1.8首先我们需要去下载linux版本的jdk1.8安装包,我们有两种方式去下载安装...

Linux系统安装JDK教程

下载jdk-8u151-linux-x64.tar.gz下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.ht...

干货|JDK下载安装与环境变量配置图文教程「超详细」

1.JDK介绍1.1什么是JDK?SUN公司提供了一套Java开发环境,简称JDK(JavaDevelopmentKit),它是整个Java的核心,其中包括Java编译器、Java运行工具、Jav...

Linux下安装jdk1.8

一、安装环境操作系统:CentOSLinuxrelease7.6.1810(Core)JDK版本:1.8二、安装步骤1.下载安装包...

Linux上安装JDK

以CentOS为例。检查是否已安装过jdk。yumlist--installed|grepjdk或者...

Linux系统的一些常用目录以及介绍

根目录(/):“/”目录也称为根目录,位于Linux文件系统目录结构的顶层。在很多系统中,“/”目录是系统中的唯一分区。如果还有其他分区,必须挂载到“/”目录下某个位置。整个目录结构呈树形结构,因此也...

Linux系统目录结构

一、系统目录结构几乎所有的计算机操作系统都是使用目录结构组织文件。具体来说就是在一个目录中存放子目录和文件,而在子目录中又会进一步存放子目录和文件,以此类推形成一个树状的文件结构,由于其结构很像一棵树...

Linux文件查找

在Linux下通常find不很常用的,因为速度慢(find是直接查找硬盘),通常我们都是先使用whereis或者是locate来检查,如果真的找不到了,才以find来搜寻。为什么...

嵌入式linux基本操作之查找文件

对于很多初学者来说都习惯用windows操作系统,对于这个系统来说查找一个文件简直不在话下。而学习嵌入式开发行业之后,发现所用到的是嵌入式Linux操作系统,本想着跟windows类似,结果在操作的时...

linux系统查看软件安装目录的方法

linux系统下怎么查看软件安装的目录?方法1:whereis软件名以查询nginx为例子...

Linux下如何对目录中的文件进行统计

统计目录中的文件数量...

Linux常见文件目录管理命令

touch用于创建空白文件touch文件名称mkdir用于创建空白目录还可以通过参数-p创建递归的目录...

Linux常用查找文件方法总结

一、前言Linux系统提供了多种查找文件的命令,而且每种查找命令都具有其独特的优势,下面详细总结一下常用的几个Linux查找命令。二、which命令查找类型:二进制文件;...

取消回复欢迎 发表评论: