1.游標(biāo)定義:\x0d\x0acursor XXXA is\x0d\x0a SELECT 語(yǔ)句;\x0d\x0aXXXB cursorName%rowtype;\x0d\x0a\x0d\x0aXXXA: 游標(biāo)名\x0d\x0aXXXB: 游標(biāo)行數(shù)據(jù)定義\x0d\x0a\x0d\x0a2. 打開(kāi)游標(biāo):\x0d\x0a-- 打開(kāi)之前最好先關(guān)一下,防止上次發(fā)生異常沒(méi)有關(guān)掉而引發(fā)不必要的異常\x0d\x0a IF XXXA%ISOPEN THEN\x0d\x0a CLOSE XXXA;\x0d\x0a END IF;\x0d\x0a\x0d\x0aOpen XXXA ;\x0d\x0a Loop\x0d\x0a Fetch XXXA into XXXB;\x0d\x0a exit when XXXA%NOTFOUND;\x0d\x0a... ... 處理邏輯\x0d\x0a end loop;\x0d\x0a close XXXA;

創(chuàng)新互聯(lián)建站主營(yíng)清河網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,清河h5成都微信小程序搭建,清河網(wǎng)站營(yíng)銷(xiāo)推廣歡迎清河等地區(qū)企業(yè)咨詢(xún)
如果都在同一個(gè)用戶(hù)下 直接insert into tab1(字段1,字段2....) select 字段1,字段2.... from tab2
如果表結(jié)構(gòu)和字段順序完全一樣 直接insert into tab1 select * from tab2 即可
如果在同一個(gè)實(shí)例不在同一個(gè)用戶(hù)下,可以先賦給用戶(hù)查詢(xún)?cè)摫淼臋?quán)限 或者用dba用戶(hù)操作(語(yǔ)句一樣,表前帶用戶(hù)名)
如果不在同一個(gè)實(shí)例下 可以建DBLINK 或者導(dǎo)出SQL語(yǔ)句 或者保存成csv用sqllldr 或者直接用exp
或者用其他工具如ETL,spss 或者JAVA 代碼等等 方法很多
以hr用戶(hù)下的employees、departments、locations這三張表為列,sin1得到的是雇員的全名和對(duì)應(yīng)的部門(mén)id,并將該部門(mén)的id作為sin2查詢(xún)時(shí)的條件,sin2得到的是該部門(mén)id所對(duì)應(yīng)的部門(mén)名和對(duì)應(yīng)的位置id,并將該位置id作為sin3查詢(xún)使得條件,最后sin3得到的就是該位置id所應(yīng)得城市,并且在sin3這個(gè)循環(huán)里將sin1里雇員的全名,sin2里的部門(mén)名以及sin3里的city作為一條記錄插入到sin_insert表里.
附上代碼:
first:
create table sin_insert(full_name varchar2(50),department_name varchar2(30),city
varchar2(30));
then:
create or replace procedure testloop
as
begin
for sin1 in (select first_name||last_name full_name,department_id from
employees) loop
for sin2 in (select department_name,location_id from departments where
department_id=sin1.department_id) loop
for sin3 in (select city from locations where
location_id=sin2.location_id) loop
insert into sin_insert values
(sin1.full_name,sin2.department_name,sin3.city);
end loop;
end loop;
end loop;
end;
可以,顯式游標(biāo)循環(huán)取scott.dept表的數(shù)據(jù)插入test表,試試吧。
例子:
declare
type
type_corsor
is
ref
cursor;
c_dept
type_corsor;
row_dept
scott.dept%rowtype;
begin
open
c_dept
for
select
*
from
scott.dept;
loop
fetch
c_dept
into
row_dept;
exit
when
c_dept%notfound;
insert
into
test
values(row_dept.deptno,row_dept.dname)
end
loop;
close
c_dept;
end;
游標(biāo)能夠根據(jù)查詢(xún)條件從數(shù)據(jù)表中提取一組記錄,將其作為一個(gè)臨時(shí)表置于數(shù)據(jù)緩沖區(qū)中,利用指針逐行對(duì)記錄數(shù)據(jù)進(jìn)行操作。
Oracle中的游標(biāo)分為顯示游標(biāo)和隱式游標(biāo) 。
在執(zhí)行SQL語(yǔ)句時(shí),Oracle會(huì)自動(dòng)創(chuàng)建隱式游標(biāo),該游標(biāo)是內(nèi)存中處理該語(yǔ)句的數(shù)據(jù)緩沖區(qū),存儲(chǔ)了執(zhí)行SQL語(yǔ)句的結(jié)果。通過(guò)隱式游標(biāo)屬性可獲知SQL語(yǔ)句的執(zhí)行狀態(tài)信息。
%found:布爾型屬性,如果sql語(yǔ)句至少影響到一行數(shù)據(jù),值為true,否則為false。
%notfound:布爾型屬性,與%found相反。
%rowcount:數(shù)字型屬性,返回受sql影響的行數(shù)。
%isopen:布爾型屬性,當(dāng)游標(biāo)已經(jīng)打開(kāi)時(shí)返回true,游標(biāo)關(guān)閉時(shí)則為false。
用戶(hù)可以顯式定義游標(biāo)。使用顯式游標(biāo)處理數(shù)據(jù)要4個(gè)步驟:定義游標(biāo)、打開(kāi)游標(biāo)、提取游標(biāo)數(shù)據(jù)和關(guān)閉游標(biāo)。
游標(biāo)由游標(biāo)名稱(chēng)和游標(biāo)對(duì)應(yīng)的select結(jié)果集組成。定義游標(biāo)應(yīng)該放在pl/sql程序塊的聲明部分。
語(yǔ)法格式:cursor 游標(biāo)名稱(chēng)(參數(shù)) is 查詢(xún)語(yǔ)句
打開(kāi)游標(biāo)時(shí),游標(biāo)會(huì)將符合條件的記錄送入數(shù)據(jù)緩沖區(qū),并將指針指向第一條記錄。
語(yǔ)法格式:open 游標(biāo)名稱(chēng)(參數(shù));
將游標(biāo)中的當(dāng)前行數(shù)據(jù)賦給指定的變量或記錄變量。
語(yǔ)法格式:fetch 游標(biāo)名稱(chēng) into 變量名;
游標(biāo)一旦使用完畢,就應(yīng)將其關(guān)閉,釋放與游標(biāo)相關(guān)聯(lián)的資源。
語(yǔ)法格式:close 游標(biāo)名稱(chēng);
declare
cursor c1 is? select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
loop
? fetch c1 into v_sno,v_cno,v_grade;
? exit when c1%notfound;--緊跟fetch之后
if c1%found then
dbms_output.put_line(to_char(c1%rowcount)||v_cno);
end if;
end loop;
close c1;?
end;
declare
cursor c1 is select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
fetch c1 into v_sno,v_cno,v_grade;
while c1%found loop
? dbms_output.put_line(v_sno||v_cno||v_grade);
?fetch c1 into v_sno,v_cno,v_grade;
end loop;
close c1;?
end;
第三種:for
declare
cursor c1 is select sno,cno,grade from sc;
begin
for item in c1 loop
dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));
end loop;
end;
Insert Into table2 value row_dept;
改成Insert Into table2(字段1,字段2…) value row_dept;
什么錯(cuò)誤,粘上來(lái)看看
網(wǎng)頁(yè)標(biāo)題:oracle游標(biāo)怎么插入,oracle游標(biāo)的使用
轉(zhuǎn)載源于:http://chinadenli.net/article49/dsehpeh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、商城網(wǎng)站、響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)