存储过程使用tsql:plsql和tsql的语法区别来源: 发布时间:星期四, 2009年9月10日 浏览:201次 评论:0
insert o testtable(recordnumber,currentdate) values (i,sysdate);
pr ‘'; select @i=@i+1; end; 比较下就可以看出来到底那里不样了 plsql里面命令结构为 delacre 定义语句段 begin 执行语句段 exception 异常处理语句段 end 这就是plsql总体结构图 定义变量和mssql区别 基本思路方法 变量名 类型标识符【notnull】:=值 例 age number(8):=26 多了定义复合数据类型变量功能 1.多了%type 变量 declare mydate usertesttable.currentdate%type; 还有 %rowtype类型变量可以识变量获得字段数据类型使用%rowtype可以识变量获得整个记录数据类型 变量名 数据表.列名%type 变量名 数据表%rowtype declare mytable testtbale%rowtype 包含了testtable 所有字段 只不过在输出时候可以选择输出那个 begin shelect * o mytable from temuuser.tedttbale where recordnumber=88 dbms_output.put_line(mytable.currentdate); end; 还有就是有了定义符合变量 格式 type 复合变量名 is record( 变量 类型 可以有好几个); 变量名 复合变量名 这个变量名就好像java中类对象样而复合变量名就是类名可以这样理解 个人观点 begin select * o 变量名 from 表名 where 条件 dbms_output.put_line(变量名.表中值) end 另外还可以定义维 type 表类型 is table of 类型 index by binary_eger 表变量名 表类型 index by binary_eger子句代表以符号整数为索引 这样访问表类型变量中数据思路方法就是“表变量名(索引符号整数)” Declare type tabletype1 is table of varchar2(4) index by binary_eger; type tabletype2 is table of tempuser.testtable.recordnumber%type index by binary_eger; table1 tabletype1; table2 tabletype2; begin table1(1):='大学'; table1(2):='大专'; table2(1):=88; table2(2):=55; dbms_output.put_line(table1(1)||table2(1)); dbms_output.put_line(table1(2)||table2(2)); end; 个标准维 定义多维表类型变量 定义了名为 tabletype1 多维表类型相当于多维table1 是多维表类型变量将数据表 tempuser.testtable 中 recordnumber为 60 记录提取出来存放在 table1 中并显示 type tabletype1 is table of testtable%rowtype index by binary_eger; table1 tabletype1; begin select * o table1(60) from tempuser.testtable where recordnumber=60; dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate); end; 在来看下面这个 serveroutput _disibledevent=>dbms_output.put_line('运算结果是:'||to_char(result)); end; || 这个符号是连接语句 to_char(result) dbms_output.put_line输出只能是串因此利用 to_char将数值型结果转换为型 To_char:将其他类型数据转换为型 To_date:将其他类型数据转换为日期型 To_number:将其他类型数据转换为数值型 再说下plsql中控制语句组合有哪几种 1. ..then..end 条件控制 条件 then 语句段; end ; 2. ..then....end 条件控制 条件 then 语句段1; 语句段2; end ; 3. 嵌套条件控制 条件1 then 条件2 then 语句段1; 语句段2; end ; 语句段3; end ; 4.loop..exit..end loop 循环控制 loop 循环语句段; 条件语句 then exit; 退出循环处理语句段 end ; end loop; 5. loop..exit..when..end loop 循环控制 采用 loop..exit..when..end loop 循环控制语法结构和loop..exit..end loop 循环控制类似 exit when 实际上就相当于 条件 then exit; end ; 6.while..loop..end loop 循环控制 while 条件 loop 执行语句段 end loop; 7.for..in..loop..end 循环控制 for 循环变量 in [reverse] 循环下界..循环上界 loop 循环处理语句段; end loop; 最后个出个例子 serveroutput _disibledevent=>number2 eger:=90; i eger:=0; begin for i in 1..10 loop number1:=number1+1; 在mssql里是 sclect @number=@number+1 end loop; dbms_output.put_line('number1值:'||to_char(number1)); end; 本人学java 对plsql看觉很简单 和java比起来简单多了但是oracle 命令只是部分更多东西需要我去学习 自夸下 哈哈 在plsql 多了事务处理命令 commit命令 commit事务提交命令在oracle中为了保证数据致性在内存中将为每个客户机建立工作区就是说在用commit命令的前操作都在这个工作群里完成只有在用commit命令的后才会把你写命令写入到数据库中 有个自动进行事务提交命令 auto _disibledevent=>open mycursor; 打开这个游标 end; 晕忘了 只是打开游标没有用 还要提取游标数据 用fetch命令 fetch 游标名 o 变量1变量2; 或者 fetch 游标名 o 记录型变量名; 上面那个要改下 serveroutput _disibledevent=>open mycursor; 打开这个游标 fetch mycursor o ; 读取游标数据把他添加到中 dbms_output._line(to_char(.sal)); 显示结果 close mysursor; close关闭这个游标 end; 游标属性 1.%isopen属性 就是判断游标是否打开如果没有打开就是用fetch语句提示 2.%found属性 就是测试前个fetch语句是否有值有就返回true 没有就返回false 3.%notfound属性 和上面相反 4.%rowcount属性 判断游标数据行数就是有多少数据 下面说下过程概念 sql里没有 完整过程结构如下 create or replace 过程名 as 声明语句段; begin 执行语句段; exception 异常处理语句段; end; 过程是有名称块as关键词代替了无名块declare 创建例子过程 创建个名为tempprocdeure过程create是创建过程标识符replace表示如果又同名过程将覆盖原过程定义了个变量其类型何testtable数据表中currentdate字段类型相同都是日期型将数据表中recordnumber字段为88 currentdate字段内容送入变量中然后输出结果 serveroutput _disibledevent=>dbms_output.put_line(to_char(tempdate)); end; 使用过程 serveroutput _disibledevent=>select danme o danme1 from scott.dept where deptno=tempdeptno; temploc:='地址'||loc1; tempdname:='姓名'||dname1; end; 定义好了 下面开始用了 serveroutput _disibledevent=>mydname:=”; myloc:=”; scott.tempprocedure(myno,mydname,myloc); dbms_output.put_line(myno); dbms_output.put_line(mydname); dbms_output.put_line(myloc); end; 搞定了 就是说用重新定义 3个变量当参数传递给上面定义过程过程里带参数变量可以接受这 3个变量值 用java语言来解释就是那个过程就是类 带 3个参数 这 3个变量就是数据 当然没有对象了哈哈毕竟不是java么哈哈 今天写到这里了 我要下班了 7.3 异常处理 就是中要处理特殊情况办法 1. 定义异常处理 定义异常处理语法如下: declare 异常名 exception; 2. 触发异常处理 触发异常处理语法如下: raise 异常名; 3. 处理异常 触发异常处理后可以定义异常处理部分语法如下: Exception When 异常名 1 then 异常处理语句段 1; When 异常名 2 then 异常处理语句段 2; 下面 PL/SQL 包含了完整异常处理定义、触发、处理过程定义名为 salaryerror 异常在 scott.emp 数据表中查找 empno=7566 记录将其值放入变量 tempsal 中判断 tempsal 值若不在 900 和2600 的间介绍说明该员工薪水有问题将激活异常处理提示信息 serveroutput _disibledevent=> tempsal <900 or tempsal>2600 then raise salaryerror; end ; exception when salaryerror then dbms_output.put_line('薪水超出范围'); end; 0
相关文章
读者评论发表评论 |