Oracle常用sql语句
1.
多行数据一行显示sql:(用sum函数或max函数,当lc为1时DQSYJRZC的值) sum函数只能查字段类型为number型的,若字段值是汉字则用max函数
select nsrsbh , sum(decode(lc,1,DQSYJRZC)) JYXJRZC1,sum(decode(lc,2,DQSYJRZC)),sum(decode(lc,3,DQSYJRZC)),sum(decode
(lc,4,DQSYJRZC)),sum(decode(lc,5,DQSYJRZC)) JYXJRZC2 from sb_nd_f07 t
where nsrsbh='371325751788249' and t.skqsrq=20120101 and t.skjzrq=20121231 group by nsrsbh
2.修改表名sql:
rename gsj_wsfwt_dm_swjg_ds to gsj_nsxydj_dm_swjg_ds;
3.
把空值转换成字符串:
decode(TT.BGYY,null,'')
数据库字符串连接:(用||)
'评定为' || tt.pdjg || '级。' || decode(TT.BGYY,null,'')
4.
把查询一个表的数据插入另一个表:
insert into gsj_nsxydj_gy_pdlc_ls(id,slh,nsrsbh,nsrmc,wsdm,lzzt,clsj,clry)
select gsj_gy_pdlc_ls.nextval,t.slh,t.nsrsbh,t.nsrmc,t.wsdm,t.dqzt,clsj,'"+dqry.getSwry_dm()+"' from gsj_nsxydj_gy_pdlc t where
t.nsrsbh = '"+data.getNsrsbh()+"' and t.pc_dm = '"+data.getPc_dm()+"' and t.wsdm = 'pd001'"
5.
强制使用索引:
select /*+index(dj_nsrxx IDX_DJ_NSRXX_P_NSRSBH) */ * from dj_nsrxx
6.
减去(查询一个表比另一个表多余的数据)
select d.nsrsbh
from temp_bd d
minus
select d.nsrsbh
from temp_bd2 d
7
--查询重复记录
select t.nsrsbh, count(t.nsrsbh)
from cs_bd2 t
having count(t.nsrsbh) > 1
group by t.nsrsbh
多个字段查询重复记录
--重复数据只显示一条(去重复):
select max(p_ID),POI_ID,t_id from T_TEM_PLACEINFO group by POI_ID,t_id
--删除重复数据:
delete from T_TEM_PLACEINFO where rowid not in (select min(rowid) from T_TEM_PLACEINFO group by POI_ID,t_id );
8.
分析表;analyze table gsj_nsxydj_zzdf_zd compute statistics
9.截字符串:
select substr(t.nsrsbh,-9)
from dj_nsrxx t
where t.nsrsbh = '371325751788249'--------751788249
10.查询数据的对应行数:
select rownum from zzs_xgmnsr_basedata_2012 t
11.
.复制表结构建表:
create table Sb_Cwbb_Zcfzb_2013 as select * from SB_CW_ZCFZB_2007 where 1=2;
从另一表里抽数据
insert into table1 select *from user.table2
12
条件sql语句(case....when....then.....end):
select t.ywlx_dm,(case ywlx_dm when '0072' then '呵呵' when 'XG01' then '小规模' end) ywlx
from dm_ywcd t where t.ywlx_dm in ('0072','XG01')
13
表解锁:
SELECT OBJECT_ID,SESSION_ID,SERIAL#,
ORACLE_USERNAME,OS_USER_NAME,S.PROCESS
FROM V$LOCKED_OBJECT A,
V$SESSION S WHERE A.SESSION_ID=S.SID
查出被lock 的对象
然后 alter system kill session 'sid,serial#';
14.
查询用户所在的表空间:
select username,default_tablespace from dba_users order by username;
15.
添加表字段:
alter table GSJ_WSFWT_KYDJ_HHRTZQK add (HHRZJHM varchar2(20),HHRZJZL varchar2(20) );
修改表字段:
alter table GSJ_WSFWT_DM_ZJ modify (ZJ_DM char(3))。
16
创建临时表 :
CREATE GLOBAL TEMPORARY table tabxml (str1 clob )
ON COMMIT DELETE ROWS
23.sql计算;to_char((B-C)/decode(C,0,1,C)*100,'FM999990.00') D,
select * from user_tables; |
25.批量更改某些字段(税务机关为例):
使用函数length和lengthb来判别
使用函数length与lengthb来判别,是基于中文字符占用2~4个字节,而ASCII字符占用一个字节,那么对比LENGTH与LENGTHB就会不一样。这样就能判别字段中是否包含中文字符,但是跟ASCIISTR一样,如果里面的非ASCI字符包含非中文,它一样不能判别。依然有取巧嫌疑。
SELECT NAME_ONE FROM TEST WHERE LENGTH(NAME_ONE) != LENGTHB(NAME_ONE);
28.创建dblink语句
create public database link TESTLINK2(dblink名) connect to WANGYON(数据库用户名) identified by "123456"(密码) USING 'ORCL21'(数据库实例名)
查询WANGYOU中的user表
select * from WANGYOU.user@TESTLINK2 order by id
本文原创,转载必追究版权。