使用存储过程读取Oracle中的clob字段的数据
在做数据库开发的时候,有时候会遇到需要读取Oracle数据库中的clob类型的数据的情况。本着代码复用的目的,我写了下面的存储过程
在做数据库开发的时候,,有时候会遇到需要读取Oracle数据库中的clob类型的数据的情况。本着代码复用的目的,我写了下面的存储过程:读取数据库中clob字段的数据。希望跟大家交流一下。
CREATE OR REPLACE PROCEDURE prc_read_clob(
table_name IN VARCHAR2,
clob_column_name IN VARCHAR2,
primary_Key_Column_names IN VARCHAR2,
primary_key_values IN VARCHAR2,
offset_i IN NUMBER,
read_length_i IN NUMBER,
RES OUT VARCHAR2,
total_length OUT NUMBER
) AS
/**
Autor:Hanks_gao.
Create Date:2008/12/10
Description:This procedure is to read clob value by conditions
--------------------------------------------------------------
-----------------Parameters descritption----------------------
table_name : The table that contains clob/blob columns(表名)
clob_column_name : Clob/blob column name of table_name(类型为clob的字段名)
primary_key_column_names : The columns seperated by '}' that can fix only one row data (that is primary key) (主键名,以'}'分隔的字符串)
primary_key_values : The primary keyes values that seperated by '}'(主键键值,以'}'分隔的字符串)
offset_i : The offset of reading clob data(要读取的位移量)
read_length_i : The length of reading clob data per times(要读取的长度)
res : Return value that can be referenced by application(读取的结果)
total_length : The total length of readed clob data(数据库查询到的clob数据的总长度)
-----------------End Parameters descritption------------------
*/
tmpPrimaryKeys VARCHAR2(2000); --To save primary_Key_Column_names temporarily(暂存主键,主键是以'}'分隔的字符串)
tmpPrimaryKeyValues VARCHAR2(2000); --To save primary_key_values temporarily(暂存主键键值,以'}'分隔的字符串)
i NUMBER; --循环控制变量
tmpReadLength NUMBER; --暂存要读取的长度
sqlStr VARCHAR2(6000); --Query string(查询字符串)
sqlCon VARCHAR2(5000); --Query condition(查询条件)
TYPE tmparray IS TABLE OF VARCHAR2(5000) INDEX BY BINARY_INTEGER;
arrayPrimaryKeys tmparray; --To save the analyse result of primary_Key_Column_names (暂存分析后得到的主键名)
arrayPrimaryKeyValues tmparray; --To save the analyse result of primary_key_values(暂存分析后得到的主键键值)
BEGIN
total_length := 0;
RES := '';
DECLARE
clobvar CLOB := EMPTY_CLOB;
BEGIN
tmpPrimaryKeys:=primary_Key_Column_names;
tmpPrimaryKeyValues:=primary_key_values;
i:=0;
WHILE INSTR(tmpPrimaryKeys,'}')>0 LOOP --Analyse the column names of primary key(将主键分开,相当于arrayPrimaryKeys =tmpPrimaryKeys.split("}") )
arrayPrimaryKeys(i):=subSTR(tmpPrimaryKeys,1,(INSTR(tmpPrimaryKeys,'}')-1));
tmpPrimaryKeys:=subSTR(tmpPrimaryKeys,(INSTR(tmpPrimaryKeys,'}')+1));
i:=i+1;
END LOOP;
i:=0;
WHILE INSTR(tmpPrimaryKeyValues,'}')>0 LOOP --Analyse the values of primary key
arrayPrimaryKeyValues(i):=subSTR(tmpPrimaryKeyValues,1,(INSTR(tmpPrimaryKeyValues,'}')-1));
tmpPrimaryKeyValues:=subSTR(tmpPrimaryKeyValues,(INSTR(tmpPrimaryKeyValues,'}')+1));
i:=i+1;
END LOOP;
IF arrayPrimaryKeys.COUNT()arrayPrimaryKeyValues.COUNT() THEN --判断键与键值是否能匹配起来
res:='KEY-VALUE NOT MATCH';
RETURN;
END IF;
i := 0;
sqlCon := '';
WHILE i sqlCon := sqlCon || ' AND ' || arrayPrimaryKeys(i) || '='''
|| replace(arrayPrimaryKeyValues(i),'''','''''') || '''';
i := i + 1;
END LOOP;
sqlStr := 'SELECT ' || clob_column_name || ' FROM ' || table_name
|| ' WHERE 1=1 ' || sqlCon || ' AND ROWNUM = 1' ; --组查询字符串
dbms_lob.createtemporary(clobvar, TRUE);
dbms_lob.OPEN(clobvar, dbms_lob.lob_readwrite);
EXECUTE IMMEDIATE TRIM(sqlStr) INTO clobvar; --执行查询
IF offset_i total_length:=dbms_lob.getlength(clobvar);
END IF;
IF read_length_i tmpReadLength := 4000;
ELSE
tmpReadLength := read_length_i;
END IF;
dbms_lob.READ(clobvar,tmpReadLength,offset_i,res); --读取数据
IF dbms_lob.ISOPEN(clobvar)=1 THEN
dbms_lob.CLOSE(clobvar);
END IF;
END;
EXCEPTION
WHEN OTHERS THEN
res:='';
total_length:=0;
END;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
