tzbl
本人想要做一个网上书店系统,所有功能尽量用开源框架,希望可以得到大家的帮助和支持,我的开发经历也会纪录在这里的。
posts - 4,  comments - 0,  trackbacks - 0

// 日期格式化:

to_date('2006-11-20 16:35:00','yyyy-mm-dd hh24:mi:ss')

 

// 取子串,第二个参数表示从原串的何处起取,第三个参数指子串长度:

substr('abcdef',1,4)

substr('ab',1,4)= 'ab'

 

// 日期截取,即截取时间,保留时间部分:

trunc(sysdate)

 

// 日期的加减:

trunc(sysdate)-1  前一天

trunc(sysdate)-1/(24*60*60)  前一秒

to_number(trunc(sysdate)-begintime)*1440  计算时间差,单位:分钟

 

// 四舍五入:

round(2.555)=2

round(2.555,2)=2.56

 

//case 语句:

case when sysdate>MyTable.cleartime

then ……

else

……

end

 

case MyTable.objid when 'hmy'

then ……

else

……

end

 

//decode 函数,类似于三元运算符:

decode(MyTable.price,null,0, MyTable.price)  如果 price null ,返回 0

具体的语法格式如下:

DECODE(input_value,value,result[,value,result…][,default_result]);

 

// 常见的比率计算示例:

Select to_char(round(sum(decode(isactive,0,1,0))/count(isactive)*100,2))||'%' from ……

 

// 在字符串类型数据判等时,如果是 char nchar ,注意多余的空格

 

// 判断交集:

如何判断两个时间段有交集, starttime_1= = =endtime_1 starttime_2= = =endtime_2

where least(endtime_1, endtime_2) >greatest(starttime_1, starttime_2)

 

// 打印输出

dbms_output.put_line(' …… ');

 

// 存储过程:

(1) 从最简单的存储过程例子开始:

create or replace procedure MyProc

as

begin

  delete from MyTable;

end;

// oracle 中可以使用 replace ,方便多了。 as 亦可用 is

 

(2) 带上参数:

create or replace procedure MyProc(p_date in date)

as

begin

  delete from MyTable where starttime= p_date;

end;

//in 可省去,如有输出参数,应用 out.

 

(3) 声明变量:

create or replace procedure MyProc

as

v_count number:=0;

begin

select count(*) into v_count from MyTable;

if v_count>0 then

…… ;

end if;

end;

// 变量声明不用 declare 。变量可以指定默认值。

( PL/SQL 程序”又称“ PL/SQL 块”,“ PL/SQL 块”分为过程,函数,触发器等,“ PL/SQL 块”又分为“带名块”和“匿名块”。“带名块”就是有名字的程序,如上面存储过程“ MyProc ”,“匿名块”就是没有名字的程序,“带名块”里面以 is/as 开始定义变量,“匿名块”里面以 declare 开始来定义变量 )

 

(4) 声明游标:

create or replace procedure MyProc

as

cursor cursor_1 is

select objid,starttime from MyTable;

c1_row cursor_1%ROWTYPE;

c1_objid MyTable.objid%TYPE;

c1_starttime MyTable.starttime%TYPE;

 

begin

  open cursor_1;

fetch cursor_1 into c1_objid,c1_starttime;

loop

fetch cursor_1 into c1_row;

exit when cursor_ 1%notfound;

  if c1_row.starttime< c1_starttime then

    ……

  end if;

end loop;

close cursor_1

end;

// 使用 %TYPE 可以省去查看列的数据类型的麻烦

// 可以使用 %ROWTYPE 来在游标中表示一行记录。

// 游标中常用的属性: %notfound %found

 

(5) 动态游标:

create or replace procedure MyProc

as

v_id varchar2(20);

 

type v_cursor is ref cursor;

cursor_1 v_cursor;

c1_objid MyTable.objid%TYPE;

c1_starttime MyTable.starttime%TYPE;

 

begin

  v_id:= …… ;

  open cursor_1 for 'select objid,starttime from MyTable where objid=''' || v_id || '''';

……

close cursor_1;

end;

// 游标的语句可以动态指定

 

// 调试存储过程

不同数据库中对存储过程的调试都是个令人头痛的问题, oracle 中如欲调试存储过程,最好有辅助软件,如 pl/sql developer 。在 pl/sql developer 可按以下步骤对存储过程进行调试:

一、定位你的 procedure

1 在屏幕左边的对象浏览器中展开 procedure

2 找到你的 procedure

二、打开测试窗口

1 在你的 procedure 上点击右键

2 在弹出的菜单中选择 test

3PL/SQL Devoloper 就会打开一个测试窗口并自动生成一个调用块

4 在测试窗口的下方输入你的 procedure 的入口参数

三、打开编辑窗口

1 在你的 procedure 上点击右键

2 在弹出的菜单中选择 edit

3PL/SQL Devoloper 就会打开编辑窗口

4 在需要的地方设置断点

四、开始调试

1 回到调试窗口

2 打开 debug 菜单,你就会看见 start 项已经可以用了,点击它

3 接下来就可以用调试窗口上的按钮进行单步跟踪

 

如果在调试过程中提示“ no debug information ”,则右键相应的存储过程,选中“ add debug information ”。

 

// 设置定时任务

variable n number;

begin

dbms_job.submit(:n,'MyProc;',sysdate,'sysdate+1/1440');

commit;

end;

// 定时执行 MyProc 存储过程,第隔一分钟执行一次。变量 n 用于返回该任务在系统中的编号 ( 唯一标识 ) ,你可以用语句将它打印出来。

// 如果存储过程有参数,则第二个参数改作类似: 'MyProc(10);' 即可。

( 使用 Submit() 过程,工作被正常地计划好。
这个过程有五个参数: job what next_date interval no_parse

PROCEDURE Submit ( job OUT binary_ineger,
What IN varchar2,
next_date IN date,
interval IN varchar2,
no_parse IN booean:=FALSE)

job
参数是由 Submit() 过程返回的 binary_ineger 。这个值用来唯一标识一个工作。
what
参数是将被执行的 PL/SQL 代码块。
next_date
参数指识何时将运行这个工作。
interval
参数何时这个工作将被重执行。
no_parse
参数指示此工作在提交时或执行时是否应进行语法分析 ——TRUE
指示此 PL/SQL 代码在它第一次执行时应进行语法分析,
FALSE 指示本 PL/SQL 代码应立即进行语法分析 )

 

begin

dbms_job.remove(21);

end;

// 移除指定编号的任务

 

begin

dbms_job.run(21);

end;

// 手动执行一次指定编号的任务

 

select job,next_date,next_sec,failures,broken from user_jobs;

// 获取系统中所有的任务信息


http://www.cnblogs.com/KissKnife/services/trackbacks/602140.aspx


只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜