错误!未指定书签。
$DELETE FROM book
WHERE book_num=$booknum;
2.6 PREPARE语句
功能:PREPARE语句显示地请求对SQL语句进行语法分析。 格式: PREPARE statement_name FROM string_spec 示例:
$PREPARE Del FROM }
while( … ) { }
…
$EXECUTE Del Using $book10; …
\
// SQLCODE=sqlca.sqlcode
printf( \exit( -1 ); if ( SQLCODE < 0 ) {
8
错误!未指定书签。
三 游标
3.1 滚动游标
3.1.1 DECLARE
DECLARE定义一个游标,它给游标命名并把一个查询同一个游标相关联。 格式:DECLARE cursor_name CURSOR FOR selectstmt 示例:
$declare C1 cursor for select xy1,xy2 into $xy1, $xy2 from fj;
3.1.2 OPEN
OPEN语句激活游标并开始执行相关联的SELECT语句 格式: OPEN cursor_name 示例:
$declare custptr scroll cursor for select * from customer where zipcode=$zip; $open custptr;
3.1.3 FETCH
FETCH语句把已打开的游标移到一个新的位置,并把记录的字段值存放到INTO子句说明的宿主变量中。
格式: FETCH [position] cursor_name [INTO HOSTVARLIST] 说明:
对于一个滚动游标,该新位置可以是活动集中的任意位置。 (1) 绝对位置
FIRST LAST
取得活动集中的第一个记录。 取得活动集中的最后一个记录。
9
错误!未指定书签。
CURRENT 取得当前记录。
ABSOLUTE n 取得活动集的第n个记录 (2) 相对位置
NEXT
取得活动集的下一个记录。 取得活动集的前一个记录。
PREVIOUS
RELATIVE n 取得相对当前记录的第n个记录,负值向前,正值向后数。
示例:
$declare custptr scroll cursor for select customer_num, lname, fname
from customer where zipcode = $zip;
$open custptr;
$fetch next custptr into $Cnum, $Lname, $Fname;
当游标成功地移动到指定位置且在该位置有记录时,则SQLCODE值为0,如果没有记录,则返回SQLNOTFOUND(100)。
3.1.4 CLOSE
CLOSE语句关闭游标。 格式: CLOSE cursor_name 示例:
关闭游标之后,除了OPEN和FREE语句之外,所有其它语句都不能再使用。
$declare custptr scroll for select cnum,lname,fname
from customer where zipcode=$zip;
$open custptr;
$fetch next custptr into $Cnum,$Lname,$Fname; $close custptr;
10
错误!未指定书签。
3.1.5 FREE
FREE语句释放空间和数据服务器中的游标资源。 格式:FREE cursor_name; 示例:
$declare custptr cursor for
select cnum, lname, fname from customer where zipcode=$zip;
$open custptr;
$fetch next custptr into $Cnum, $Lname, $Fname; $close custptr; $free custptr;
3.2 更新游标
更新游标只能查看下一条记录,主要在进行修改和删除操作时使用。 格式: DECLARE cursor_name CURSOR FOR select_statement
FOR UPDATE [OF column_list]
示例:
$declare mycurs cursor for
select manu_code, manu_name from manufact for update;
$open mycurs; for( ; ; ) {
$fetch mycurs into $mfcode, $mfname; …
if ( user_input == UPDATE ) {
$update manufact set
( manu_code, manu_name ) = ( $mfcode, $mfname ) where current of mycurs;
if ( user_input == DELETE ) {
$ delete from manufact where current of mycurs;
11
错误!未指定书签。
}
…
12