网络编程
位置:首页>> 网络编程>> 数据库>> plsql与tsql的语法不同

plsql与tsql的语法不同

 来源:asp之家 发布时间:2009-09-13 17:33:00 

标签:plsql,tsql,语法

insert into testtable(recordnumber,currentdate) values (i,sysdate);
print ‘';
select @i=@i+1;
end;
比较一下就可以看出来到底那里不一样了
plsql里面命令的结构为
delacre
定义语句段
begin
执行语句段
exception
异常处理语句段
end
这就是plsql程序总体结构图
定义变量与mssql的不同
基本方法
变量名 类型标识符【notnull】:=值
例 age number(8):=26
多了定义复合数据类型变量的功能
1.多了%type 变量
declare
mydate user。testtable.currentdate%type;
还有 %rowtype类型的变量可以识变量获得字段的数据类型,使用%rowtype可以识变量获得整个记录的数据类型。
变量名 数据表.列名%type
变量名 数据表%rowtype
declare
mytable testtbale%rowtype 包含了testtable 所有字段 只不过在输出时候可以选择输出那个
begin
shelect * into mytable
from temuuser.tedttbale
where recordnumber=88
dbms_output.put_line(mytable.currentdate);
end;
还有就是有了定义符合变量
格式
type 复合变量名 is record(
变量 类型, 可以有好几个);
变量名 复合变量名 这个变量名就好像java中类的对象一样而复合变量名就是类名可以这样理解 个人观点
begin
select * into 变量名 from 表名 where 条件
dbms_output.put_line(变量名.表中的值)
end
另外还可以定义一维数组
type 表类型 is table of 类型 index by binary_integer
表变量名 表类型
index by binary_integer子句代表以符号整数为索引,
这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”
Declare
type tabletype1 is table of varchar2(4) index by binary_integer;
type tabletype2 is table of tempuser.testtable.recordnumber%type index by
binary_integer;
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_integer;
table1 tabletype1;
begin
select * into table1(60)
from tempuser.testtable
where recordnumber=60;
dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate);
end;
在来看下面的这个程序
set serveroutput on
Declare
result integer;
begin
result:=10+3*4-20+5**2;
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. if..then..end if条件控制
if 条件 then
语句段;
end if;
2. if..then..else..end if条件控制
if 条件 then
语句段1;
else
语句段2;
end if;
3. if 嵌套条件控制
if 条件1 then
if 条件2 then
语句段1;
else
语句段2;
end if;
else
语句段3;
end if;
4.loop..exit..end loop 循环控制
loop
循环语句段;
if 条件语句 then
exit;
else
退出循环的处理语句段
end if;
end loop;
5. loop..exit..when..end loop 循环控制
采用 loop..exit..when..end loop 循环控制的语法结构与loop..exit..end loop 循环控制类似
exit when 实际上就相当于
if 条件 then
exit;
end if;
6.while..loop..end loop 循环控制
while 条件 loop
执行语句段
end loop;
7.for..in..loop..end 循环控制
for 循环变量 in [reverse] 循环下界..循环上界 loop
循环处理语句段;
end loop;
最后一个出个例子
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=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;

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com