oracle 语句 笔记

1、查询某个表有多少列。

1 select column_name 
2   from user_tab_columns
3 where table_name = 'DQ_S1';

列出所有的字段名。

2、查询昨天一天24个小时数

1 select * 
2 from dq_s2 
3 where date_time > trunc(sysdate-1) and date_time <=trunc(sysdate)
4 order by siteid asc,date_time asc;

 3、插入日期型值

1 insert into employee(empno, ename, hiredate, sal) values(7369,'SMITH', to_date('1980/12/17', 'yyyy/MM/dd'),800)

 4、merge into 用法

merge into 既可以更新,也可以插入

1)满足条件时,更新(update);不满足,插入(insert)。

1 merge into test02 t1
2 using (select 3 aa  from dual) t2
3 on (t1.aa = t2.aa)
4 when matched then
5 update set t1.bb = 6
6 when not matched then
7 insert (aa,bb) values (3,6)

表test02 就两列 aa bb

select 3 aa  from dual 判断 aa 中是否有3 这个值,并且on (t1.aa = t2.aa) 或多个条件,如果有就update 更新 bb,如果没有就inset插入

1  merge into zd_sjlr t1 using dual on(t1.C_DATE=to_date('20130908','yyyy-MM-dd') and t1.C_TIME='0:00-12:00' and t1.C_ROAD='CH2') 
2 when matched then update set t1.C_BTIJI=2.1, t1.C_STIJI=6.1 where t1.C_DATE=to_date('20130908','yyyy-MM-dd') and t1.C_TIME='0:00-12:00' and t1.C_ROAD='CH2' 
3 when not matched then insert(t1.C_DATE,t1.C_TIME,t1.C_ROAD,t1.C_BTIJI,t1.C_STIJI) values(to_date('20130908','yyyy-MM-dd'),'0:00-12:00','CH2','2.1','6.1')

 批执行

declare
  total date:=  trunc(sysdate-2) ;
  begin
  for i_count in 1..10000 LOOP
   insert into DQ_DATE(date_time)  values (  total );
  total := total + 1/24;
  end loop;
end;

 2)同一张表,不满足条件,插入(insert)数据

1 merge into dq_report_info t1 using dual on(t1.reportname=?) 
2 when not matched then insert(t1.FID,t1.DUTY_DATE,t1.REPORTNAME,t1.DUTY_NAME,t1.REPORTSRC,t1.REPORTDATE,t1.STATUS1)
3 values(sys_guid(),?,?,?,?,?,?)

5、原始数据时序图

连接两列的字符串:t1.siteid||'-'||t2.sitename

1 select t2.sitename,t1.*,t1.siteid||'-'||t2.sitename siteall from 
2 (select tb1.*,case when tb2.FID is not null then '有数' else '无数' end status from 
3 (select SITEID,DATE_TIME - interval '1' hour starttime,DATE_TIME endtime from (select DATE_TIME from DQ_DATE  where DATE_TIME > sysdate -interval '7' hour and DATE_TIME < sysdate),(select distinct siteid siteid from DQ_S2 where DATE_TIME > sysdate-7) order by siteid desc,DATE_TIME) tb1
4 left join DQ_S2 tb2 on tb1.siteid=tb2.siteid and tb1.endtime = tb2.DATE_TIME)  t1
5  left join DQ_ZDXXB t2 on t1.siteid = t2.siteid 

 6、合并多条数据为一条数据

用wm_concat()  函数  数据<4000条

1 SELECT wm_concat (t2.sitename) sitename
2   FROM    (SELECT *
3              FROM DQ_T_V8_MV
4             WHERE siteid IN (3,5,11,18,9,10,19,20,8,28,7,37)
5                   AND all_level = 1) t1
6        LEFT JOIN
7           DQ_ZDXXB t2
8        ON t1.siteid = t2.siteid
9  WHERE t1.day_time = TRUNC (SYSDATE - 1);
View Code

 7、联合主键

CREATE TABLE DQ_QK_C
(
   C_DATE      DATE,
   C_CONTENT   VARCHAR2 (10),
   C_RESULT    VARCHAR2 (5),
   C_PROCESS   VARCHAR2 (50),
   CONSTRAINT PK_TAB PRIMARY KEY (C_DATE, C_CONTENT)/*联合主键*/
);

 8、修改添加字段

alter table tableName modify(colName varchar2(25));
alter table table_name add (col1 type,col2 type);

 9、复制表结构、数据

--1. 复制表结构及其数据:

create table table_name_new as select * from table_name_old

--2. 只复制表结构:

create table table_name_new as select * from table_name_old where 1=2;

--或者:

create table table_name_new like table_name_old

--3. 只复制表数据:

--如果两个表结构一样:

insert into table_name_new select * from table_name_old

--如果两个表结构不一样:

insert into table_name_new(column1,column2...) select column1,column2... from table_name_old

pasting

 10、insert into  Table as Select

insert into table a select * from table b 

 11、把A用户的t1授权给B用户

grant select on t1 to B
原文地址:https://www.cnblogs.com/kingxiaozi/p/3487851.html