《数据库系统概论》-Chapter8-数据库编程

SQL编程技术可以有效克服SQL语言实现复杂应用方面的不足,提高应用系统和数据库管理系统间的互操作性

 

8.1嵌入式SQL P245

 

8.2过程化SQL P253

 

8.3存储过程和函数 P255

 

8.4ODBC编程 259

 

《答案》例题P110

EXEC SQL SELECT Sno,Sname,Ssex,Sage,Sdept INTO: Hsno, : Hname, :Hsex, : Hage, :Hdept

FROM Student

WHERE Sno=:givesno

 

利用存储过程来实现下面的应用:从一个账户转指定数额的款项到另一个账户中

CREATE PROCEDURE TRANSFER(inAccount INT, outAccount INT, amount FLOAT)

AS DECLARE

totalDeposit FLOAT;

BEGIN

SELECT total INTO totalDeposit

FROM ACCOUNT WHERE

ACCOUNTNUM=outAccount;

IF totalDeposit IS NULL THEN

ROLLBACK;

RETURN;

END IF;

IF totalDeposit.total

ROLLBACK;

RETURN;

END IF;

UPDATE account SET total=toal-amount

WHERE ACCOUNTNUM=outAccount;

UPDATE account SET total=total+amount WHERE ACCOUNTNUM=inAccount;

COMMIT;

END;

 

查询某一门课程的信息。要查询的课程由用户在程序运行过程中指定,放在主变量中

EXEC SQL BEGIN DECLARE SECTION:

char deptname[20];

char Hsno[9];

char HSname[20];

char HSsex[2];

int Hsage;

int NEWAGE;

EXEC SQL END DECLARE SECTION;

long SQLCODE;

EXEC SQL INCLUDE sqlca;//定义SQL通信区

int main(void)

{

int count=0;

char yn;

printf("Please choose the department name:");

scanf("%s", &deptname);

EXEC SQL CONNECT TO 学生课程数据库@localhost:54321 USER

"SYSTEM"/"MANAGER";

EXEC SQL DECLARE SX CURSOR FOR /*游标*/

SELECT Sno, Sname, SSex, Sage

FROM Student

WHERE Sdept=: deptname;

EXEC SQL OPEN SX; /*打开游标*/

for(; ;)

EXEC SQL FETCH SX INTO: HSno, :HSname, :HSsex, :HSage;/*游标向前推进一行,取结果送主变量*/

if(sqlca, sqlcode !=0) break;

if(count++ =0)

printf("");

printf("");

printf("");

do{

scanf("%c" , &yn);

}while();

if(){

printf("");

scanf("");

EXEC SQL UPDATE Student

SET Sage=:NEWAGE

WHERE CURRENT OF SX;/*对Student表中的年龄进行更新*/

}

}

EXEC SQL COLSE SX;/*关闭游标*/

EXEC SQL COMMIT WORK;/*提交更新*/

EXEC SQL DISCONNECT 学生课程数据库

 

习题P270

你可能感兴趣的:(数据库,考研复试,Note)