Oracle 系列参考教程 Pro*C 程序开发
Create/Modify Email:xingchengli@gmail.com Date:2006/10/23
Oracle Pro*C 程序开发
Oracle 系列参考教程 Pro*C 程序开发
Create/Modify Email:xingchengli@gmail.com Date:2006/10/23
1 前言 .................................................................................................................................................1
1.1 读者范围 .................................................................................................................................1
1.2 内容组织 .................................................................................................................................1
1.3 约定 .........................................................................................................................................1
2 新特性介绍......................................................................................................................................2
2.1 ORACIE9I RELEASE 2 (9.2) 新特性..........................................................................................2
2.2 ORACLE9I RELEASE 1 (9.0.1) 新特性......................................................................................2
2.3 ORACLE 8I RELEASEE 8.1.5 新特性.........................................................................................3
2.4 ORACLE 8I RELEASE 8.1.4 新特性 ...........................................................................................3
2.5 ORACLE 8I RELEASE 8.1.3 新特性 ...........................................................................................3
3 概述 .................................................................................................................................................3
3.1 什么是ORACLE预编译程序? ................................................................................................3
3.2 为什么使用ORACLE预编译程序? ........................................................................................4
3.3 为什么使用SQL?..................................................................................................................4
3.4 为什么使用PL/SQL? ............................................................................................................5
3.5 PRO*C预编译的优点..............................................................................................................5
3.6 常见的问题 .............................................................................................................................6
3.6.1
我怎么编译链接应用程序?
.........................................................................................6
3.6.2
什么是
varchar?...............................................................................................................8
3.6.3
在什么情况下不
使用
Pro*C/C++
和
SQLLIB
库函数?
...............................................8
3.6.4
能在
Pro*C/C++
程序中调用存储过程吗?
..................................................................8
3.6.5
我能在
SQL
语句的任意位置使用绑定变量(也可理解为用户自定义变量或输入
宿主变量)吗?
..............................................................................................................................8
3.6.6
对
Pro*C/C++
字符类型变量的困惑?
..........................................................................9
3.6.7
关于字符串指针变量的应用有特殊需要注意的么?
.................................................9
3.6.8
为什么
SPOOL
不能用在
Pro*C
程序中
? ........................................................................9
3.6.9 Pro*C/C++
支持结构作为宿主变量么?
..........................................................................9
3.6.10
可以在递归函数中嵌入
SQL
么
?..................................................................................10
3.6.11
我可以在任意版本的
Oracle
中使用任意版本的预编译器么?
.................................10
3.6.12 1405
错误(
Fetch column values is null
)可避免么?
...............................................10
4 预编译介绍....................................................................................................................................10
4.1 嵌入式SQL编程概念介绍....................................................................................................10
4.1.1
可嵌入
Pro*C/C++
的
SQL
语句
....................................................................................10
4.1.2
嵌入
SQL
语句的语法格式
............................................................................................11
4.1.3
静态和动态
SQL
语句
....................................................................................................12
4.1.4
嵌入的
PL/SQL
语句块
..................................................................................................12
4.1.5
宿主变量和指示变量
...................................................................................................12
4.1.6 Oracle
数据类型
................................................................................................................13
4.1.7
数组
...............................................................................................................................13
4.1.8
数据类型转换
...............................................................................................................13
4.1.9
私有
SQL
工作区、游标和记录集
................................................................................13
Oracle 系列参考教程 Pro*C 程序开发
Create/Modify Email:xingchengli@gmail.com Date:2006/10/23
4.1.10
事务
...............................................................................................................................13
4.1.11
错误和警告
...................................................................................................................14
4.2 开发嵌入式PRO*C/C++程序过程........................................................................................14
4.3 程序编写规范 .......................................................................................................................15
4.3.1
注释
...............................................................................................................................15
4.3.2
常数表示方法
...............................................................................................................15
4.3.3
变量声明段
...................................................................................................................16
4.3.4
定界符
...........................................................................................................................17
4.3.5
文件长度限制
...............................................................................................................17
4.3.6
预编译参数对函数声明的影响
...................................................................................17
4.3.7
宿主变量命名规则
.......................................................................................................18
4.3.8
超长源代码换行
...........................................................................................................18
4.3.9
源代码单行最大长度
...................................................................................................18
4.3.10 MAXLITERAL ...............................................................................................................18
4.3.11
操作符
...........................................................................................................................19
4.3.12
语句结束符号
...............................................................................................................19
4.4 根据条件进行预编译 ...........................................................................................................19
4.5 样例表 ...................................................................................................................................20
4.5.1
样例数据
.......................................................................................................................21
4.6 样例程序 ...............................................................................................................................21
4.6.1
编译运行方法
...............................................................................................................24
5 数据库操作描述............................................................................................................................24
5.1 连接到数据库 .......................................................................................................................25
5.1.1
使用
ALTER AUTHORIZATION
选项改变用户密码
....................................................25
5.1.2
自动连接
.......................................................................................................................25
5.1.3
连接权限
.......................................................................................................................26
5.1.4
通常连接失败的原因
...................................................................................................26
5.2 高级连接选项 .......................................................................................................................26
5.2.1
预备知识
.......................................................................................................................26
5.2.2
并发连接
.......................................................................................................................27
5.2.3
默认的数据库和连接
...................................................................................................27
5.2.4
并行直接连接
...............................................................................................................27
5.2.5
间接连接
.......................................................................................................................28
5.3 事务应用场合 .......................................................................................................................29
5.4 开始和结束事务 ...................................................................................................................29
5.5 使用COMMIT语句...............................................................................................................29
5.6 使用SAVEPOINT语句..........................................................................................................30
5.7 ROLLBACK语句..................................................................................................................31
5.8 SET TRANSACTION语句 ...................................................................................................31
5.9 重置默认锁状态 ...................................................................................................................32
5.9.1
使用
FOR UPDATE OF.................................................................................................32
5.9.2
使用
LOCK TABLE ........................................................................................................32
5.10 FETCH中应用COMMIT语句 ..............................................................................................33
5.11 分布式事务处理 ...................................................................................................................33
Oracle 系列参考教程 Pro*C 程序开发
Create/Modify Email:xingchengli@gmail.com Date:2006/10/23
5.12 有用的技巧和方针 ...............................................................................................................34
6 数据类型和宿主变量....................................................................................................................34
6.1 ORACLE数据类型..................................................................................................................34
6.1.1
内部数据类型列表
.......................................................................................................34
6.1.2
外部数据类型列表
.......................................................................................................35
6.2 宿主变量 ...............................................................................................................................36
6.2.1
声明宿主变量
...............................................................................................................36
6.2.2
使用宿主变量
...............................................................................................................37
6.3 指示变量 ...............................................................................................................................38
6.3.1 INDICATOR
关键字
..........................................................................................................38
6.3.2
指示变量用法实例
.......................................................................................................38
6.3.3
指示变量应用规范
.......................................................................................................39
6.3.4
限制
...............................................................................................................................39
6.4 VARCHAR变量 ....................................................................................................................39
6.4.1
定义
VARCHAR
变量
......................................................................................................39
6.4.2
使用
VARCHAR
变量
......................................................................................................40
6.4.3
空值处理
.......................................................................................................................41
6.4.4 VARCHAR
变量作为函数参数用法
..................................................................................41
6.4.5
例程
...............................................................................................................................41
6.5 CURSOR变量 .......................................................................................................................44
6.5.1
定义
cursor
变量
.............................................................................................................44
6.5.2
分配
cursor
变量
.............................................................................................................44
6.5.3
打开
cursor
变量
.............................................................................................................45
6.5.4
关闭和释放
cursor
变量
.................................................................................................46
6.5.5
使用限制
.......................................................................................................................46
6.5.6
例程
...............................................................................................................................47
6.6 CONTEXT变量.....................................................................................................................49
6.7 通用ROWID..........................................................................................................................50
6.8 结构型宿主变量 ...................................................................................................................51
6.8.1
结构和数组
...................................................................................................................51
6.8.2 PL/SQL
的
RECORD...........................................................................................................52
6.8.3
结构嵌套和联合
...........................................................................................................52
6.8.4
结构型指示变量
...........................................................................................................52
6.8.5
例程
...............................................................................................................................52
6.9 指针变量 ...............................................................................................................................54
6.9.1
声明指针变量
...............................................................................................................54
6.9.2
使用指针变量
...............................................................................................................55
7 静态SQL........................................................................................................................................55
7.1 基本SQL语句........................................................................................................................55
7.1.1 SELECT
语句
.....................................................................................................................56
7.1.2 INSERT
语句
......................................................................................................................60
7.1.3 UPDATE
语句
....................................................................................................................62
7.1.4 DELETE
语句
....................................................................................................................64
Oracle 系列参考教程 Pro*C 程序开发
Create/Modify Email:xingchengli@gmail.com Date:2006/10/23
7.1.5 WHERE
子句
......................................................................................................................65
7.2 DML返回子句 ......................................................................................................................65
7.3 普通顺序游标操作 ...............................................................................................................66
7.3.1 DECLARE
语句
..................................................................................................................66
7.3.2 OPEN
语句
.........................................................................................................................67
7.3.3 FETCH
语句
......................................................................................................................67
7.3.4 CLOSE
语句
.......................................................................................................................68
7.4 滚动游标 ...............................................................................................................................68
7.4.1
使用滚动游标
...............................................................................................................68
7.4.2
使用
CLOSE_ON_COMMIT
预编译选项
......................................................................69
7.5 优化提示 ...............................................................................................................................69
7.6 CURRENT OF子句...............................................................................................................69
7.7 游标控制 ...............................................................................................................................70
7.8 一个普通游标范例 ...............................................................................................................70
7.9 一个滚动游标应用范例 .......................................................................................................72
8 ORACLE动态SQL.......................................................................................................................74
8.1 动态SQL含义........................................................................................................................74
8.2 动态SQL的优缺点................................................................................................................74
8.3 动态SQL适用环境................................................................................................................74
8.4 动态SQL执行必须条件........................................................................................................75
8.5 动态语句执行过程 ...............................................................................................................75
8.6 使用动态SQL的情况和方法................................................................................................75
8.6.1 METHOD 1 .......................................................................................................................76
8.6.2 METHOD 2 .......................................................................................................................78
8.6.3 METHOD 3 .......................................................................................................................80
8.6.4 METHOD 4 .......................................................................................................................83
8.6.4.1 需要SQLDA............................................................................................................................83
8.6.4.2 DESCRIBE语句......................................................................................................................83
8.6.4.3 什么是SQLDA?....................................................................................................................84
8.6.4.4 处理过程描述 .........................................................................................................................84
8.6.4.5 使用SQLDA变量....................................................................................................................85
8.6.4.6 一些预备知识 .........................................................................................................................87
8.6.4.7 基本步骤 .................................................................................................................................89
8.6.4.7.1 定义一个宿主变量select_stmt..........................................................................................91
8.6.4.7.2 定义SQLDA变量描述字 ..................................................................................................91
8.6.4.7.3 为描述字分配空间............................................................................................................91
8.6.4.7.4 设置描述字中数组的最大成员个数................................................................................92
8.6.4.7.5 给select_stmt宿主变量赋值..............................................................................................93
8.6.4.7.6 PREPARE语句 ...................................................................................................................93
8.6.4.7.7 DECLARE游标 ..................................................................................................................94
8.6.4.7.8 DESCRIBE绑定变量 .........................................................................................................94
8.6.4.7.9 重置占位符数目................................................................................................................95
8.6.4.7.10 得到宿主变量值并分配存储空间..................................................................................95
8.6.4.7.11 打开游标..........................................................................................................................96