在 Oracle CLOB 字段中插入多达 32000 个字符!





3.00/5 (4投票s)
2006年7月8日
3分钟阅读

79756

232
使用 ASP 代码在 Oracle CLOB 字段中插入最多 32000 个字符的示例。
引言
本示例介绍如何使用 ASP 代码在 Oracle CLOB 字段中插入最多 32000 个字符。我将在后续文章中介绍如何插入更多字符。该示例应适用于 Oracle 8、9 和 10g。
如果 32000 个字符对您来说足够,请使用此示例,因为存储高达 4GB 会更复杂。
Oracle 部分
1) 创建一个名为 TBL_CLOB 的表,包含 2 个字段
id_int = integer;
clob_txt =clob;
2) 创建一个名为 P_CLOB 的存储过程,包含以下代码:
(P_ID_INT in int,P_CLOB_TXT in varchar2)as
begin
insert into TBL_CLOB values(P_ID_INT,P_CLOB_TXT);
end;
3) 测试插入最多 32000 个字符。使用 SQL Plus,在 CLOB 字段中输入一些星号
SQL> exec p_clob(1,rpad('*',32000,'*'));
SQL> commit;
SQL> exec p_clob(2,rpad('*',19872,'*'));
SQL> commit;
4) 检索您刚刚插入的 2 条记录,并计算 CLOB 字段中的字符数
SQL> select id_int,dbms_lob.getlength(clob_txt) from tbl_clob;
5) 您应该会得到类似这样的结果
ID_INT DBMS_LOB.GETLENGTH(CLOB_TXT)
---------- ----------------------------
1 32000
2 19872
ASP 部分
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<% // 将连接更改为您自己的连接%>
<!--#include file="../Connections/myCon.asp" -->
<%
var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
if (Request.QueryString) {
MM_editAction += "?" + Server.HTMLEncode(Request.QueryString);
}
var MM_abortEdit = false;
var MM_editQuery= " ";
%>
<%
var rsTBL_CLOB = Server.CreateObject("ADODB.Recordset");
rsTBL_CLOB.ActiveConnection = MM_myCon_STRING;
rsTBL_CLOB.Source = "SELECT * FROM TBL_CLOB ORDER BY ID_INT DESC";
rsTBL_CLOB.CursorType = 0;
rsTBL_CLOB.CursorLocation = 2;
rsTBL_CLOB.LockType = 1;
rsTBL_CLOB.Open();
var rsTBL_CLOB_numRows = 0;
%>
<%
if (String(Request("MM_insert")) == "form1") {
// section 1: select the last value of the ID_INT and add 1 to it. (sequence)
var RSGetMaxID = Server.CreateObject("ADODB.Recordset");
RSGetMaxID.ActiveConnection = MM_myCon_STRING;
RSGetMaxID.Source = "SELECT max(ID_INT+1) as ID_INT FROM TBL_CLOB";
RSGetMaxID.CursorType = 0;
RSGetMaxID.CursorLocation = 2;
RSGetMaxID.LockType = 1;
RSGetMaxID.Open();
var rsTBL_CLOB_numRows = 0;
var myID_INT=RSGetMaxID.Fields.Item("ID_INT").Value;
RSGetMaxID.Close();
// if the ID_INT is null, make it = 1
if (myID_INT == null) {myID_INT=1}
// End of section 1
// SECTION 2
// Uses a COMMAND to send the form values to the P_CLOB Stored Procedure
var Command1__P_ID_INT=myID_INT; // the next vaue of the ID_INT
var Command1__P_CLOB_TXT=Request.Form("CLOB_TXT"); // the text entered on the page
var Command1 = Server.CreateObject("ADODB.Command");
Command1.ActiveConnection = MM_myCon_STRING;
Command1.CommandText = "P_CLOB";
Command1.Parameters.Append(Command1.CreateParameter("P_ID_INT", 5, 1,2,Command1__P_ID_INT));
Command1.Parameters.Append(Command1.CreateParameter("P_CLOB_TXT", 200, 1,40000,Command1__P_CLOB_TXT));
Command1.CommandType = 4;
Command1.CommandTimeout = 10;
Command1.Prepared = true;
Command1.Execute();
var MM_editRedirectUrl = "clob.asp";
if (MM_editRedirectUrl) {
Response.Redirect(MM_editRedirectUrl);}
}
// END of section 2
%>
<%
var Repeat1__numRows = -1;
var Repeat1__index = 0;
rsTBL_CLOB_numRows += Repeat1__numRows;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Clob: up to 32000 chars</title>
</head>
<body>
<table border="1">
<tr>
<td>ID</td>
<td>CLOB_TXT</td>
</tr>
<% while ((Repeat1__numRows-- != 0) && (!rsTBL_CLOB.EOF)) { %>
<tr>
<td valign="top"><%=(rsTBL_CLOB.Fields.Item("ID_INT").Value)%></td>
<td><%=(rsTBL_CLOB.Fields.Item("CLOB_TXT").Value)%></td>
</tr>
<%
Repeat1__index++;
rsTBL_CLOB.MoveNext();
}
%>
</table>
<form name="form1" method="post" action="<%=MM_editAction%>">
<p>
<input name="ID_INT" type="hidden" id="ID_INT">
</p>
<p>
<textarea name="CLOB_TXT" cols="100" rows="5" id="CLOB_TXT">看,我马上就要插入超过 4000 个字符了……</textarea>
</p>
<p>
<input type="submit" name="Submit" value="Insert">
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<%
rsTBL_CLOB.Close();
%>