往oracle函CLOB、LONG字段类型的数据表插入值超过4000字节时,在执行INSERT INTO提示:ORA-01704:文字字符串过长错误,这种情况下,如果想用SQL实现的话,可以通过ORACLE存储过程实现。
--
处理方法一:用EXECUTE IMMEDIATE 实现
-- ASQL:带参数的INSERT INTO 语句
-- ALongVar1 : 参数值,大于4000字节
CREATE OR REPLACE PROCEDURE EXEC_SQL1(ASQL varchar2 , ALongVar1 clob)
AS
BEGIN
EXECUTE IMMEDIATE ASQL USING ALongValue;
END EXEC_SQL1;
-- 处理方法二:用Dbms_sql接口实现
-- ASQL:带参数的INSERT INTO 语句
-- ALongVar1 : 参数值,大于4000字节
CREATE OR REPLACE PROCEDURE EXEC_SQL(ASQL clob, ALongValue clob)
AS
The_c1 Integer ;
The_result Integer ; -- dml_sql_result
BEGIN
The_C1 : = Dbms_sql.open_cursor;
Dbms_sql.parse(the_C1, ASQL, DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE
(The_c1, ' :LongValue ' , ALongValue);
The_result: = Dbms_sql. execute (The_c1);
Dbms_sql.close_cursor(The_C1);
END EXEC_SQL;
-- ASQL:带参数的INSERT INTO 语句
-- ALongVar1 : 参数值,大于4000字节
CREATE OR REPLACE PROCEDURE EXEC_SQL1(ASQL varchar2 , ALongVar1 clob)
AS
BEGIN
EXECUTE IMMEDIATE ASQL USING ALongValue;
END EXEC_SQL1;
-- 处理方法二:用Dbms_sql接口实现
-- ASQL:带参数的INSERT INTO 语句
-- ALongVar1 : 参数值,大于4000字节
CREATE OR REPLACE PROCEDURE EXEC_SQL(ASQL clob, ALongValue clob)
AS
The_c1 Integer ;
The_result Integer ; -- dml_sql_result
BEGIN
The_C1 : = Dbms_sql.open_cursor;
Dbms_sql.parse(the_C1, ASQL, DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE
(The_c1, ' :LongValue ' , ALongValue);
The_result: = Dbms_sql. execute (The_c1);
Dbms_sql.close_cursor(The_C1);
END EXEC_SQL;
DELPHI调用例子:
AdoQuery1.Close ;
AdoQuery1.Parameters.Clear ;
AdoQuery1.SQL.Clear ;
AdoQuery1.SQL.Add('{call exec_sql(?,?)}') ;
AdoQuery1.Parameters.CreateParameter('asql',ftOraClob,pdinput, 30,'') ;
AdoQuery1.Parameters.CreateParameter('alongvalue',ftOraClob,pdinput, 30,'') ;
--语句
AdoQuery1.Parameters.ParamByName('asql').Value :='INSERT INTO TAB(ID, CLOBFLD) VALUES(1, :CLOBVALUE)';
--值
AdoQuery1.Parameters.ParamByName('CLOBVALUE').Value :='CLOBVALUE....';
AdoQuery1.ExecSQL ;