PL/SQL学习笔记五
游标是从数据库中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向首记录, 利用fetch语句移动该指针,从而对游标中的数据进行各种操作。
1.定义游标
cursor 游标名 is select语句;
2.打开游标
open 游标名;
3.提取游标数据
fetch 游标名 into 变量名1, 变量名2, ....;
或
fetch 游标名 into 记录型变量名;
4.关闭游标
close 游标名;
5.游标的属性
%isopen
该属性标识游标是否打开,如果没有打开游标就使用fetch语句将提示错误。
%isfound
该属性标识一个fetch语句是否有值,有值返回true,否则返回false。
%notfound
该属性与%isfound相反。
%rowcount
该属性用于获取游标的数据行数。
示例:
set serveroutput on
declare
tempsal scott.emp.sal%type;
cursor mycursor is
select * from scott.emp
where sal>tempsal;
cursorrecord mycursor%rowtype;
begin
tempsal:=800;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursorrecord;
dbms_output.put_line(to_char(cursorrecord.deptno));
dbms_output.put_line(to_char(mycursor%rowcount));
else
dbms_output.put_line('游标还未打开');
end if;
end;
相关文档:
C#中操作Oracle时的SQL语句参数的用法
OracleTransaction myTrans ;
conn.Open();
myTrans =conn.BeginTransaction(IsolationLevel.ReadCommitted) ......
1.获取所有数据库名:
SELECT Name from Master..SysDatabases ORDER BY Name
2.获取所有表名:
SELECT Name from DatabaseName..SysObjects Where XType='U' ORDER BY Name
XType='U':表示所有用户表;
XType='S':表示所有系统表;
3.获取所有字段名:
SELECT Name from SysColumns WHERE id=Object_Id('TableNam ......
SQL自动异地备份方法
方法一:
mssql数据库远程备份的job
/*在远程机器操作系统的计算机管理里建立一个用户名为kyle的用户,密码为1234,同时在那台机器的非系统盘里建一个名为backup的共享文件夹,为了安全另外设置这个文件夹只有这个kyle用户可以访问。*/
declare @sql varchar(500)
select @sql='\\10.2. ......
--创建链接服务器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用户名 ', '密码 '
--查询示例
select * from ITSV.数据库名.dbo.表名
--导入示例
select * into 表 from ITSV.数据库名.dbo.表名
--以后不 ......
1.条件控制
1.1 if .. then .. end if
if 条件 then
语句段;
end if;
1.2 if .. then .. else .. end if
if 条件 then
语句段;
else
语句段;
end if;
1.3 if嵌套
2.循环控制
2.1 loop .. exit .. end loop
loop
& ......