A. PL、sql的游標怎麼用
一> 游標是什麼:x0dx0a游標字面理解就是游動的游標。x0dx0a用資料庫語言來描述:游標是映射在結果集中一行數據上的位置實體,有了游標x0dx0a用戶就可以訪問結果集中的任意一行數據了,將游標放置到某行後,即可對該行數據進行操作,例如提取當前x0dx0a行的數據等等。x0dx0a二> 游標的分類:x0dx0a顯式游標和隱式游標x0dx0ax0dx0a顯示游標的使用需要4步:x0dx0a1.聲明游標x0dx0ax0dx0aCURSOR mycur(vartype number) isx0dx0aselect emp_no,emp_zc from cus_emp_basic x0dx0awhere com_no = vartype;x0dx0ax0dx0a2.打開游標x0dx0aopen mycur(000627) 註:000627:參數x0dx0ax0dx0a3.讀取數據x0dx0afetch mycur into varno,varprice;x0dx0ax0dx0a4.關閉游標x0dx0aclose mycur;x0dx0a三> 游標的屬性x0dx0aoracle 游標有4個屬性: %ISOPEN , %FOUND , %NOTFOUND, %ROWCOUNTx0dx0ax0dx0a%ISOPEN 判斷游標是否被打開,如果打開%ISOPEN 等於true,否則等於falsex0dx0a%FOUND %NOTFOUND 判斷游標所在的行是否有效,如果有效,則%FOUNDD等於true,否則等於falsex0dx0a%ROWCOUNT 返回當前位置為止游標讀取的記錄行數。x0dx0ax0dx0a四> 示例:x0dx0ax0dx0aset serveroutput on;x0dx0adeclarex0dx0avarno varchar2(20);x0dx0avarprice varchar2(20);x0dx0ax0dx0aCURSOR mycur(vartype number) isx0dx0aselect emp_no,emp_zc from cus_emp_basic x0dx0awhere com_no = vartype;x0dx0abeginx0dx0ax0dx0aif mycur%isopen = false thenx0dx0ax0dx0aopen mycur(000627);x0dx0aend if;x0dx0afetch mycur into varno,varprice;x0dx0awhile mycur%found x0dx0aloopx0dx0adbms_output.put_line(varno||','||varprice);x0dx0aif mycur%rowcount=2 thenx0dx0aexit;x0dx0aend if;x0dx0afetch mycur into varno,varprice;x0dx0ax0dx0aend loop;x0dx0aclose mycur;x0dx0ax0dx0aend;x0dx0ax0dx0apl/sql 記錄 的結構和c語言中的結構體類似,是由一組數據項構成的邏輯單元。x0dx0apl/sql 記錄並不保存再資料庫中,它與變數一樣,保存再內存空間中,在使用記錄時候,要首先定義記錄結構x0dx0a,然後聲明記錄變數。可以把pl/sql記錄看作是一個用戶自定義的數據類型。x0dx0ax0dx0aset serveroutput on;x0dx0adeclarex0dx0ax0dx0atype person is recordx0dx0a(x0dx0aempno cus_emp_basic.emp_no%type,x0dx0aempzc cus_emp_basic.emp_zc%type);x0dx0ax0dx0aperson1 person;x0dx0ax0dx0acursor mycur(vartype number)isx0dx0aselect emp_no,emp_zc from cus_emp_basic x0dx0awhere com_no=vartype;x0dx0ax0dx0abeginx0dx0aif mycur%isopen = false thenx0dx0aopen mycur(000627);x0dx0aend if;x0dx0ax0dx0aloopx0dx0afetch mycur into person1;x0dx0aexit when mycur%notfound;x0dx0adbms_output.put_line('雇員編號:'||person1.empno||',地址:'||person1.empzc);x0dx0aend loop;x0dx0aclose mycur;x0dx0aend;x0dx0ax0dx0a典型游標for 循環x0dx0ax0dx0a游標for循環示顯示游標的一種快捷使用方式,它使用for循環依次讀取結果集中的行x0dx0a數據,當form循環開始時,游標自動打開(不需要open),每循環一次系統自動讀取x0dx0a游標當前行的數據(不需要fetch),當退出for循環時,游標被自動關閉(不需要使用close)x0dx0ax0dx0a使用游標for循環的時候不能使用open語句,fetch語句和close語句,否則會產生錯誤。x0dx0ax0dx0aset serveroutput on; mycur(vartype number)isx0dx0aselect emp_no,emp_zc from cus_emp_basic x0dx0awhere com_no=vartype; person in mycur(000627) loopx0dx0ax0dx0adbms_output.put_line('雇員編號:'||person.emp_no||',地址:'||person.emp_zc);x0dx0aend loop;x0dx0ax0dx0aend;
B. SQL游標的問題問題,怎麼解決
--一般游標分定義,打開,讀取,關閉和釋放幾個步驟--1.定義變數,用來保存游標讀取的數據DECLARE
@ID
INT
--2.定義游標DECLARE
Cur
CURSOR
FOR
Select
id
From
表--3.打開游標OPEN
Cur--4.循環讀取FETCH
NEXT
FROM
Cur
INTO
@ID
WHILE
@@FETCH_STATUS
=
0BEGIN--要做的事FETCH
NEXT
FROM
Cur
INTO
@IDEND
--5.關閉游標CLOSE
Cur--6.釋放游標DEALLOCATE
Cur
C. SQL游標怎麼用
例子
table1結構如下
id
int
name
varchar(50)
declare
@id
int
declare
@name
varchar(50)
declare
cursor1
cursor
for
--定義游標cursor1
select
*
from
table1
--使用游標的對象(跟據需要填入select文)
open
cursor1
--打開游標
fetch
next
from
cursor1
into
@id,@name
--將游標向下移1行,獲取的數據放入之前定義的變數@id,@name中
while
@@fetch_status=0
--判斷是否成功獲取數據
begin
update
table1
set
name=name+'1'
where
id=@id
--進行相應處理(跟據需要填入SQL文)
fetch
next
from
cursor1
into
@id,@name
--將游標向下移1行
end
close
cursor1
--關閉游標
deallocate
cursor1
D. plsql中怎樣獲取未知結構的動態游標(ref
如果給的是一個查詢SQL文本,那麼事情很容易(對於9i及以上版本),只要使用dbms_sql.open_cursor打開游標,再使用dbms_sql.describe_columns即可得到游標的所有欄位名稱及類型等數據,存儲在一個集合類型變數中(具體請看dbms_sql.desc_tab)。請參考如下PLSQL代碼:
DECLARE
l_curidINTEGER; l_cntNUMBER;
l_desctabdbms_sql.desc_tab; l_sqltextVARCHAR2(2000); BEGIN
l_sqltext := 'select owner,object_type,object_name from dba_objects
where rownum<= 10';--可以是任意有效的查詢sql文本 l_curid := dbms_sql.open_cursor();
dbms_sql.parse(l_curid, l_sqltext, dbms_sql.native); dbms_sql.describe_columns(l_curid, l_cnt, l_descTab);
FOR i IN1 ..l_desctab.count LOOP
dbms_output.put(lpad(l_desctab(i).col_name, 20)); ENDLOOP;
dbms_output.new_line;
dbms_sql.close_cursor(l_curId); END;
運行結果如下:
OWNER OBJECT_TYPE OBJECT_NAME
注意,必須使用 DBMS_SQL.OPEN_CURSOR 打開游標,否則,就不是這種處理方法了。
E. sql 中游標的作用及使用方法
游標可以從資料庫中查詢出一個結果集,在你關閉它之前,你可以反復使用這個結果集,讀取這個結果集中的任意行任意欄位的內容,一般在存儲過程或前台程序中常見。
F. Sql中的游標是幹嘛的
游標(cursor)是結果集的邏輯擴展,可以看做指向結果集的一個指針,通過使用游標,應用程序可以逐行訪問並處理結果集。
ResultSet對象用於接收查詢結果,next()方法用於判斷結果集是否為空,相當於指針,指向結果集下一個數據。
(6)sql讀取游標擴展閱讀:
游標的生命周期包含有五個階段:聲明游標、打開游標、讀取游標數據、關閉游標、釋放游標。
1、聲明游標語法
DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]
[ FORWARD_ONLY | SCROLL ]
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
[ TYPE_WARNING ]
FOR select_statement
[ FOR UPDATE [ OF column_name [ ,...n ] ] ]
2、打開游標語法
open [ Global ] cursor_name | cursor_variable_name
3、讀取游標數據語法
Fetch[ [Next|prior|Frist|Last|Absoute n|Relative n ]from ][Global] cursor_name[into @variable_name[,....]]
4、關閉游標語法
close [ Global ] cursor_name | cursor_variable_name
5、釋放游標語法
deallocate cursor_name
G. PL、SQL的游標怎麼用
一> 游標是什麼:
游標字面理解就是游動的游標。
用資料庫語言來描述:游標是映射在結果集中一行數據上的位置實體,有了游標
用戶就可以訪問結果集中的任意一行數據了,將游標放置到某行後,即可對該行數據進行操作,例如提取當前
行的數據等等。
二> 游標的分類:
顯式游標和隱式游標
顯示游標的使用需要4步:
1.聲明游標
CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no = vartype;
2.打開游標
open mycur(000627) 註:000627:參數
3.讀取數據
fetch mycur into varno,varprice;
4.關閉游標
close mycur;
三> 游標的屬性
oracle 游標有4個屬性: %ISOPEN , %FOUND , %NOTFOUND, %ROWCOUNT
%ISOPEN 判斷游標是否被打開,如果打開%ISOPEN 等於true,否則等於false
%FOUND %NOTFOUND 判斷游標所在的行是否有效,如果有效,則%FOUNDD等於true,否則等於false
%ROWCOUNT 返回當前位置為止游標讀取的記錄行數。
四> 示例:
set serveroutput on;
declare
varno varchar2(20);
varprice varchar2(20);
CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no = vartype;
begin
if mycur%isopen = false then
open mycur(000627);
end if;
fetch mycur into varno,varprice;
while mycur%found
loop
dbms_output.put_line(varno||','||varprice);
if mycur%rowcount=2 then
exit;
end if;
fetch mycur into varno,varprice;
end loop;
close mycur;
end;
pl/sql 記錄 的結構和c語言中的結構體類似,是由一組數據項構成的邏輯單元。
pl/sql 記錄並不保存再資料庫中,它與變數一樣,保存再內存空間中,在使用記錄時候,要首先定義記錄結構
,然後聲明記錄變數。可以把pl/sql記錄看作是一個用戶自定義的數據類型。
set serveroutput on;
declare
type person is record
(
empno cus_emp_basic.emp_no%type,
empzc cus_emp_basic.emp_zc%type);
person1 person;
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
if mycur%isopen = false then
open mycur(000627);
end if;
loop
fetch mycur into person1;
exit when mycur%notfound;
dbms_output.put_line('雇員編號:'||person1.empno||',地址:'||person1.empzc);
end loop;
close mycur;
end;
典型游標for 循環
游標for循環示顯示游標的一種快捷使用方式,它使用for循環依次讀取結果集中的行
數據,當form循環開始時,游標自動打開(不需要open),每循環一次系統自動讀取
游標當前行的數據(不需要fetch),當退出for循環時,游標被自動關閉(不需要使用close)
使用游標for循環的時候不能使用open語句,fetch語句和close語句,否則會產生錯誤。
set serveroutput on;
declare
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
for person in mycur(000627) loop
dbms_output.put_line('雇員編號:'||person.emp_no||',地址:'||person.emp_zc);
end loop;
end;
H. SQL游標怎麼用
具體用法如下:
1、打開sqlservermanagementstudio,選中要操作的資料庫;
2、新建一個查詢;
3、聲明一個游標,以及一些需要用到的臨時變數;
4、使用游標,首先打開游標,並將游標下移一行;
5、循環取出數據,並進行相應的邏輯處理,其中的變數保存在@id和@name中;
6、每次循環的結束,要把游標下移;
7、要關閉游標,並釋放內存。
注意事項:游標名稱不能以@開頭。
I. Sql中的游標是幹嘛的
游標(cursor)是結果集的邏輯擴展,可以看做指向結果集的一個指針,通過使用游標,應用程序可以逐行訪問並處理結果集。
ResultSet對象用於接收查詢結果,next()方法用於判斷結果集是否為空,相當於指針,指向結果集下一個數據。
(9)sql讀取游標擴展閱讀:
游標的生命周期包含有五個階段:聲明游標、打開游標、讀取游標數據、關閉游標、釋放游標。
1、聲明游標語法
DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]
[ FORWARD_ONLY | SCROLL ]
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
[ TYPE_WARNING ]
FOR select_statement
[ FOR UPDATE [ OF column_name [ ,...n ] ] ]
2、打開游標語法
open [ Global ] cursor_name | cursor_variable_name
3、讀取游標數據語法
Fetch[ [Next|prior|Frist|Last|Absoute n|Relative n ]from ][Global] cursor_name[into @variable_name[,....]]
4、關閉游標語法
close [ Global ] cursor_name | cursor_variable_name
5、釋放游標語法
deallocate cursor_name