Data Warehouse: Oracle Application Notes
stored procedure
grammar
CREATE OR REPLACE PROCEDURE 名称(
参数 in/out 类型,
datadate in varchar2,
name out varchar2)
AS
--as后接定义或赋值语句
data := datadate;
dt varchar2(8);
da_t date;
BEGIN
--begin后接函数主体
merge into xxx
using(select xx from xx left join on xx where xx)
on()
when matched then update set
xx = xx
when not matched then insert(
x.xx,x.xxx)values(
xxx,xxx
);
commit;
*****记录日志*****
dbms_output.put_line('SQLCODE='||SQLCODE||'%SQLERRM'||SQLERRM||'%');
****异常处理****
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('SQLCODE='||SQLCODE||'%SQLERRM'||SQLERRM||'%');
rollback:
raise:
END
merge into
merge into table_name a1 --目标表
using (table|view|select ) a2 --数据源表 可表、视图、子查询
on () --关联条件
when matched then --关联成立
--更新
update table_name set xxx=xxx where xx
--删除
delete from table_name where xxx=xxx where xx
--更新删除同存时,删除的条件要在更新条件内
when not matched then --关联不成立
--插入
insert (xxx) values (xxx) where xx
when not matched by source then --当源表不存在,目标表存在
--删除
delete;
dual table
--查看当前连接用户
select user from dual;
--查看当前日期、时间
select sysdate from dual;
select to_char(sysdate,''yyyy-mm-dd hh24:mi:ss'') from dual;
--当作计算器用
select 1+2 from dual;
--测试函数
select nvl(1,1,0) from dual;
dblink
create
CREATE DATABASE LINK 数据库链接名
CONNECT TO 用户名
IDENTIFIED BY 密码
USING '’数据库连接字符串';
CREATE PUBLIC DATABASE LINK LINK_SDATA
CONNECT TO query
IDENTIFIED BY 123456
USING '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.188.245.201)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = hsmis)
))';
transfer
--后加@连接名
select * from xx.xxx@LINK_SDATA
DDL statement query
--查询dblink
select DBMS_METADATA.GET_DDL('DB_LINK','LINK_SDATA ') FROM DUAL;
--查询建表语句
select DBMS_METADATA.GET_DDL('TABLE','TableName') FROM DUAL;
make numbers
TRUNCATE TABLE LS_CRM.T_PC_CUST_BAK;
INSERT INTO LS_CRM.T_PC_CUST_BAK SELECT * FROM LS_CRM.T_PC_CUST WHERE CUST_ID = '2020202020';
UPDATE LS_CRM.T_PC_CUST SET XXX = 'XXX', XXX = 'XXX' WHERE CUST_ID = '';
some functions
The sys_guid()
system randomly generates the primary key function
NVL
nvl(colmn,a)
returns the column value if the field colmn is not empty, and returns a if it is empty
nvl2(colmn,a,b)
returns a if the field colmn is not empty, and returns b if it is empty
drop table XXX purge
TRUNK
The TRUNC function can be used to intercept dates or numbers. Its usage depends on the type of argument passed in.
When the input parameter is a date, the TRUNC function can intercept the input date according to the specified format. The syntax format is: TRUNC(date [,fmt]), where date is a date value and fmt is the date format. The date will be truncated by the specified date format; omit it to truncate by the most recent date1.
SELECT TRUNC(SYSDATE) FROM DUAL; --返回当前时间
SELECT TRUNC(SYSDATE,'YY') FROM DUAL; --返回当年第一天
SELECT TRUNC(SYSDATE,'MM') FROM DUAL; --返回当月的第一天
SELECT TRUNC(SYSDATE,'D') FROM DUAL; --返回当前星期的第一天,即星期天
When the incoming parameter is a number, the TRUNC function can intercept the number. The syntax format is: TRUNC(number [,decimals]), where number is the value to be truncated, decimals indicates the number of digits after the decimal point to be retained, optional, if it is ignored, all decimal parts will be truncated. Note: Data are not rounded1 when truncated.
SELECT TRUNC(123.567,2) FROM DUAL; --123.56,将小数点右边指定位数后面的截去;
SELECT TRUNC(123.567,-2) FROM DUAL; --100,第二个参数可以为负数,表示将小数点左边指定位数后面的部分截去,即均以0记;
SELECT TRUNC(123.567) FROM DUAL; --123,默认截去小数点后面的部分;
random number
DBMS_RANDOM.VALUE*1OOOO
--10000以内随机数
trunc(DBMS_RANDOM.VALUE*1OOOO,2)
--10000以内随机数,保留两位小数
make numbers
Copy and insert all the data of 20230223 days in a table to the data of 20230224 days
INSERT INTO table_name (
column1
, column2
, ...
, date_column)
SELECT
column1
, column2
, ...
, TO_DATE('20230224', 'YYYYMMDD')
FROM table_name
WHERE date_column = TO_DATE('20230223', 'YYYYMMDD');
Insert all the data of the 20230223 day partition in a table into the 20230223 day partition
INSERT INTO table_name PARTITION (partition_name)
SELECT * FROM table_name
WHERE date = TO_DATE('20230223', 'YYYYMMDD');