当前位置:首页 » 编程语言 » 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;