目录
1、查询表的前10条数据
select p.* from t_policy p where rownum < 10;
2、查询orcle表是否被被锁
select sess.sid,
sess.serial#,
lo.oracle_username,
lo.os_user_name,
ao.object_name,
lo.locked_mode
from v$locked_object lo, dba_objects ao, v$session sess
where ao.object_id = lo.object_id
and lo.session_id = sess.sid;
如有记录则表示有lock,记录下SID和serial# ,讲记录的ID替换下面的738,1429,即可解除LOCK
alter system kill session '738,1429';
3、列转行函数
select deptno,listagg(ename,',')within group(order by sal)name from emp group by deptno;
4、创建表并添加字段
alter table t_policy_rebak add (policy_status varchar(4),order_status varchar(4),order_charge_status varchar(4));
5、自己生成sql
select 'update t_policy_rebak t set t.order_id = '''||p.order_id||''' where t.policy_id='''||p.id||''';'
from t_policy p
where p.id in ('dasdadadadadadaf95e6d93');
6、多表更新
update t_branch_agent aa
set (aa.branch_code,aa.agent_code)=
(select b.branch_code,a.agent_code from t_sal_agent_info a
join t_branch_info b on b.branch_code=substr(a.manage_branch_code,0,4)
where a.agent_code in('123','234','345') and b.branch_code=aa.branch_code)
where exists(select 1 from t_agent_info a
join t_branch_info b on b.branch_code=substr(a.manage_branch_code,0,4)
where a.agent_code in('123','234','234') and b.branch_code=aa.branch_code);
7、Oracle 汉字排序
ORDER BY nlssort(NAME, 'NLS_SORT=SCHINESE_PINYIN_M') --按照拼音排序
ORDER BY nlssort(NAME, 'NLS_SORT=SCHINESE_RADICAL_M') --按照部首排序
ORDER BY nlssort(NAME, 'NLS_SORT=SCHINESE_STROKE_M') --按照笔画排序
8、Oracle循环遍历
select b.branch_name,
b.branch_code
from t_branch_info b
start with b.branch_code in
(select substr(gap.branch_code,0,4)
from t_activity_privilege gap
where gap.activity_id = 'asdafafadsfadfasdfsdfs'
and gap.channel_code='2')
connect by prior b.branch_code = b.parent_privilege_branch
9、oracle序列的创建、使用和删除
--创建序列
create sequence emp_sequence
increment by 1 -- 每次加几个
start with 1 -- 从1开始计数
nomaxvalue -- 不设置最大值
nocycle -- 一直累加,不循环
cache 100;
--创建序列
create sequence nad_pv_sequ
increment by 1
start with 1807487
maxvalue 999999999999999999
nocycle
cache 100;
--删除序列
drop sequence nad_pv_sequ;
为表创建序列
create sequence empseq ... ;
select empseq.currval from dual ;
--自动插入序列的数值
insert into emp
values (empseq.nextval, 'lewis', 'clerk',
7902, sysdate, 1200, null, 20) ;