您好,登錄后才能下訂單哦!
這篇文章主要介紹“在PL/SQL塊中怎么執行select語句”,在日常操作中,相信很多人在在PL/SQL塊中怎么執行select語句問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”在PL/SQL塊中怎么執行select語句”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在PL/SQL塊中"END;"表示結束PL/SQL塊,但并沒有結束事務,一個塊可以跨越多個事務,一個事務也可以跨越多個塊;PL/SQL塊中不支持DDL和DCL語句,例如建表,賦予用戶權限等.
一:在PL/SQL塊中執行select語句
select語句在PL/SQL塊中的語法結構如下
SELECT select_list
INTO {variable_name[, variable_name]...
| record_name}
FROM table
[WHERE condition];
范例:
SQL> set serveroutput on
SQL> DECLARE
2 v_first_name hr.employees.first_name%TYPE;
3 v_last_name hr.employees.last_name%TYPE;
4 BEGIN
5 SELECT first_name,last_name INTO
6 v_first_name,v_last_name FROM hr.employees
7 WHERE employee_id=100;
8 DBMS_OUTPUT.PUT_LINE(v_first_name ||' '||v_last_name);
9* END;
SQL> /
Steven King
PL/SQL procedure successfully completed
二.在PL/SQL塊中執行DML語句和控制事務
可以在PL/SQL塊中使用SQL的函數,自定義的序列號,字段的默認值插入數據
SQL> create table t14703a (id number(5),constraint pk_t14703a primary key(id),
2 location varchar2(30) default 'fuzhou',salary number(8,2));
Table created.
SQL> create sequence user_seq
2 increment by 1
3 start with 1
4 maxvalue 99999
5 nocache
6* nocycle
Sequence created.
SQL> select user_seq.nextval from dual;
NEXTVAL
----------
1
SQL> BEGIN
2 INSERT INTO hr.t14703a
3 (id,location,salary) VALUES
4 (user_seq.nextval,default,'6000');
5 COMMIT;
6* END;
SQL> /
PL/SQL procedure successfully completed.
SQL> select * from hr.t14703a;
ID LOCATION SALARY
---------- ---------- ----------
3 fuzhou 6000
SQL> DECLARE
2 v_salary_increase hr.t14703a.salary%TYPE := 500;
3 BEGIN
4 UPDATE hr.t14703a SET salarysalary= salary + v_salary_increase
5 WHERE id=3;
6* END;
7 /
PL/SQL procedure successfully completed.
SQL> select * from hr.t14703a;
ID LOCATION SALARY
---------- ---------- ----------
3 fuzhou 6500
三.使用隱式游標定義PL/SQL塊中DML語句的輸出
SQL游標,游標是私有的SQL工作區,有隱式和顯式游標兩個類型;oracle server使用隱式的游標來執行SQL語句
A cursor is a private SQL work area.
There are two types of cursors:
–Implicit cursors
–Explicit cursors
The server uses implicit cursors to parse and execute your SQL statements.
Explicit cursors are explicitly declared by the programmer.
4種游標的屬性SQL%COUNT,SQL%FOUND,SQL%NOTFOUND,SQL%ISOPEN
SQL> VAR rownums varchar2(30)
SQL> set serveroutput on
SQL> DECLARE
2 v_a hr.t04310_a.a%TYPE := 50000;
3 BEGIN
4 delete from hr.t04310_a where a < v_a;
5 :rownums := (SQL%ROWCOUNT ||' ' || 'row deleted');
6 DBMS_OUTPUT.PUT_LINE(:rownums);
7* END;
8 /
40000 row deleted
PL/SQL procedure successfully completed.
到此,關于“在PL/SQL塊中怎么執行select語句”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。