第11讲3--游标Cursor.pdf
An Introduction to Database System Video 11-3?Cursor An Introduction to Database System ? ? ? ? ?SQL? ? ? ? ? ?SQL? ? An Introduction to Database System ? ? ? ? SQL? ? SQL?SQL? ? ? ? ? ?SQL? ? ? ?SQL? An Introduction to Database System ?SQL? ?SQL? ?SELECT? ?CURRENT?UPDATE? ?CURRENT?DELETE? An Introduction to Database System 1. ?SELECT? ? ?1? ?2? ?3? ?4? An Introduction to Database System ?1? ? ?DECLARE? ? ? EXEC SQL DECLARE CURSOR FOR ; ? ? ?SELECT? ?select? ? An Introduction to Database System ?2? ? ?OPEN? ? ? EXEC SQL OPEN ; ? ? ?SELECT? ? ? An Introduction to Database System ?3? ? ?FETCH? ? ? EXEC SQL FETCH INTO ,.; ? ? ? ? An Introduction to Database System ?4? ? ?CLOSE? ? ? EXEC SQL CLOSE ; ? ? ? ? ? ? ? An Introduction to Database System 2. CURRENT?UPDATE/DELETE? ? ?CURRENT?UPDATE?DELETE? ? ? ? ?CURRENT?UPDATE?DELETE? ? ? An Introduction to Database System CURRENT?UPDATE/DELETE? ? CURRENT?UPDATE?DELETE? ? ? ? ?SELECT? ? ?CURRENT?UPDATE?DELETE? ? ?UPDATE?DELETE? WHERE CURRENT OF ? ? An Introduction to Database System CURRENT?UPDATE?DELETE? ? ?CURRENT?UPDATE?DELETE? ? ?SELECT?UNION?ORDER BY? ? ?SELECT? ? ? ? An Introduction to Database System 3. ? ? ?8.1 ? EXEC SQL BEGIN DECLARE SECTION; /*?*/ char Deptname20; char Hsno9; char Hsname20; char Hssex2; int HSage; int NEWAGE; EXEC SQL END DECLARE SECTION; /*?*/ long SQLCODE; EXEC SQL INCLUDE SQLCA; /*?SQL?*/ An Introduction to Database System ? int main(void) /*C?*/ int count = 0; char yn; /*?yn?yes?no*/ printf(Please choose the department name(CS/MA/IS): ); scanf(%s,deptname); /*?deptname?*/ EXEC SQL CONNECT TO TESTlocalhost:54321 USER SYSTEM/MANAGER; /*?TEST*/ EXEC SQL DECLARE SX CURSOR FOR /*?SX*/ SELECT Sno,Sname,Ssex,Sage /*SX?*/ FROM Student WHERE SDept = :deptname; EXEC SQL OPEN SX; /*?SX?*/ An Introduction to Database System ? for ( ; ; ) /*?*/ EXEC SQL FETCH SX INTO :HSno,:Hsname,:HSsex,:HSage; /*?*/ if (SQLCA.SQLCODE!= 0) /*SQLCODE != 0?*/ break; /*?SQLCA?*/ if(count+ = 0) /*?*/ printf(n%-10s %-20s %-10s %-10sn, Sno“,Sname“,Ssex, Sage); printf(%-10s %-20s %-10s %-10dn“, HSno,Hsname,Hssex,HSage); /*?*/ printf(“UPDATE AGE(y/n)?”); /*?*/ doscanf(%c, while(yn != N . An Introduction to Database System ? if (yn = y | yn = Y) /*?*/ printf(INPUT NEW AGE:); scanf(%d, /*?*/ EXEC SQL UPDATE Student /*?SQL?*/ SET Sage = :NEWAGE WHERE CURRENT OF SX; /*?*/ EXEC SQL CLOSE SX; /*?SX?*/ EXEC SQL COMMIT WORK; /*?*/ EXEC SQL DISCONNECT TEST; /*?*/