[Pro*c]滚动游标变量的使用
生活随笔
收集整理的這篇文章主要介紹了
[Pro*c]滚动游标变量的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?代碼:xx.pc
/* 功能:演示了Oracle滾動游標變量的使用定義游標時注意事項: 1. DECLARE CURSOR語句必須是使用游標的第一條語句 2. 游標名稱是一個標識符,而不是宿主變量,其長度是可以任意的,但只有前31個字符有效 3. 游標所對應的SELECT語句不能包含INTO子句 4. 游標語句(DECLARE,OPEN,FETCH,CLOSE)必須在同一個預編譯單元內 */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sqlca.h> #pragma comment(lib, "orasql10.lib")int connect(); void cursor(); void sql_error(); void main() { EXEC SQL WHENEVER SQLERROR DO sql_error(); // 安裝錯誤處理句柄 if(connect() == 0) { cursor(); EXEC SQL COMMIT RELEASE; // 提交事務,斷開連接 } else printf("連接失敗\n"); } int connect() // connect to oracle database { char username[10], password[10], server[10]; strcpy(username, "scott"); strcpy(password, "scott"); strcpy(server, "orcl"); EXEC SQL CONNECT :username IDENTIFIED BY :password USING :server;if(sqlca.sqlcode == 0) return 0; else return sqlca.sqlcode; } void sql_error() // print error infomation { printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc); } void cursor() // 游標操作 { int dno; // 定義宿主變量 char name[10]; float salary;// 定義游標變量sql_cursor emp_cursor; // sql_cursor:是Proc*C/C++的偽類型printf("請輸入部門號:");scanf("%d", &dno);EXEC SQL ALLOCATE :emp_cursor; // 分配游標變量EXEC SQL EXECUTEBEGINOPEN :emp_cursor FOR Select ename, sal from emp where deptno=:dno;END;END-EXEC;EXEC SQL WHENEVER NOT FOUND DO BREAK; // 游標數據提取完畢后退出循環while(1){ EXEC SQL FETCH :emp_cursor into :name, :salary;printf("name = %s(%d), salary = %4.0f\n", name, strlen(name), salary);}EXEC SQL CLOSE :emp_cursor; // 關閉游標變量EXEC SQL FREE :emp_cursor; // 釋放游標變量printf("sqlca.sqlerrd[2] = %d\n", sqlca.sqlerrd[2]); // sqlca.sqlerrd[2]存放著Select語句作用的行數 }
另外還需要做一定的設置:
總結
以上是生活随笔為你收集整理的[Pro*c]滚动游标变量的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 模块之 string.py
- 下一篇: 基于SSM+SpringBoot+MyS