标签:job order media serve update tput 新建 other 说明
1、常用循环(for循环)
1 CREATE OR REPLACE PROCEDURE proc_testfor 2 AS 3 V_empno NUMBER; 4 BEGIN 5 FOR curr_row IN 6 ( 7 SELECT * FROM EMP a LEFT JOIN DEPT ON DEPT.DEPTNO = a.DEPTNO WHERE 1=1 8 ) 9 LOOP 10 V_empno:=curr_row.empno; 11 IF V_empno>=7700 THEN 12 dbms_output.PUT_LINE(curr_row.empno||‘,‘||curr_row.ENAME); 13 END IF; 14 --dbms_output.PUT_LINE(curr_row.empno); 15 END LOOP; 16 EXCEPTION 17 WHEN OTHERS THEN ROLLBACK; 18 END proc_testfor;
2、常用关联查询(LEFT JOIN 、INNER JOIN、RIGHT JOIN)
1 SELECT e.* ,dept.* 2 FROM EMP e RIGHT JOIN DEPT ON DEPT.DEPTNO = e.DEPTNO; 3 4 SELECT e.* ,dept.* 5 FROM EMP e LEFT JOIN DEPT ON DEPT.DEPTNO = e.DEPTNO; 6 7 8 SELECT e.* ,dept.* 9 FROM EMP e INNER JOIN DEPT ON DEPT.DEPTNO = e.DEPTNO;
3、常用伪列函数用来分页,分组排序处理特殊数据(ROWNUM、ROWID、row_number() OVER (PARTITION BY e.DEPTNO ORDER BY e.EMPNO) rn)
row_number() OVER (PARTITION BY e.DEPTNO ORDER BY e.EMPNO) rn 说明:e.DEPTNO是分组列,e.EMPNO 排序
SELECT ROWNUM,ROWID,row_number() OVER (PARTITION BY e.DEPTNO ORDER BY e.EMPNO) rn,e.* FROM EMP e;
4、多表更新MERGE INTO
1 MERGE INTO BS_BRAND B 2 USING (SELECT A.* FROM ( 3 SELECT T.*,ROW_NUMBER() OVER(PARTITION BY T.BRANDNAME ORDER BY T.BRANDID DESC) RN 4 FROM VIEW_BRANDINFO_MODIAFY_TEMP T ) A WHERE a.rn=1) L 5 ON ( UPPER(REPLACE(B.BRANDNAME,‘ ‘,‘‘))=UPPER(REPLACE(L.BRANDNAME,‘ ‘,‘‘))) 6 WHEN MATCHED THEN 7 UPDATE SET B.FORMATNAME=L.FORMATNAME,B.MAINFORMATNAME=L.MAINFORMATNAME,B.DETAILFORMATNAME=L.DETAILFORMATNAME, 8 B.BOTTOMFORMATNAME=L.BOTTOMFORMATNAME,B.FORMATID=L.FORMATID,B.MAINFORMATID=L.MAINFORMATID,B.DETAILFORMATID=L.DETAILFORMATID, 9 B.BOTTOMFORMATID=L.BOTTOMFORMATID,b.SECONDRECORDSTATUS=‘HISTORY‘ 10 WHERE B.BRANDID IN (SELECT A.BRANDID FROM ( 11 SELECT T.*,ROW_NUMBER() OVER(PARTITION BY T.BRANDNAME ORDER BY T.BRANDID DESC) RN 12 FROM VIEW_BRANDINFO_MODIAFY_TEMP T ) A WHERE a.rn>1)
1 UPDATE POS_NEW_CONTRACT SET ISDELETE=1 where (CHANGECONTRACTID) in 2 (select CHANGECONTRACTID from POS_NEW_CONTRACT group by CHANGECONTRACTID having count(*) > 1) 3 and rowid not in (select min(rowid) from POS_NEW_CONTRACT group by CHANGECONTRACTID having count(*)>1) 4 AND ISDELETE=0; 5 6 --没有主键根据rowid更新
5、decode()、case ..when、nvl()、nvl2()函数
1 SELECT e.EMPNO,e.ENAME,SAL,nvl(comm,0),nvl2(comm,1600,0), 2 decode(e.DEPTNO,20,‘销售部‘,30,‘市场部‘,‘其它部门‘) AS decodeDept, 3 CASe WHEN e.DEPTNO=20 THEN ‘销售部‘ 4 WHEN e.DEPTNO=30 THEN ‘市场部‘ ELSE ‘其它部门‘ 5 END AS caseDept FROM EMP e; 6 7 --Oracle在nvl()函数的功能上扩展,提供了nvl2()函数 nvl2()(E1, E2, E3)的功能为: 8 --如果E1为NULL,则函数返回E3,若E1不为null,则返回E2 9 10 --DECODE 与CASE WHEN 的比较 11 -- 1.DECODE 只有Oracle 才有,其它数据库不支持; 12 -- 2.CASE WHEN的用法, Oracle、SQL Server、 MySQL 都支持; 13 -- 3.DECODE 只能用做相等判断,但是可以配合sign函数进行大于,小于,等于的判断, 14 -----CASE when可用于=,>=,<,<=,<>,is null,is not null 等的判断; 15 -- 4.DECODE 使用其来比较简洁,CASE 虽然复杂但更为灵活; 16 -- 5.另外,在decode中,null和null是相等的,但在case when中,只能用is null来判断
6、时间函数处理
1 一)日期函数[重点掌握前四个日期函数] 2 1,add_months[返回日期加(减)指定月份后(前)的日期] 3 4 select sysdate S1,add_months(sysdate,10) S2, 5 add_months(sysdate,-5) S3 from dual; 6 7 2,last_day [返回该月最后一天的日期] 8 select last_day(sysdate) from dual; 9 10 3,months_between[返回日期之间的月份数] 11 select sysdate S1, months_between(‘1-4月-04‘,sysdate) S2, 12 months_between(‘1-4月-04‘,‘1-2月-04‘) S3 from dual 13 14 4,next_day(d,day): 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日 15 select sysdate S1,next_day(sysdate,1) S2, 16 next_day(sysdate,‘星期日‘) S3 FROM DUAL 17 18 5,round[舍入到最接近的日期](day:舍入到最接近的星期日) 19 select sysdate S1, 20 round(sysdate) S2 , 21 round(sysdate,‘year‘) YEAR, 22 round(sysdate,‘month‘) MONTH , 23 round(sysdate,‘day‘) DAY from dual 24 25 6,trunc[截断到最接近的日期] 26 select sysdate S1, 27 trunc(sysdate) S2, 28 trunc(sysdate,‘year‘) YEAR, 29 trunc(sysdate,‘month‘) MONTH , 30 trunc(sysdate,‘day‘) DAY from dual 31 32 7,返回日期列表中最晚日期 33 select greatest(‘01-1月-04‘,‘04-1月-04‘,‘10-2月-04‘) from DUAL 34 35 8、to_char()[将日期和数字类型转换成字符类型] 36 select to_char(sysdate) s1, 37 to_char(sysdate,‘yyyy-mm-dd‘) s2, 38 to_char(sysdate,‘yyyy‘) s3, 39 to_char(sysdate,‘yyyy-mm-dd hh12:mi:ss‘) s4, 40 to_char(sysdate, ‘hh24:mi:ss‘) s5, 41 to_char(sysdate,‘DAY‘) s6 from dual; 42 9、to_date() ‘2018-09-23 11:00:41‘ 43 44 SELECT to_date(‘2018-09-23 11:00:41‘,‘yyyy-mm-ddhh24:mi:ss‘) s1, 45 to_date(‘2018-09-23‘,‘yyyy-mm-ddhh24:mi:ss‘) s2 46 FROM DUAL ;
7、字符串相关函数
字符函数:
initcap(st) 返回st将每个单词的首字母大写,所有其他字母小写
lower(st) 返回st将每个单词的字母全部小写
upper(st) 返回st将每个单词的字母全部大写
concat(st1,st2) 返回st为st2接st1的末尾(可用操作符"||")
--concat 只能连接两个字符串,|| 可以连接多个
--https://www.cnblogs.com/zuizui1204/p/6111640.html
lpad(st1,n[,st2]) 返回右对齐的st,st为在st1的左边用st2填充直至长度为n,st2的缺省为空格
rpad(st1,n[,st2]) 返回左对齐的st,st为在st1的右边用st2填充直至长度为n,st2的缺省为空格
ltrim(st[,set]) 返回st,st为从左边删除set中字符直到第一个不是set中的字符。缺省时,指的是空格
rtrim(st[,set]) 返回st,st为从右边删除set中字符直到第一个不是set中的字符。缺省时,指的是空格
replace(st,search_st[,replace_st]) 将每次在st中出现的search_st用replace_st替换,返回一个st。缺省时,删除search_st
substr(st,m[,n]) n=返回st串的子串,从m位置开始,取n个字符长。缺省时,一直返回到st末端
length(st) 数值,返回st中的字符数
instr(st1,st2[,m[,n]]) 数值,返回st1从第m字符开始,st2第n次出现的位置,m及n的缺省值为1
8、表结构增列、修改列、删除列语句
1 create table test as select * from dept; --从已知表复制数据和结构 2 create table test as select * from dept where 1=2; --从已知表复制结构但不包括数据 3 4 --新增一列 5 ALTER TABLE TEST ADD rn NUMBER; 6 --修改列: 7 alter table TEST rename column rn to rn1; 8 --修改列的类型: 9 alter table TEST modify rn1 varchar(200); 10 --删除数据库一列 11 alter table TEST drop column rn1; 12 13 ----建表语句判断 14 declare tableExist number; 15 begin 16 select count(1) into tableExist from user_tables where table_name=upper(‘TEST‘) ; 17 if tableExist = 0 then 18 execute immediate 19 ‘CREATE TABLE TEST( 20 BunkID NUMBER , 21 PlazaID NUMBER , 22 CHANGECONTRACTID NUMBER , 23 FloorID NUMBER , 24 ISDELETE NUMBER , 25 BunkCode VARCHAR2(100) , 26 BAIHUOFLAG VARCHAR2(10) , 27 ModifyDate DATE, 28 LastUpDATE DATE )‘; 29 end if; 30 end;
9、新建job
1 DECLARE 2 3 MONTH_BUNKREPORTJOB NUMBER; 4 5 BEGIN 6 7 DBMS_JOB.SUBMIT(JOB => MONTH_BUNKREPORTJOB, 8 9 WHAT => ‘PRO_ZCZL_TO_POS_BUNKDATA;‘, --执行的存储过程的名字 10 11 NEXT_DATE =>to_date(‘2018-09-16 23:30:00‘,‘yyyy-mm-dd hh24:mi:ss‘), 12 13 INTERVAL => ‘SYSDATE+1‘); --每天运行PRO_ZCZL_TO_POS_BUNKDATAJOB一次 14 15 COMMIT; 16 17 END;
标签:job order media serve update tput 新建 other 说明
原文地址:https://www.cnblogs.com/gyjjyg/p/9692144.html