drop table person
go
create table person
(
NUM INTEGER,
NAME VARCHAR(20),
AGE INTEGER,
RANK INTEGER,
COMMENT VARCHAR(100)
)
go
describe select * from person
go
--存储过程
create or replace procedure insertBatch(num integer,age integer,rank integer) --默认为in
language sql
begin
declare name varchar(20);
declare comment varchar(100);
declare num integer;
declare age integer;
declare rank integer;
set num = 1;
set age = 10;
set rank = 100;
while num <= 9
do
set name = 'gaoke'||num;
set comment = 'cccccc'||num;
insert into person values(num,name,age,rank,comment);
set num = num+1;
set age = age+1;
set rank = rank + 1;
end while;
end
go
--调用存储过程
call insertBatch (1,10,100)
go
commit
go
select * from person
go
--创建一个相关表
drop table update_count
go
create table update_count
(
id integer,
count integer
)
go
insert into update_count values(1,0)
go
select * from update_count
go
--函数
create or replace function afun()
returns integer
begin
return
(select count from update_count where id = 1);
end
go
--触发器
create or replace trigger atrigger
after update of age on person
referencing new as nnn old as ooo
for each row mode db2sql
begin atomic
update update_count set count = count+1 where id = 1;
end
go
--测试用,类似pl/sql的代码段,游标的使用(年龄大于12的人在原来的年龄上依次加1、2、3、4……)
begin
declare id integer;
declare n integer;
declare i integer;
declare step integer;
declare sqlcode integer default 0;
set i = 1;
begin
declare cur cursor for select num,age from person;
open cur;
cursorLoop:
loop
fetch cur into id,n;
if sqlcode = 100
then leave cursorLoop;
else
if n > 12
then
set step = n + i;
update person set age = step where num = id ;
set i = i + 1;
end if;
end if;
end loop;
close cur;
end;
end
go
select * from person
go
select afun() from update_count
go
分享到:
相关推荐
731考试会测试考生在应用开发中使用DB2的能力,包括SQL编程、存储过程、触发器、游标、UDF(用户定义函数)以及JDBC和ODBC连接。此外,还可能涵盖XML支持、SQL Joins优化、并发控制和异常处理等方面。 学习这些认证...
11. **存储过程和触发器**:编写和调用存储过程以执行复杂的业务逻辑,以及了解触发器在自动执行特定操作时的作用。 12. **数据库备份与恢复**:了解DB2的备份策略,如何进行数据库备份和恢复,以确保数据安全。 ...
10. **存储过程和触发器**:存储过程是一组预编译的SQL语句,可以在需要时调用,提高效率并简化代码。触发器则是在特定数据库事件(如INSERT、UPDATE或DELETE)发生时自动执行的代码。 11. **安全性与权限**:DB2...
4. **存储过程和触发器**:学习编写和管理存储过程,这是封装复杂逻辑的数据库对象。同样,了解触发器的用途,它们是在特定数据库操作前或后自动执行的代码段,可以用于实现业务规则。 5. **游标和事务管理**:掌握...
MySQL的SQL语法简洁明了,易于学习,但同时也提供了高级特性,如存储过程、触发器和视图。书中将详细讲解MySQL的安装、配置、数据类型、表结构设计,以及如何使用SQL进行数据操作和查询优化。 SQL Server 2000是...
内容覆盖了数据检索、排序、过滤、分组、表和视图的创建及管理、子查询、联结、存储过程、游标、触发器、数据类型、数据库安全等数据库操作的各个方面。作者特别强调了SQL的实践性,为读者提供了大量实例,确保了...
3. 掌握PL/SQL语言,用于进行数据库编程,包括游标、存储过程、存储函数、包和触发器的应用。 4. 学习简单的数据库管理,包括权限分配、角色设置以及日常维护任务。 5. 了解Oracle与其他主流数据库(如Access、SQL ...
3. **存储过程(Stored Procedure)**:存储过程是预编译好的SQL代码块,可以接受输入参数,返回结果集或值。使用存储过程可以提高应用程序的性能和安全性。 4. **游标(Cursor)**:游标允许程序逐行处理查询结果集中...