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

sql游標怎麼用if

發布時間: 2023-01-06 16:48:03

『壹』 求教,sql的if如何使用

SQLServerIF用法:
IF條件表達式
BEGIN
SQL語句
END
ELSE
BEGIN
SQL語句
END
設置一個變數@sex,如果@sex為』F』,
那麼查詢Employees中所有TitleOfCourtesy為』Ms.』或
』Mrs.』的所有記錄;如不是,查詢剩下的紀錄。

declare@sexchar(1)
set@sex='M'
if@sex='F'
begin
select*fromEmployees
whereTitleOfCourtesy='Ms.'
orTitleOfCourtesy='Mrs.'
end
else
begin
select*fromEmployees
whereTitleOfCourtesy<>'Ms.'
andTitleOfCourtesy<>'Mrs.'
end

『貳』 游標的詳細用法

declare @id int\x0d\x0adeclare @name varchar(50)\x0d\x0adeclare cursor1 cursor for --定義游標cursor1\x0d\x0aselect * from table1 --使用游標的對象(跟據需要填入select文)\x0d\x0aopen cursor1 --打開游標\x0d\x0afetch next from cursor1 into @id,@name --將游標向下移1行,獲取的數據放入之前定義的變數@id,@name中\x0d\x0awhile @@fetch_status=0 --判斷是否成功獲取數據\x0d\x0abegin\x0d\x0aupdate table1 set name=name+'1'\x0d\x0awhere id=@id --進行相應處理(跟據需要填入SQL文)\x0d\x0afetch next from cursor1 into @id,@name --將游標向下移1行\x0d\x0aend\x0d\x0aclose cursor1 --關閉游標\x0d\x0adeallocate cursor1

『叄』 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;

『肆』 SQL中如何使用IF語句

SQL中的if語句與偽代碼的寫法很相似,即:

IF (條件) then
執行語句體
END IF;

舉例:
begin
if 1 > 0 then
dbms_output.put_line('1>0');
end if;
end;

『伍』 SQL中游標的用法

在倒數第3行上面加上:fetch next from pcurr into @ID,@Sex
即:
......
set @Sexs = '男'
begin
update Person set Sex = @Sexs where ID = @ID
end
fetch next from pcurr into @ID,@Sex
end
close pcurr
deallocate pcurr

『陸』 sql游標的用法

游標再怎麼變也不過是在循環遍歷一個集合.

你說的太抽象了.

順便提一下, SQL這種面向集合的語言, 集合操作才能體現高效

游標這種循環的東西, 都是被逼到絕路上才用的.

『柒』 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

『捌』 SQL游標如何使用

A. 在簡單的游標中使用 FETCH
下例為 authors 表中姓以字母 B 開頭的行聲明了一個簡單的游標,並使用 FETCH NEXT 逐個提取這些行。FETCH 語句以單行結果集形式返回由 DECLARE CURSOR 指定的列的值。

USE pubs
GO
DECLARE authors_cursor CURSOR FOR
SELECT au_lname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname

OPEN authors_cursor

-- Perform the first fetch.
FETCH NEXT FROM authors_cursor

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

au_lname
----------------------------------------
Bennet
au_lname
----------------------------------------
Blotchet-Halls
au_lname
----------------------------------------

B. 使用 FETCH 將值存入變數
下例與上例相似,但 FETCH 語句的輸出存儲於局部變數而不是直接返回給客戶端。PRINT 語句將變數組合成單一字元串並將其返回到客戶端。

USE pubs
GO

-- Declare the variables to store the values returned by FETCH.
DECLARE @au_lname varchar(40), @au_fname varchar(20)

DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname, au_fname

OPEN authors_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

-- Concatenate and display the current values in the variables.
PRINT "Author: " + @au_fname + " " + @au_lname

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

Author: Abraham Bennet
Author: Reginald Blotchet-Halls

C. 聲明 SCROLL 游標並使用其它 FETCH 選項
下例創建一個 SCROLL 游標,使其通過 LAST、PRIOR、RELATIVE 和 ABSOLUTE 選項支持所有滾動能力。

USE pubs
GO

-- Execute the SELECT statement alone to show the
-- full result set that is used by the cursor.
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname

-- Declare the cursor.
DECLARE authors_cursor SCROLL CURSOR FOR
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname

OPEN authors_cursor

-- Fetch the last row in the cursor.
FETCH LAST FROM authors_cursor

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM authors_cursor

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM authors_cursor

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM authors_cursor

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM authors_cursor

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

au_lname au_fname
---------------------------------------- --------------------
Bennet Abraham
Blotchet-Halls Reginald
Carson Cheryl
DeFrance Michel
del Castillo Innes
Dull Ann
Green Marjorie
Greene Morningstar
Gringlesby Burt
Hunter Sheryl
Karsen Livia
Locksley Charlene
MacFeather Stearns
McBadden Heather
O'Leary Michael
Panteley Sylvia
Ringer Albert
Ringer Anne
Smith Meander
Straight Dean
Stringer Dirk
White Johnson
Yokomoto Akiko

au_lname au_fname
---------------------------------------- --------------------
Yokomoto Akiko
au_lname au_fname
---------------------------------------- --------------------
White Johnson
au_lname au_fname
---------------------------------------- --------------------
Blotchet-Halls Reginald
au_lname au_fname
---------------------------------------- --------------------
del Castillo Innes
au_lname au_fname
---------------------------------------- --------------------
Carson Cheryl

『玖』 sql內嵌游標怎麼插if

在第一個游標循環的end前插入空行

『拾』 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;