强大灵活的语言分析
达梦数据库对数据库的操作提供了灵活语言支持。达梦不仅支持功能强大的PL/SQL,还支持类C语言的BLOCK语句块。
1. PLSQL
PL/SQL是达梦数据库的过程化语言,包括一整套的数据类型、条件结构、循环结构和异常处理结构,PL/SQL可以执行SQL语句,SQL语句中也可以使用PL/SQL函数。PL/SQL支持所有SQL数据类型和所有SQL函数。支持游标、%ROWTYPE、%TYPE、EXCEPTION和数组类型,同时支持所有达梦对象类型。例如:
declare
cursor cc(x int) is select id, name from sysobjects where id < x;
r cc%rowtype[];
cnt int;
k cc%rowtype;
begin
open cc(20);
cnt = 10;
r = new cc%rowtype[cnt];
for i in 1..cnt loop
fetch cc into r[i];
end loop;
for i in 1..9 loop
for j in i + 1..10 loop
if r[i].name > r[j].name then
k = r[i];
r[i] = r[j];
r[j] = k;
end if;
end loop;
end loop;
for i in 1..cnt loop
print r[i].id;
print r[i].name;
end loop;
close cc;
end;
/
cursor cc(x int) is select id, name from sysobjects where id < x;
r cc%rowtype[];
cnt int;
k cc%rowtype;
begin
open cc(20);
cnt = 10;
r = new cc%rowtype[cnt];
for i in 1..cnt loop
fetch cc into r[i];
end loop;
for i in 1..9 loop
for j in i + 1..10 loop
if r[i].name > r[j].name then
k = r[i];
r[i] = r[j];
r[j] = k;
end if;
end loop;
end loop;
for i in 1..cnt loop
print r[i].id;
print r[i].name;
end loop;
close cc;
end;
/
2. BLOCK语句块
达梦数据库提供了类C语言的BLOCK语句块,支持C语言的基本数据类型,支持C语言格式的条件结构、循环结构和异常处理结构,GOTO等结构,BLOCK语句块中支持SQL语句和函数。另外BLOCK语句块中还支持引用游标、%ROWTYPE、%TYPE、EXCEPTION、数组类型和记录类型。例如:
{
sysobjects%rowtype r;
sysobjects%rowtype y[];
ref cursor cc;
int i;
y = new sysobjects%rowtype[10];
open cc for 'select * from sysobjects where id < 10';
i = 1;
do
{
fetch cc into r;
if(cc%found)
{
y[i] = r;
i++;
}
}
while(cc%found);
for (i = 1; i <= 10; i ++)
{
r = y[i];
print r.id;
print r.name;
}
}
/
sysobjects%rowtype r;
sysobjects%rowtype y[];
ref cursor cc;
int i;
y = new sysobjects%rowtype[10];
open cc for 'select * from sysobjects where id < 10';
i = 1;
do
{
fetch cc into r;
if(cc%found)
{
y[i] = r;
i++;
}
}
while(cc%found);
for (i = 1; i <= 10; i ++)
{
r = y[i];
print r.id;
print r.name;
}
}
/