您的位置:首页 > 数据库 > Oracle

oracle的一些积累

2008-06-30 08:39 148 查看
最近一直在跟数据库打交道,用的还是自己不常用的oracle,期间问题到是碰到了不少,不过问题都已经解决了!又好好的充实了一把。

--例如:

首先创建一张表

create table TRAINTABLE --(列车表)

(

TRAINID NUMBER(10) not null primary key,-- 列车编号 PK 自动增长列

ARRIVETIME DATE not null,-- 过车时间



--如何创建自增列(oracle中不能像sql中那样用identity一样创建自动增长列,因为他本身表中会创建一个rownum)

create seq_triaintable start with 1 minvalue 1 maxvalue 9999999 increnment by 1

创建按一个序列名为seq_triaintable 以1开始增长最小值为1最大值为9999999每次增长值为1

--如何用此序列

sqe_traintable.nextval下一个值

seq_traintable.currentval当前值

--创建存储过程(增删改)

--通过TRAINID更新数据

create or replace procedure pro_update_info_traintable(ID_ number,time_ date)

as

ID_temp varchar2(10);

begin

select trainid into ID_temp from TrainTable where trainid = ID_ for update of ARRIVETIME ; ---锁定你要更新的那行数据

update traintable set ARRIVETIME=to_date( time_,'YYYY-MM-DD HH24:MI:SS') where trainid=ID_;

exception --异常处理

when no_data_found

then dbms_output.put_line('没有此条数据!');

end pro_update_info_traintable;

to_date(参数,'YYYY-MM-DD HH24:MI:SS')是对日期的一种转换

---添加一行数据

create or replace procedure Pro_Insert_Info_traintable(ID_ number,time_ date)

as

begin

insert into T_RYGL traintable(seq_T_traintable.nextval,to_date(time_,'YYYY-MM-DD HH24:MI:SS'));

end Pro_Insert_Info_traintable;

begin

Pro_Insert_Info_traintable('参数','参数');

end ;

--删除一行数据

create or replace procedure Pro_Delete_Info_traintable(ID_ number)

as

begin

delete from Pro_Insert_Info_traintable where trainid = ID_;

end;

--------在oracle中查询的结果并不能返回一个结果集这就导致在填充数据的时候没有值,解决的方法句是用游标

(这个地方我就花了好长时间去研究,老是认为自己的语句写错了!!!



--查询TRAINTABLE表中的所有数据

create or replace package Pac_Select_Info_Traintable is

type mycursor is ref cursor;

procedure Pro_Select_Info_Traintable(ret_cursor out mycursor);

end Pac_Select_Info_Traintable;

create or replace package body Pac_Select_Info_Traintable is

procedure Pro_Select_Info_Traintable(ret_cursor out mycursor) as

begin

open ret_cursor for select * from TRAINTABLE;

end Pro_Select_Info_Traintable;

end Pac_Select_Info_Traintable;

在前台的页面中好多地方都时要用到datalist,datagrid之类的东西,我就写了好多类似与上面的查询语句的程序包来进行数据填充 现在想起来当时自己真的很笨哦

,这些存储过程,难道就不能写在一起封装起来,就像在逻辑代码一样,写一个公共的类里然后面写不同的方法通过类名进行调用。答案当然是肯定的。后来又看了一些书,将写好的存储过程,触发器,之类的东西进行封装成程序包,是数据库优化的一部分。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: