當前位置:首頁 » 編程語言 » oracleplsql循環
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

oracleplsql循環

發布時間: 2022-07-16 18:17:14

1. oracle存儲過程中循環for in是如何使用的

1、首先編寫存儲過程的整體結構,如下圖所示定義變數。

2. oracle plsql 練習 要用到游標

唉,又是大學生吧,自己不動腦筋.發個例子你看看.
declare
v_num number := &input;
v_last_num number := 0;
v_result varchar(2000) := '';
begin
loop
v_last_num := mod(v_num,10);
v_result := v_result || to_char(v_last_num);
v_num := v_num - v_last_num;
exit when v_num=0;
v_num := v_num / 10;
end loop;
dbms_output.put_line(v_result);
end;

第六章 游標(cursor)管理
create SYNONYM emp for scott.emp;
--作用:
declare
v_ename emp.ename%type;
begin
select ename into v_ename
from emp;

dbms_output.put_line(v_ename);
end;

--顯式游標的定義和遍歷
--通過無條件的循環來遍歷游標
declare
--1.定義游標
cursor c_emp_cursor
is select ename from emp;

v_ename emp.ename%type;
begin
--2.打開游標
open c_emp_cursor;

--3.遍歷游標
loop
fetch c_emp_cursor into v_ename; --rs.next();

exit when c_emp_cursor%NOTFOUND;

dbms_output.put_line(v_ename);
end loop;

--4.關閉游標
close c_emp_cursor;
end;

--用WHILE循環來遍歷游標
declare
cursor c_emp_cursor
is select * from emp;

v_row emp%rowtype;
begin
open c_emp_cursor;

fetch c_emp_cursor into v_row;

while c_emp_cursor%FOUND loop
dbms_output.put_line(v_row.ename || ' ' || v_row.empno);
fetch c_emp_cursor into v_row;
end loop;

close c_emp_cursor;
end;

--FOR循環遍歷游標的方式
declare
cursor c_emp_cursor
is select * from emp;
begin
for v_row IN c_emp_cursor loop
dbms_output.put_line(v_row.empno || ' ' || v_row.ename);
end loop;
end;

--定義參數類型的游標
declare
cursor c_emp_cursor(p_deptno emp.deptno%type)
is select ename from emp where deptno=p_deptno;

v_ename emp.ename%type;
begin
open c_emp_cursor(&input);

loop
fetch c_emp_cursor into v_ename;
exit when c_emp_cursor%NOTFOUND;
dbms_output.put_line(v_ename);
end loop;

close c_emp_cursor;
end;

--為游標指定返回類型1-rowtype
declare
cursor c_emp_cursor
return emp%rowtype --強制類型
is select * from emp;
begin
for v_row IN c_emp_cursor loop
dbms_output.put_line(v_row.empno);
end loop;
end;

--為游標指定返回類型2-記錄(Record)類型
declare
--定義一個記錄類型
type t_emp_record is record
(
empno emp.empno%type,
ename emp.ename%type
);

v_emp_record t_emp_record;

cursor c_emp_cursor
return v_emp_record --強制類型
is select empno,ename from emp;
begin
open c_emp_cursor;
loop
fetch c_emp_cursor into v_emp_record;
exit when c_emp_cursor%NOTFOUND;
dbms_output.put_line(v_emp_record.empno);
dbms_output.put_line(v_emp_record.ename);
end loop;
close c_emp_cursor;
end;

--如何通過游標來修改數據
declare
--通過游標修改數據要上行級鎖
cursor c_emp_cursor
is select * from emp where deptno=20 for update;
begin
for v_row In c_emp_cursor loop
delete from emp
where current of c_emp_cursor;
end loop;
commit;
end;
select * from emp;

--REF引用類型的游標描述
declare
type t_ref_cursor is ref cursor;

v_ref_cursor t_ref_cursor;
begin
open v_ref_cursor
for select * from emp;

close v_ref_cursor;

open v_ref_cursor
for select * from dept;

close v_ref_cursor;
end;

--1.隱式游標(SQL)的使用
--用途:因為資料庫系統在做insert,delete,update,select這些操作的時候
--都是通過游標去完成的,所以,在做操作之前我們的資料庫系統都會隱式
--的打開一個游標,在執行操作完畢之後隱式的關閉游標,那麼每次的操作的結果
--都會保存在一個叫做SQL的隱式游標中,我們可以通過操作這個隱式游標來獲取
--資料庫操作的信息

begin
delete from emp where deptno=10;

dbms_output.put_line(SQL%ROWCOUNT);
end;

第七章 存儲過程(Procere)和包(Package)

SQLServer - 存儲過程 (output參數 return)
Oracle -存儲過程(out參數) 函數(return)

--1.存儲過程
create or replace procere print(msg varchar2)
is
begin
dbms_output.put_line(msg);
end;

--1.1 調用存儲過程
execute print('helloworld');

--1.2
create or replace procere findEmp(p_empno emp.empno%type)
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=p_empno;

print(v_ename);
exception
when no_data_found then
print('未發現指定員工.');
end;

--1.3 傳遞參數
create or replace procere myabs(p_num1 IN number,p_num2 OUT number)
is
begin
if p_num1 >0 then
p_num2 := p_num1;
else
p_num2 := (0 - p_num1);
end if;
end;

--調用
declare
v_result number;
begin
myabs(-987,v_result);
print(v_result);
end;

create or replace procere change_num(p_num1 IN OUT number,p_num2 IN OUT number)
is
v_temp number;
begin
v_temp := p_num1;
p_num1 := p_num2;
p_num2 := v_temp;
end;

--調用
declare
v_num1 number :=111;
v_num2 number :=222;
begin
change_num(v_num1,v_num2);

print(v_num1);
print(v_num2);
end;

--2.函數(FUNCTION)
create or replace function myabs1(p_num1 number)
return number
is
begin
if p_num1 >= 0 then
return p_num1;
else
return (0 - p_num1);
end if;
end;

begin
print(myabs1(-999));
end;

create or replace function findEmp1(p_empno emp.empno%type)
return emp.ename%type
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where empno=p_empno;

return v_ename;
exception
when no_data_found then
print('未發現員工');
end;

begin
print(findEmp1(7499));
end;

--3.包(Package)-包(package-定義)和包體(package body-實現)
create or replace package mypkg
is
procere print(msg varchar2);

function findEmp(p_empno number)
return emp.ename%type;
end;

create or replace package body mypkg
is
procere print(msg varchar2)
is
begin
dbms_output.put_line(msg);
end;

function findEmp(p_empno number)
return emp.ename%type
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where empno=p_empno;

return v_ename;
exception
when no_data_found then
print('未發現員工');
end;
end;

--4.通過函數返回結果集
create or replace function findAllEmps
return SYS_REFCURSOR
is
v_ref_cursor SYS_REFCURSOR;
begin
open v_ref_cursor
for 'select * from emp';

return v_ref_cursor;
end;
我上學時候練慣用的

3. oracle資料庫中循環同義詞,怎麼處理

1、 當用程序連接或者用plsql查詢同義詞時,如果出現ora-01775:同義詞的循環鏈這樣的問題。
一般是因為存在同義詞,但同義詞沒有相應的對象。

2、 先查有沒有循環的同義詞。
select * from dba_synonyms
where table_owner='TEST'
and synonym_name<>table_name;

沒有記錄。

3、 再查同義詞沒有對象的資料庫對象
select * from dba_synonyms
where table_owner='TEST'
and
synonym_name in
(select a.synonym_name from dba_synonyms a where a.table_owner='TEST'
minus
select object_name from user_objects)

4、 把查詢出來的結果進行查詢表
select * from DRILL_PRESON
如果該同義詞沒有相應的對象,則會包ora-01775的錯誤

5、 把這個同義詞刪除
drop public synonym DRILL_PRESON

4. 寫PLSQL語句(或存儲過程)循環insert實現數據的復制

先試試這個,好久不寫了,一會兒找個環境幫你試試,補0的問題一會兒再解決
create table B as select * from A where 1=0;
declare

cursor ind is select * from A ;

begin
for cur in ind
loop
for num 1..2

loop

insert into B values ( cur.id||num, cur.name||num, note );

end loop;

end loop;
end ;

5. Oracle中使用PL/SQL怎樣用循環插入多條數據

使用loop循環,比如:
for item in (select a,b,c from table_a where 條件) loop
insert into table_b(a,b,c) values (item.a,item.b,item.c);
end loop;
也可以使用索引表循環,以上只是一個簡單的例子,需要根據你的具體情況選擇循環方式。

6. Oracle 11g的PLSQL部分

·結果集緩存(Result Set Caching)
這一特性能大大提高很多程序的性能。在一些MIS系統或者OLAP系統中,需要使用到很多select count(*)這樣的查詢。在之前,我們如果要提高這樣的查詢的性能,可能需要使用物化視圖或者查詢重寫的技術。在11g,我們就只需要加一個 /*+result_cache*/的提示就可以將結果集緩存住,這樣就能大大提高查詢性能。當然,在這種情況下,我們可能還要關心另外一個問題:完整性。因為在oracle中是通過一致性讀來保證數據的完整性的。而顯然,在這種新特性下,為提高性能,是從緩存中的結果集中讀取數據,而不會從回滾段中讀取數據的。關於這個問題,答案是完全能保證完整性。因為結果集是被獨立緩存的,在查詢期間,任何其他DML語句都不會影響結果集中的內容,因而可以保證數據的完整性。
·對象依賴性改進
在11g之前,如果有函數或者視圖依賴於某張表,一旦這張表發生結構變化,無論是否涉及到函數或視圖所依賴的屬性,都會使函數或視圖變為invalid。在11g中,對這種情況進行了調整:如果表改變的屬性與相關的函數或視圖無關,則相關對象狀態不會發生變化。
·正則表達式的改進
在10g中,引入了正則表達式。這一特性大大方便了開發人員。11g,oracle再次對這一特性進行了改進。其中,增加了一個名為regexp_count的函數。另外,其他的正則表達式函數也得到了改進。
·新SQL語法 =>
我們在調用某一函數時,可以通過=>來為特定的函數參數指定數據。而在11g中,這一語法也同樣可以出現在sql語句中了。例如,你可以寫這樣的語句:
select f(x=>6) from al;
·對TCP包(utl_tcp、utl_smtp…)支持FGAC(Fine Grained Access Control)安全控制
·增加了只讀表(read-only table)
在以前,我們是通過觸發器或者約束來實現對表的只讀控制。11g中不需要這么麻煩了,可以直接指定表為只讀表。
·觸發器執行效率提高了
·內部單元內聯(Intra-Unit inlining)
在C語言中,你可以通過內聯函數(inline)或者宏實現使某些小的、被頻繁調用的函數內聯,編譯後,調用內聯函數的部分會編譯成內聯函數的函數體,因而提高函數效率。在11g的plsql中,也同樣可以實現這樣的內聯函數了。
·設置觸發器順序
可能在一張表上存在多個觸發器。在11g中,你可以指定它們的觸發順序,而不必擔心順序混亂導致數據混亂。
·混合觸發器(compound trigger)
這是11g中新出現的一種觸發器。她可以讓你在同一觸發器中同時具有申明部分、before過程部分、after each row過程部分和after過程部分。
·創建無效觸發器(Disabled Trigger)
11g中,開發人員可以可以閑創建一個invalid觸發器,需要時再編譯她。
·在非DML語句中使用序列(sequence)
在之前版本,如果要將sequence的值賦給變數,需要通過類似以下語句實現:
select seq_x.next_val into v_x from al;
在11g中,不需要這么麻煩了,下面語句就可以實現:
v_x := seq_x.next_val;
·PLSQL_Warning
11g中。可以通過設置PLSQL_Warning=enable all,如果在when others沒有錯誤爆出就發警告信息。
·PLSQL的可繼承性
可以在oracle對象類型中通過super(和java中類似)關鍵字來實現繼承性。
·編譯速度提高
因為不再使用外部C編譯器了,因此編譯速度提高了。
·改進了DBMS_SQL包
其中的改進之一就是DBMS_SQL可以接收大於32k的CLOB了。另外還能支持用戶自定義類型和bulk操作。
·增加了continue關鍵字
在PLSQL的循環語句中可以使用continue關鍵字了(功能和其他高級語言中的continue關鍵字相同)。
·新的PLSQL數據類型——simple_integer
這是一個比pls_integer效率更高的整數數據類型。

7. 請教大神,oracle資料庫循環語句怎麼寫

  1. 你想要的這幾個結果,都可以直接使用SQL語句查出,無需循環。

  2. Oracle循環實在PLSQL塊中編寫:關鍵字for XXX loop 循環體 end loop;

8. 如何向oracle 資料庫循環錄入數據

INSERT INTO table
SELECT 'aaa' || ROWNUM, 'aaa' FROM DUAL CONNECT BY ROWNUM <= 100

其中 100是你想插入的記錄條數