一、利用游標實現迴圈巢狀
在對oracle資料進行操作時我們會經常碰到迴圈甚至迴圈巢狀的情況。這個時候游標的作用就體現出來了。
declare
vid number(19);
vdate date;
--a表游標定義
cursor a_cursor is
select distinct o.employeeid
from operations o
where o.employeeid is null
order by 1;
--b表游標定義
cursor b_cursor(eid number) is
select distinct trunc(o.startdate) startdate
from operations o
where o.employeeid = eid
and o.employeeid is null
order by 1;
begin
for a_cur in a_cursor loop
vid := a_cur.receiverid;
for b_cur in b_cursor(vid) loop
vdate := b_cur.startlifecycle;
employee_operationsettlement(vdate,vid);--做操作
end loop;
end loop;
end;
二、靈活使用%rowtype
declare
v_employee employees%rowtype
begin
select * into v_operation
from employees e
where e.code = '9999';
--插入資料
insert into employees (code,name,age)
values ('9998',v_operation.name,v_operation.age);
--修改資料
update employees t
set row = v_employee
where e.code = '9999';
end;
三、根據主鍵表名查詢存在外來鍵關聯的表名和欄位名,並查詢指定主鍵值在外鍵表中關聯的資料條目
declare
vsql varchar2(256);
vcount number;
cursor a_cursor is
select distinct
b.table_name tablename,c.column_name columnname
from dba_constraints a, dba_constraints b, user_cons_columns c
where a.constraint_name = b.r_constraint_name
and b.constraint_type = 'r'
and a.table_name = 'operationroles'
and c.constraint_name = b.constraint_name
and (instr(b.table_name,'_')=0 or instr(b.table_name,'_cs')>0)--表名過濾條件(不包含『_』,或包含『_cs』)
order by 1,2;
begin
for a_cur in a_cursor loop
vsql := 'select count('||a_cur.columnname||') from '||a_cur.tablename||' where '||a_cur.columnname||' = 40010';--指定值為(40010)
execute immediate vsql into vcount;
if vcount > 0 then --只看存在資料的條目
dbms_output.put_line(vsql||'____'||vcount);--輸出(sql____對應資料條數)
end if;
end loop;
end;
四、遞迴查詢
--尋根
select o.*
from partyroleassociations p,organizationunits o
where p.partyroleid = o.id
and level = 1 --層級
start with p.associationpartyroleid = '131000755'--開始子節點id
connect by p.associationpartyroleid = prior p.partyroleid;
---自頂向下
select o.*
from partyroleassociations p,organizationunits o
where p.associationpartyroleid = o.id
start with p.associationpartyroleid = '131000161'--父級節點id
connect by prior p.associationpartyroleid = p.partyroleid;
五、通過group by和wm_concat來快速定位資料
select o.employeeid, wm_concat(o.name) --行列轉換
from operations o
group by o.employeeid;
六、依照模板資料快速插入資料,特別適用於表字段特別多的情況
declare
v_employee employees%rowtype;
cursor a_cursor is select * from employees;
begin
select * into v_operation
from employees e
where e.code = '9999';
--1insert into employees (code,name,age)
values ('9998',v_operation.name,v_operation.age);
--2v_operation.code := '9997';
v_operation.name := '張三';
insert into employees
values v_operation;
--3for a_cur in a_cursor loop
if a_cur.code = '9999' then
a_cur.code := '9996';
a_cur.name := '李四';
insert into employees
values a_cur;
end if;
end loop;
--4insert into employees
(code, name, age)
select '9995' code, --值,別名(可忽略)
'王五' name,
agefrom employees e
where e.code = '9999';
end;
七、快速定位儲存過程
在專案開發中,經常遇到這樣的情況,現在需要改動乙個表、函式或者儲存過程(簡稱為物件),但是不知道這個物件被哪些函式、定時任務或儲存過程呼叫,便可利用以下sql解。
select distinct t.type "型別", t.name "名稱"
from user_source t
where t.type = 'procedure' --function(函式),procedure(儲存過程),trigger(觸發器)
and lower(t.text) like '%string%' --string英文小寫
union all
select distinct 'job' "型別", j.job_name "名稱" from user_scheduler_jobs j
where lower(j.job_action) like '%string%';--查詢定時任務action中是否包含
八、中斷oracle的job
網上查了一下,大多是通過dba_jobs_running去查sid,發現查不到,後來發現是可以通過dba_scheduler_running_jobs去查sid的
select t.session_id sid from dba_scheduler_running_jobs t;
select sid,serial# from v$session where sid = '1018';
alter system kill session '1018,127';
QML開發常用知識
1.qml的內部邏輯可以直接除錯 2.ctrl alt space,在寫qml時,可以直接調出工具條 3.屬性以小寫字母開發 4.屬性改變事件,基本都是on property changed 5.在 裡使用qrc資源的qml,在從qrc的item複製路徑下新增qrc,然後冒號後再加乙個斜槓如qrc ...
Oracle常用知識點
oracle的預設賬號及密碼有以下三種 1.使用者名稱 sys密碼 change on install 2.使用者名稱 system密碼 manager 3.使用者名稱 scott密碼 tiger 注意登陸模式不是normal select 分組的列,sum 要求和的列 from 表 group b...
ORACLE的常用知識技巧
對資料表要進行備份可以在同一表空間裡新建一張表 create table t bak as select from t 如果要對某些表或檢視建立同義詞可以通過語句執行 select create or replace public synonym table name for user.table ...