Oracle扩展    

1、CASE WHEN

2、NVL()函数

3、exists 和not exists

4、oracle 中(+)是什么意思

5、一个简单的视图

6、查看锁表、解锁

7、查询时增加字段的语句

8、创建GUID的函数

9、按照第2个字段降序查询

10、where 1=1

11、查看该序列是否存在

12、创建和已有视图一样字段的表

13、删表

14、从虚表中查询下一个序列的值

15、创建一个视图

16、用SQL语句查询数据库中带有某个字段的所有表名

17、decode函数

18、单行行数之rpad()函数

19、查询表中有多少行记录

20、如果存在表就删除不存在就创建

21、查询当前用户名下的表的信息

22、批量修改sql代码块

23、时间格式修改

24、SUM( )函数

25、存储过程(集合、for循环)

26、代码块,对表进行加列

27、创建包及包体

28、获取系统当前月份的最后一天

29、去空格操作

30、正数返回1,负数返回-1,零返回0

31、截取字符串

32、字符串拼接

33、union和union all

34、类似IF判断

100、数据库的对象

1、CASE WHEN

1.1、简单Case函数

CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END

select CASE 0                                                                                                                                                                                                                           
WHEN 1 THEN                                                                                                                                                                                                                             
'男'                                                                                                                                                                                                                                     
WHEN 2 THEN                                                                                                                                                                                                                             
'女'                                                                                                                                                                                                                                     
ELSE                                                                                                                                                                                                                                    
'其他'                                                                                                                                                                                                                                    
END                                                                                                                                                                                                                                     
from dual;

1.2、Case搜索函数

CASE
WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END ‘默认’

select CASE                                                                                                                                                                                                                             
WHEN 0 = 1 THEN                                                                                                                                                                                                                         
'男'                                                                                                                                                                                                                                     
WHEN 0 = 2 THEN                                                                                                                                                                                                                         
'女'                                                                                                                                                                                                                                     
ELSE                                                                                                                                                                                                                                    
'其他'                                                                                                                                                                                                                                    
END                                                                                                                                                                                                                                     
from dual;
--可充当三目表达式使用                                                                                                                                                                                                                            
select  (case when length(t.billlastdate)=14 then 1 when length(t.billlastdate)=19 then 2 else 3 end) as bbb from PAY_LQD_QUOTA_BILL t

2、NVL( )函数

2.1、NVL( )函数

NVL函数的格式如下: NVL(expr1,expr2)

含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值。

2.2、NVL2( )函数

NVL2函数的格式如下: NVL2(expr1,expr2, expr3)

含义是:如果该函数的第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第三个参数的值。

3、exists 和not exists

exists表示()内子查询语句返回结果不为空说明where条件成立就会执行主sql语句,如果为空就表示where条件不成立,sql语句就不会执行。not exists和exists相反,子查询语句结果为空,则表示where条件成立,执行sql语句。否则不执行。

例:1.如果部门名称中含有字母A,则查询所有员工信息(使用exists)

select * from emp where exists (select * from dept where dname like '%A%' and deptno = emp.deptno) temp and deptno=temp.deptno;

4、oracle 中(+)是什么意思

oracle 中的(+)是一种特殊的用法,(+)表示外连接,并且总是放在非主表的一方。

例如

左外连接:select A.a,B.a from A LEFT JOIN B ON A.b=B.b;

等价于select A.a,B.a from A,B where A.b = B.b(+);

再举个例子,这次是右外连接:select A.a,B.a from A RIGHT JOIN B ON A.b=B.b;

等价于select A.a,B.a from A,B where A.b (+) = B.b;

5、一个简单的视图

create or replace view ep_v_bfbudgetreg as                                                                                                                                                                                              
select b.ADJUSTID,b.BILLID,b.BILLCODE,b.BILLSEQ,b.VOUCHERID,b.BILLMEMO,b.BILLCREATEDATE,                                                                                                                                                
b.BILLCREATEUSER,b.BILLSTATUS,b.CCID,b.BDGYEAR,b.DEPDIV,b.BDGMANAGEDIV,                                                                                                                                                                 
b.SUPDEP,b.AGENCY,b.FUNDTYPE,b.EXPTYPE,b.EXPFUNC,b.EXPECO,                                                                                                                                                                              
b.expdetail,b.EXPFUNDTYPE,b.expendcomp,b.lastsource,b.INDSOURCE,b.BGTDOCNO,                                                                                                                                                             
b.bgtprojcode,b.DEPPROCODE,b.DEPPRONAME,b.BGTREMARK,b.SENDDATE,b.GRANTLEADER,                                                                                                                                                           
b.BDGALLOW,b.BGTVERSION,                                                                                                                                                                                                                
b.ORGINALAMT,                                                                                                                                                                                                                           
b.AMT,b.AMTLOCK,b.AMTUNLOCK,b.CONTROLTYPE,b.CONTROLAMT,b.GOVPROAMT,                                                                                                                                                                     
b.PLANGENTYPE,b.N1,b.N2,b.N3,b.N4,b.N5,b.E0,b.E1,                                                                                                                                                                                       
nvl(b.E2,0) E2,nvl(b.E3,0) E3,nvl(b.E4,0) e4,nvl(b.E5,0) e5,nvl(b.E6,0) e6,                                                                                                                                                             
nvl(b.E7,0) E7,nvl(b.E8,0) E8,nvl(b.e9,0) e9,                                                                                                                                                                                           
b.BILLLASTDATE,b.BILLLASTUSER,b.WFSTATE,                                                                                                                                                                                                
b.projlevel,                                                                                                                                                                                                                            
case when nvl(b.ccid,0)=0 then 0 else ( b.amtlock-nvl( v.cur4450,0)) end  as amtlockuse,                                                                                                                                                
case when nvl(b.ccid,0)=0 then 0 else b.govproamt-nvl(v.cur4460,0) end as govproamtuse,                                                                                                                                                 
case when nvl(b.ccid,0)=0 then 0 else b.controlamt-nvl( v.cur4470,0) end as controlamtuse,                                                                                                                                              
nvl(cur4501,0) as amtunlockuse,                                                                                                                                                                                                         
nvl(cur4501,0) as amtunlockpaytypeuse,                                                                                                                                                                                                  
b.sfaccount,                                                                                                                                                                                                                            
b.bgtprojid,b.uniquebfid,B.IMPORTBATCH,b.s3,b.s2,b.s1,b.s4,b.s5,b.s6,b.s7,b.s8,b.s9,B.CATALOGPROCODE,B.CATALOGPRONAME,B.CATALOGPROJID,                                                                                                  
b.dbyear,nvl(b.DEPEXPECO,0) DEPEXPECO                                                                                                                                                                                                   
from EP_T_BFBUDGET_ADJUSTREG b,                                                                                                                                                                                                         
(                                                                                                                                                                                                                                       
select dbyear,ccid,                                                                                                                                                                                                                     
sum(case when t.glsubjid=4450 then t.curbal else 0 end) cur4450,                                                                                                                                                                        
sum(case when t.glsubjid=4451 then t.curbal else 0 end) cur4451,                                                                                                                                                                        
sum(case when t.glsubjid=4451 and t.paytype=0 then t.curbal else 0 end) curpaytype4451,                                                                                                                                                 
sum(case when t.glsubjid=4501 then t.curbal else 0 end) cur4501,                                                                                                                                                                        
--sum(case when t.glsubjid=4451 and t.paytype in (5,7) then t.curbal else 0 end) curgovpaytype4451,                                                                                                                                     
sum(case when t.glsubjid=4460 then t.curbal else 0 end) cur4460,                                                                                                                                                                        
sum(case when t.glsubjid=4470 then t.curbal else 0 end) cur4470,                                                                                                                                                                        
sum(case when t.glsubjid=1101 then t.curbal else 0 end) cur1101                                                                                                                                                                         
from FP_T_GLCTRL02 t where t.glsubjid in(4450,4451,4460,4470,1101,4501) group by dbyear,ccid                                                                                                                                            
) v                                                                                                                                                                                                                                     
where b.billstatus>=0 and b.ccid=v.ccid(+) and b.dbyear=v.dbyear(+)                                                                                                                                                                     
;

6、查看锁表、解锁

--查询谁锁表                                                                                                                                                                                                                                 
SELECT /*+ rule */                                                                                                                                                                                                                      
s.username,                                                                                                                                                                                                                             
decode(l.type, 'TM', 'TABLE LOCK', 'TX', 'ROW LOCK', NULL) LOCK_LEVEL,                                                                                                                                                                  
o.owner,                                                                                                                                                                                                                                
o.object_name,                                                                                                                                                                                                                          
o.object_type,                                                                                                                                                                                                                          
s.sid,                                                                                                                                                                                                                                  
s.serial#,                                                                                                                                                                                                                              
s.terminal,                                                                                                                                                                                                                             
s.machine,                                                                                                                                                                                                                              
s.program,                                                                                                                                                                                                                              
s.osuser                                                                                                                                                                                                                                
FROM v$session s, v$lock l, dba_objects o                                                                                                                                                                                               
WHERE l.sid = s.sid                                                                                                                                                                                                                     
AND l.id1 = o.object_id(+)                                                                                                                                                                                                              
AND s.username is NOT NULL;                                                                                                                                                                                                             
                                                                                                                                                                                                                                        
--执行以下语句可查询被锁的session和serial#                                                                                                                                                                                                           
                                                                                                                                                                                                                                        
select b.username,b.sid,b.serial#,logon_time                                                                                                                                                                                            
                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                        
--删除锁住的session和serial#                                                                                                                                                                                                                  
                                                                                                                                                                                                                                        
alter system kill session '5732,8215';

7、查询时增加字段的语句

select t.*,'成博' name from FP2_T_PIFORM t;

8、创建GUID的函数

create or replace function getGUID return nvarchar2 is                                                                                                                                                                                  
Result nvarchar2(36);                                                                                                                                                                                                                   
guid   nvarchar2(36);                                                                                                                                                                                                                   
begin                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                        
select sys_guid() into guid from dual;                                                                                                                                                                                                  
Result := substr(guid, 1, 8) || '-' || substr(guid, 9, 4) || '-' ||                                                                                                                                                                     
substr(guid, 13, 4) || '-' || substr(guid, 17, 4) || '-' ||                                                                                                                                                                             
substr(guid, 21, 12);                                                                                                                                                                                                                   
return(Result);                                                                                                                                                                                                                         
end getGUID;

查询

select getguid() from dual;--AF27C1D8-27AB-4884-9A64-EA326221ABD9                                                                                                                                                                       
select getguid from dual;--AF27C1D8-27AB-4884-9A64-EA326221ABD9                                                                                                                                                                         
select sys_guid() from dual;--E5E1CDDA7D7E46EF9317DB5CC8486E72                                                                                                                                                                          
select get_uuid() from dual;--69f95a0b-8c9b-4152-9e58-978aa3c5d63d

uuid

create or replace function get_uuid return nvarchar2 is                                                                                                                                                                                 
Result nvarchar2(36);                                                                                                                                                                                                                   
guid   nvarchar2(36);                                                                                                                                                                                                                   
begin                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                        
select lower(sys_guid()) into guid from dual;                                                                                                                                                                                           
Result := substr(guid, 1, 8) || '-' || substr(guid, 9, 4) || '-' ||                                                                                                                                                                     
substr(guid, 13, 4) || '-' || substr(guid, 17, 4) || '-' ||                                                                                                                                                                             
substr(guid, 21, 12);                                                                                                                                                                                                                   
return(Result);                                                                                                                                                                                                                         
end get_uuid;

9、按照第2个字段降序查询

select * from fp2_t_vmdefine order by 2 desc;

10、where 1=1

//存在即合理                                                                                                                                                                                                                                 
String sql  =  select * from table1 where 1=1  ;                                                                                                                                                                                        
if(!a.equals("")){                                                                                                                                                                                                                      
sql=sql+"a='"+a+"'";                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                       
//拼条件时就不用判断是否需要添加where

11、查看该序列是否存在

select * from user_sequences where sequence_name='序列名' ---存在有结果,不存在结果为空

创建序列

-- Create sequence                                                                                                                                                                                                                      
create sequence EP_S_ACMAINBANK                                                                                                                                                                                                         
minvalue 1                                                                                                                                                                                                                              
maxvalue 999999999999999999                                                                                                                                                                                                             
start with 261                                                                                                                                                                                                                          
increment by 1                                                                                                                                                                                                                          
cache 20;

拓展

select * from user_sequences                                                                                                                                                                                                            
select * from user_tables                                                                                                                                                                                                               
select * from user_tablespaces                                                                                                                                                                                                          
select * from user_triggers                                                                                                                                                                                                             
select * from user_procedures                                                                                                                                                                                                           
select * from user_Views                                                                                                                                                                                                                
select * from user_indexes                                                                                                                                                                                                              
select * from all_source where type='PROCEDURE'                                                                                                                                                                                         
select * from all_tables where owner='SCOTT';--SCOTT为用户名,用户名必须是大写

https://www.cnblogs.com/wsy0202/p/12496708.html

12、创建和已有视图一样字段的表

CREATE TABLE EP_T_VESTAMPPOSITION AS SELECT * FROM EP_V_VESTAMPPOSITION

13、删表

drop table 表名称  --TRUNCATE 和DELETE只删除数据, DROP则删除整个表(结构和数据)。                                                                                                                                                                             
truncate table 表名称--直接删表中所有数据,日志表也删,无法恢复                                                                                                                                                                                                
delete from 表名称 where 列名称 = 值 --一条一条记录删,日志表不删,可以恢复

(1)DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。

TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。

(2)TRUNCATE 只能对TABLE;     DELETE可以是table和view

14、从虚表中查询下一个序列的值

select EP_S_ACBANKACCOUNT.nextval from dual;                                                                                                                                                                                            
select EP_S_ACBANKACCOUNT.currval from dual;--当前值

15、创建一个视图

--2、配置视图                                                                                                                                                                                                                                
create or replace view FP_V_PUBSTATMPLOCATION as                                                                                                                                                                                        
select 1 ITEMID,'01' ITEMCODE,'服务端' ITEMNAME,(select sys_guid() from dual) GUID,'11111111111111111111111111111111111111111111111111' USESCOPE,'2016-01-01' STARTDATE,'9998-12-31' ENDDATE,0 SUPERITEMID,1 LEVELNO,0 ISLEAF,1 USESTATE  f
union all                                                                                                                                                                                                                               
select  2,  '02',  'UKEY',(select sys_guid() from dual) GUID, '11111111111111111111111111111111111111111111111111' USESCOPE,'2016-01-01' STARTDATE,'9998-12-31' ENDDATE,0 SUPERITEMID,1 LEVELNO,0 ISLEAF,1 USESTATE from dual;

16、用SQL语句查询数据库中带有某个字段的所有表名

SELECT column_name,table_name FROM user_tab_columns WHERE column_name='VOUCHERTYPEID';                                                                                                                                                  
SELECT column_name,table_name FROM user_tab_columns WHERE column_name LIKE '%VOUCHERTYPEID%';

17、decode函数

1.decode函数的两种形式

第一种形式
decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)
select decode(1+99,2,'及格',1,'不及格','零分') from dual;--如果条件值1+99=2,查询结果为及格--如果条件值1+99=1,查询结果为不及格,否则为零分
第二种形式
decode(字段或字段的运算,值1,值2,值3)

这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3

18、单行行数之rpad()函数

填充函数

rpad函数从右边对字符串使用指定的字符进行填充
rpad(string,padded_length,[pad_string])
string 表示:被填充的字符串
padded_length 表示:字符的长度,是返回的字符串的数量,如果这个数量比原字符串的长度要短,rpad函数将会把字符串截取成从左到右的n个字符;
pad_string 是个可选参数,这个字符串是要粘贴到string的右边,如果这个参数未写,lpad函数将会在string的右边粘贴空格。
例如:

select  rpad('tech', 7)from dual;-- 将返回'tech '                                                                                                                                                                                          
select  rpad('tech', 2)from dual;-- 将返回'te'                                                                                                                                                                                             
select  rpad('tech', 8, '0')from dual;-- 将返回'tech0000'                                                                                                                                                                                  
select  rpad('tech on the net', 15, 'z')from dual;-- 将返回 'tech on the net'                                                                                                                                                              
select  rpad('tech on the net', 16, 'z')from dual;-- 将返回 'tech on the netz'
select rpad('aa',5)||decode('bbb',null,' ',rpad('bbb',8))||rpad('cccc',12) from dual;                                                                                                                                                   
--decode()函数如果bbb为null则输出' ',否则输出rpad('bbb',8)                                                                                                                                                                                          
---查询结果 aa   bbb     cccc

19、查询表中有多少行记录

select count(*) from SOCSECURITYFUND_PAYEEACCT

20、如果存在表就删除不存在就创建

declare                                                                                                                                                                                                                                 
i integer;                                                                                                                                                                                                                              
begin                                                                                                                                                                                                                                   
select count(*) into i from user_tables where table_name = 'DEPT1';                                                                                                                                                                     
if i > 0 then                                                                                                                                                                                                                           
dbms_output.put_line('该表已存在!');                                                                                                                                                                                                         
execute immediate 'DROP TABLE DEPT1';                                                                                                                                                                                                   
else                                                                                                                                                                                                                                    
dbms_output.put_line('该表不存在');                                                                                                                                                                                                          
end if;                                                                                                                                                                                                                                 
execute immediate 'create table DEPT1(deptno NUMBER(2),dname  VARCHAR2(14),loc    VARCHAR2(13))';                                                                                                                                       
end;

21、查询当前用户名下的表的信息

select * from user_tables                                                                                                                                                                                                               
--dba_tables : 系统里所有的表的信息,需要DBA权限才能查询                                                                                                                                                                                                   
--all_tables : 当前用户有权限的表的信息(只要对某个表有任何权限,即可在此视图中看到表的相关信息)                                                                                                                                                                                
--user_tables: 当前用户名下的表的信息

22、批量修改sql代码块

DECLARE                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
BEGIN                                                                                                                                                                                                                                   
--批量修改srcbillcode                                                                                                                                                                                                                       
FOR d IN (SELECT a.billseq from pay_lqd_cert_zkj a)                                                                                                                                                                                     
LOOP                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                        
EXECUTE IMMEDIATE 'update pay_lqd_cert t set t.srcbillcode = (SELECT b.srcbillcode FROM pay_lqd_cert_zkj b WHERE  b.billseq =  '''||d.billseq||''') WHERE t.billseq = '''||d.billseq||'''';                                             
COMMIT;                                                                                                                                                                                                                                 
END LOOP;                                                                                                                                                                                                                               
END;

23、时间格式修改

--先修改时间格式,在转换成字符串格式                                                                                                                                                                                                                     
update PAY_LQD_QUOTA_BILL t set t.billlastdate=                                                                                                                                                                                         
to_char(to_date(t.billlastdate, 'yyyymmddhh24miss'),                                                                                                                                                                                    
'yyyy-mm-dd hh24:mi:ss')                                                                                                                                                                                                                
where length(t.billlastdate)=14;

24、SUM( )函数

SUM用法:使用sum聚合函数配合case when xx then x else xx end)进行统计当满足条件时的和。字面理解就是:sum(1)的意思就是加1个,sum(0)的意思加0个。

--可配合case when 使用,如果为14就加10,否则加0                                                                                                                                                                                                        
select sum(case when length(t.billlastdate)=14 then '10' else '0' end) from PAY_LQD_QUOTA_BILL t;
--配合group by使用                                                                                                                                                                                                                          
select t.deptno,sum(t.sal) from emp t group by t.deptno;--按部门分组需支出的薪资为

25、存储过程(集合、for循环)

CREATE OR REPLACE PROCEDURE epru_transfer_itemofex_tj(p_billid    IN VARCHAR2, --单据流水号                                                                                                                                                  
p_voucherid in VARCHAR2,                                                                                                                                                                                                                
issucc      OUT NUMBER, --返回1则说明规则通过,其他不成功                                                                                                                                                                                              
message     OUT VARCHAR2 --返回执行信息                                                                                                                                                                                                       
) AS                                                                                                                                                                                                                                    
--===================================================================================================================                                                                                                                   
--======================================================项目支出资金列支人员经费=====================================================                                                                                                               
--===================================================================================================================                                                                                                                   
--规则说明:预算单位通过零余额账户用项目支出资金列支人员经费时,监控系统自动预警冻结                                                                                                                                                                                             
depbgtecoid number(9);                                                                                                                                                                                                                  
procatid  number(9);                                                                                                                                                                                                                    
USE_DES VARCHAR2(100);                                                                                                                                                                                                                  
depbgtecobgtid number(9);                                                                                                                                                                                                               
                                                                                                                                                                                                                                        
BEGIN                                                                                                                                                                                                                                   
depbgtecoid := 0;                                                                                                                                                                                                                       
procatid := 0;                                                                                                                                                                                                                          
USE_DES := '';                                                                                                                                                                                                                          
depbgtecobgtid := 0;                                                                                                                                                                                                                    
issucc        := 1;                                                                                                                                                                                                                     
message       := '';                                                                                                                                                                                                                    
                                                                                                                                                                                                                                        
IF p_voucherid = '1084' then--1层                                                                                                                                                                                                        
select t.USE_DES into USE_DES from pay_voucher t where t.billid = p_billid and t.is_deleted !=1 group by t.USE_DES;                                                                                                                     
DECLARE                                                                                                                                                                                                                                 
CURSOR c_mainvoucher1 IS                                                                                                                                                                                                                
select v.itemcode depbgtecoid from pay_voucher t join FP_T_CTEXPECO v on t.dep_bgt_eco_id = v.itemid where t.billid = p_billid  and t.is_deleted !=1 group by v.itemcode;                                                               
CURSOR c_mainvoucher2 IS                                                                                                                                                                                                                
select v.itemcode  procatid from pay_voucher t join FP_T_CTPROJEXPENDTYPE v on t.pro_cat_id = v.itemid where t.billid = p_billid and t.is_deleted !=1 group by v.itemcode;                                                              
CURSOR c_mainvoucher3 IS                                                                                                                                                                                                                
select v.itemcode depbgtecobgtid from pay_voucher t join FP_T_CTEXPECO v on t.dep_bgt_ecobgt_id = v.itemid where t.billid = p_billid and t.is_deleted !=1 group by v.itemcode;                                                          
begin                                                                                                                                                                                                                                   
FOR c_row1 IN c_mainvoucher1 LOOP--for                                                                                                                                                                                                  
IF (c_row1.depbgtecoid not in(30101,30102,30103,30106,30107,30108,30109,30199,30226,30229,30301,30302,30303,30304,30305,30306,30307,30308,30309,30310,30311,30399)) THEN--2层                                                            
FOR c_row2 IN c_mainvoucher2 LOOP--for                                                                                                                                                                                                  
if(c_row2.procatid in(3,22)) THEN                                                                                                                                                                                                       
FOR c_row3 IN c_mainvoucher3 LOOP--for                                                                                                                                                                                                  
if(USE_DES not in('抚恤金','劳务','工资','社会保险缴费','公积金','薪资','奖金') and c_row3.depbgtecobgtid not in(30101,30102,30103,30106,30107,30108,30109,30199,30226,30229,30301,30302,30303,30304,30305,30306,30307,30308,30309,30310,30311,30399)) THEN 
issucc  := 0;                                                                                                                                                                                                                           
message := '预算单位通过零余额账户用项目支出资金列支人员经费';                                                                                                                                                                                                  
end if;                                                                                                                                                                                                                                 
END LOOP;                                                                                                                                                                                                                               
end if;                                                                                                                                                                                                                                 
END LOOP;                                                                                                                                                                                                                               
RETURN;                                                                                                                                                                                                                                 
END IF; --2层                                                                                                                                                                                                                            
END LOOP; --for                                                                                                                                                                                                                         
end;                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                        
END IF; --1层                                                                                                                                                                                                                            
                                                                                                                                                                                                                                        
EXCEPTION                                                                                                                                                                                                                               
WHEN OTHERS THEN                                                                                                                                                                                                                        
raise_application_error(-20005,                                                                                                                                                                                                         
'项目支出资金列支人员经费出现异常。' || SQLERRM);                                                                                                                                                                                                        
END;

26、代码块,对表进行加列

declare vCnt int;                                                                                                                                                                                                                       
begin                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                        
select count(*) into vCnt                                                                                                                                                                                                               
from  user_tab_cols where   table_name='PAY_FUND_REFUND' and column_name='XPAY_DATETIME' ;                                                                                                                                              
                                                                                                                                                                                                                                        
if vCnt=0 then                                                                                                                                                                                                                          
execute immediate ' alter table PAY_FUND_REFUND add XPAY_DATETIME varchar2(20)';                                                                                                                                                        
end if;                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
end;

27、创建包及包体

包(pay_gx_pkgsyscomm.spc)

create or replace package pay_gx_pkgsyscomm is                                                                                                                                                                                          
                                                                                                                                                                                                                                        
TYPE T_CURSOR IS REF CURSOR;                                                                                                                                                                                                            
                                                                                                                                                                                                                                        
function set_param_startdate(str varchar2) return varchar2;                                                                                                                                                                             
function get_param_startdate return varchar2;                                                                                                                                                                                           
                                                                                                                                                                                                                                        
function set_param_enddate(str varchar2) return varchar2;                                                                                                                                                                               
function get_param_enddate return varchar2;                                                                                                                                                                                             
                                                                                                                                                                                                                                        
end pay_gx_pkgsyscomm;

包体(pay_gx_pkgsyscomm.bdy)

create or replace package body pay_gx_pkgsyscomm is                                                                                                                                                                                     
str1 varchar2(4000);                                                                                                                                                                                                                    
function set_param_startdate(str varchar2) return varchar2 is                                                                                                                                                                           
begin                                                                                                                                                                                                                                   
str1 :=str;                                                                                                                                                                                                                             
return str;                                                                                                                                                                                                                             
end;                                                                                                                                                                                                                                    
function get_param_startdate return varchar2 is                                                                                                                                                                                         
begin                                                                                                                                                                                                                                   
return str1;                                                                                                                                                                                                                            
end;                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                        
function set_param_enddate(str varchar2) return varchar2  is                                                                                                                                                                            
begin                                                                                                                                                                                                                                   
str1 :=str;                                                                                                                                                                                                                             
return str;                                                                                                                                                                                                                             
end;                                                                                                                                                                                                                                    
function get_param_enddate return varchar2 is                                                                                                                                                                                           
begin                                                                                                                                                                                                                                   
return str1;                                                                                                                                                                                                                            
end;                                                                                                                                                                                                                                    
end pay_gx_pkgsyscomm;

28、获取系统当前月份的最后一天

select to_char(last_day(sysdate),'YYYYMMdd') from dual;--获取系统当前月份的最后一天                                                                                                                                                                  
select to_char(TRUNC(SYSDATE-1),'yyyymmdd') from dual;--获取系统当天的前一天

扩展

/*加法 */                                                                                                                                                                                                                                 
select sysdate,add_months(sysdate,12) from dual; --加1年                                                                                                                                                                                  
select sysdate,add_months(sysdate,1) from dual; --加1月                                                                                                                                                                                   
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期                                                                                                                                                             
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天                                                                                                                                                              
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时                                                                                                                                                          
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟                                                                                                                                                       
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒                                                                                                                                                     
                                                                                                                                                                                                                                        
/*减法 */                                                                                                                                                                                                                                 
select sysdate,add_months(sysdate,-12) from dual; --减1年                                                                                                                                                                                 
select sysdate,add_months(sysdate,-1) from dual; --减1月                                                                                                                                                                                  
select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; --减1星期                                                                                                                                                             
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; --减1天                                                                                                                                                              
select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --减1小时                                                                                                                                                          
select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1分钟                                                                                                                                                       
select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒                                                                                                                                                     
                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                        
/*时间函数(SYSDATE)简析 */                                                                                                                                                                                                                    
select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;--获取年月日 周 时分秒                                                                                                                                                                 
select to_char(sysdate,'W') from dual;--取得当前日期是本月的第几周                                                                                                                                                                                   
select sysdate,to_char(sysdate,'D') from dual;--取得当前日期是一个星期中的第几天,注意星期日是第一天                                                                                                                                                              
select to_char(sysdate,'yyyy') from dual; --年                                                                                                                                                                                           
select to_char(sysdate,'Q')from dual; --季                                                                                                                                                                                               
select to_char(sysdate,'mm') from dual; --月                                                                                                                                                                                             
select to_char(sysdate,'dd') from dual; --日                                                                                                                                                                                             
select to_char(sysdate,'day') from dual;--取当前日期是星期几中文显示                                                                                                                                                                                 
select sysdate from dual;--得到当前的日期                                                                                                                                                                                                      
select trunc(sysdate) from dual;--得到当天凌晨0点0分0秒的日期                                                                                                                                                                                       
select trunc(sysdate) + 0.99999 from dual;---- 得到这天的最后一秒                                                                                                                                                                                
select trunc(sysdate) + 1/24 from dual;-- 得到当天小时的具体数值                                                                                                                                                                                   
select trunc(sysdate) + 7/24 from dual;                                                                                                                                                                                                 
select trunc(sysdate+1) from dual;--得到明天凌晨0点0分0秒的日期                                                                                                                                                                                     
select trunc(sysdate)+1 from dual;                                                                                                                                                                                                      
select trunc(sysdate,'mm') from dual;--本月一日的日期                                                                                                                                                                                          
select trunc(add_months(sysdate,1),'mm') from dual;--得到下月一日的日期                                                                                                                                                                          
select last_day(sysdate) from dual;--返回当前月的最后一天?                                                                                                                                                                                        
select last_day(trunc(sysdate)) from dual;                                                                                                                                                                                              
select trunc(last_day(sysdate)) from dual;                                                                                                                                                                                              
select trunc(add_months(sysdate,1),'mm') - 1 from dual;                                                                                                                                                                                 
select trunc(sysdate,'yyyy')+ rn -1 date0 from (select rownum rn from all_objects where rownum<366);--得到一年的每一天                                                                                                                          
SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;--今天是今年的第N天                                                                                                                                                                                     
select add_months(sysdate,24) from dual;--如何在给现有的日期加上2年                                                                                                                                                                                 
select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual;--判断某一日子所在年分是否为润年                                                                                                                                 
select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','闰年','平年') from dual;                                                                                                                                   
select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;--得到日期的季度                                                                                                                                                                     
select to_char(sysdate, 'Q') from dual;

29、去空格操作

select trim(' e h t ') from dual;--e h t                                                                                                                                                                                                
select replace(' e h t ',' ') from dual;--eht                                                                                                                                                                                           
--trim只去前后空格,replace去所有空格

30、正数返回1,负数返回-1,零返回0

select  sign(-5643) from dual;--结果-1                                                                                                                                                                                                    
select  sign(0) from dual; --结果0                                                                                                                                                                                                        
select  sign(6744) from dual;--结果1

31、截取字符串

SELECT SUBSTR('2021-04-09 09:37:21!', 0,10) FROM dual     --从0开始截10位,返回2021-04-09

拓展

SELECT SUBSTR('Hello SQL!', 1) FROM dual;     --截取所有字符串,返回'Hello SQL!'                                                                                                                                                                  
SELECT SUBSTR('Hello SQL!', 2) FROM dual;     --从第2个字符开始,截取到末尾。返回'ello SQL!'                                                                                                                                                            
SELECT SUBSTR('Hello SQL!', -4) FROM dual;    --从倒数第4个字符开始,截取到末尾。返回'SQL!'                                                                                                                                                               
SELECT SUBSTR('Hello SQL!', 3, 6) FROM dual;  --从第3个字符开始,截取6个字符。返回'llo SQ'                                                                                                                                                              
SELECT SUBSTR('Hello SQL!', -4, 3) FROM dual; --从倒数第4个字符开始,截取3个字符。返回'SQL'

拓展INSTR()获取字符位置

长度之前的字符跳过

正向直接数位置,字符所在位置超出后为0
反向数字符个数,正向数位置

--表示从源字符串'city_company_staff'中第1个字符开始查找子字符串'_'第1次出现的位置                                                                                                                                                                                  
SELECT INSTR('city_company_staff', '_') FROM dual    --返回5                                                                                                                                                                              
                                                                                                                                                                                                                                        
--表示从源字符串'city_company_staff'中第5个字符开始查找子字符串'_'第1次出现的位置                                                                                                                                                                                  
SELECT INSTR('city_company_staff', '_', 5) FROM dual    --返回5                                                                                                                                                                           
                                                                                                                                                                                                                                        
--表示从源字符串'city_company_staff'中第5个字符开始查找子字符串'_'第1次出现的位置                                                                                                                                                                                  
SELECT INSTR('city_company_staff', '_', 5, 1) FROM dual    --返回5                                                                                                                                                                        
                                                                                                                                                                                                                                        
--表示从源字符串'city_company_staff'中第3个字符开始查找子字符串'_'第2次出现的位置                                                                                                                                                                                  
SELECT INSTR('city_company_staff', '_', 3, 2) FROM dual    --返回13                                                                                                                                                                       
                                                                                                                                                                                                                                        
--start参数为-1,从右向左检索,查找'_'字符串在源字符串中第1次出现的位置                                                                                                                                                                                              
SELECT INSTR('city_company_staff', '_', -1, 1) FROM dual    --返回13                                                                                                                                                                      
                                                                                                                                                                                                                                        
--start参数为-6,从右向左检索,查找'_'字符串在源字符串中第2次出现的位置                                                                                                                                                                                              
SELECT INSTR('city_company_staff', '_', -6, 2) FROM dual    --返回5

32、字符串拼接

select '%'||'Task'||'%' from dual;--%Task%                                                                                                                                                                                              
select CONCAT('%', 'Task') from dual;--%Task                                                                                                                                                                                            
select CONCAT(CONCAT('%', 'Task'),'%') from dual;--%Task%

33、union和union all

union会自动去重,union all不去重

34、类似IF判断

表达式等价
DECODE(E1, E2, E3, E4)IF E1 = E2 THEN E3 ELSE E4
NULLIF(E1, E2)IF E1 = E2 THEN NULL ELSE E1
NVL(E1, E2)IF E1 IS NULL THEN E2 ELSE E1
NVL2(E1, E2, E3)IF E1 IS NULL THEN E3 ELSE E2
扩展
decode(e,e1,v1,e2,v2,…en,vn,a)if e=e1 then v1,if e=e2 then v2,if e=en then vn else a

100、数据库的对象

--表                 Table                                                                                                                                                                                                               
--视图                View                                                                                                                                                                                                                
--序列                Sequence                                                                                                                                                                                                            
--触发器              Trigger                                                                                                                                                                                                              
--函数               Function                                                                                                                                                                                                             
--存储过程            Procedure