惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
月光博客
月光博客
Jina AI
Jina AI
F
Fortinet All Blogs
博客园 - 聂微东
The Cloudflare Blog
美团技术团队
B
Blog RSS Feed
N
Netflix TechBlog - Medium
罗磊的独立博客
The GitHub Blog
The GitHub Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
V
V2EX

博客园 - 博闻李

ASP.NET AJAX中PageMethods方法调用示例 - 博闻李 - 博客园 SQL Server数据库所有表索引性能分析 SQL Server数据库所有表重建索引 查询数据库字段所在的位置 查询时锁的使用[转] DBCC DBREINDEX重建索引提高SQL Server性能[引] Javascript键盘输入相关 - 博闻李 - 博客园 VS2008 Web Application和Web Site的区别[转] IIS Temporary ASP.NET Files拒绝访问解决方案 WebDAV是什么?[引] ASP.NET C# 访问Oracle数据库示例 VSTS 2008的TFS安装实战[转] Oracle PL/SQL常用函数列表[转载] SQL Server 索引结构及其使用(一)[转] SQL数据库设计经验[转] 一套外企的数据库设计面试题[转] UML在关系型数据库设计中的应用[转] 数据库设计范式深入浅出[转] showModalDialog和showModelessDialog使用[转]
Oracle初学笔记
博闻李 · 2009-03-08 · via 博客园 - 博闻李

这里主要记录使用PL/SQL Developer 7.1.4操作Oracle 9i的一些基本内容:

1.存储过程

(1)先创建包并声明存储过程

create or replace package BowenPKG is
       type refcursor is ref cursor;

       procedure Getdata(refout out refcursor);
       procedure GetdataById(rid in number,refout out refcursor);

end BowenPKG;

(2)创建包体和存储过程

create or replace package body BowenPKG is
 
  Procedure Getdata(refout out refcursor)as
  Begin
            Open refout for
            select * from tb;      
  End Getdata;
 
  Procedure GetdataById(rid in number,refout out refcursor)as
  Begin
            Open refout for
            select * from tb where id=rid;           
  End GetdataById;
   
end BowenPKG;

2.游标

这里是使用游标的2个小例子,使用了不同的PL/SQL分支语句控制:

(1)While语句

代码示例

(2) Loop语句

代码示例

3.自增列

自增列的实现,Oracle与SQL Server不同。SQL Server是通过设置表字段属性实现的;Oracle是通过序列(Sequence)实现的。每个序列只针对一个字段。

(1)创建

创建序列的语句是:

create sequence TTBID
minvalue 1
maxvalue 9999999999999
start with 33
increment by 1
cache 32
order;

在PL/SQL Developer中的操作是:在【浏览器】(broswer)的项目列表中,在【序列】(Sequence)右键属性里选择【新建】,然后在出现的界面上设置即可。

(2)使用

插入数据:

insert into ttb values(ttbid.nextval,'ddddddddddddddd');
commit;

返回序列值:

declare
  ln number;
begin
  insert into  ttb (id)  values (ttbid.nextval) ;
  returning id into ln;
  commit;
end;

insert into  ttb values (ttbid.nextval,'ddddddddddddddd') ; 
select ttbid.currval from dual;
commit;

4.触发器

触发器既可以手写语句创建,也可以使用PL/SQL Developer辅助创建。

以下示例是为一个表的自增字段创建触发器,从而实现类似SQL Server中的自增字段数据插入效果(可以忽略自增字段字段)。

create or replace trigger tri_ttb
  before insert on ttb
  for each row
begin
  select ttbid.nextval into :new.tid from dual;
end tri_ttb;

使用示例:

Insert into ttb(tname) values('test context');
Commit;

在实践中发现,触发器不仅可以在insert或update中触发,还可以被多个操作触发,使用 or 关键字联接即可。如下: 

create or replace trigger tri_ttb
  before insert or update on ttb
  for each row
begin
  select ttbid.nextval into :new.tid from dual;
 end tri_ttb;

5.备份还原数据库