Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Re: Clob datatypes


Message #1 by "William Meitzen" <wmeitzen@y...> on Tue, 18 Jun 2002 21:32:08
> Does anyone know how to query an oracle database on a field that is a 
CLOB
> datatype through
> ASP?

function CLOBRead
Read a specific CLOB field, return as a string
strClobString = CLOBRead
("oracle_table_name", "CLOB_column_name", "Where_clause", "DSN_string")
Example:
strContent = CLOBRead("common_stuff", "content", "title='CLOBUpdate'", 
application("CommonStuffRead"))
Note: application("whatever") is the same 
as "DSN=name;UID=user;PWD=password"
function CLOBRead(strTableCR, strCLOBColumnCR, strWhereClauseCR, strDSNCR)
	dim conDBOracleGenericReadCR, strStringReturnedCR, strSQLCR
	dim intCLOBLengthCR, intStartCR
	const intSegmentLengthCR=4000
	set conDBOracleGenericReadCR=server.CreateObject("ADODB.Recordset")
	conDBOracleGenericReadCR.open "select dbms_lob.getlength(" & 
strCLOBColumnCR & ") as CLOBLength from " & strTableCR & " where " & 
strWhereClauseCR, strDSNCR
	intCLOBLengthCR=conDBOracleGenericReadCR("CLOBLength")
	conDBOracleGenericReadCR.close
	strStringReturnedCR=""
	intStartCR=1
	if intCLOBLengthCR>1 then
		do
			conDBOracleGenericReadCR.open "select 
dbms_lob.substr(" & strCLOBColumnCR & ", " & intSegmentLengthCR & ", " & 
intStartCR & ") as CLOBSegment from " & strTableCR & " where " & 
strWhereClauseCR, strDSNCR
			strStringReturnedCR=strStringReturnedCR & 
conDBOracleGenericReadCR("CLOBSegment")
			conDBOracleGenericReadCR.close
			intStartCR=intStartCR+intSegmentLengthCR
		loop until intStartCR=>intCLOBLengthCR
	end if
	set conDBOracleGenericReadCR=nothing
	CLOBRead=strStringReturnedCR
end function

subroutine CLOBUpdate
Update a specific CLOB field
call CLOBUpdate("oracle_table_name", "CLOB_column_name", 
strSuperDuperLongString, "where_clause", "DSN_string")
Example:
call CLOBUpdate("directory_employees", "BiographicalInfo", 
strBiographicalInfo, "LastName='" & request.form("LastName") & "' and 
FirstName='" & request.form("FirstName") & "' and MiddleName='" & 
request.form("MiddleName") & "'", application("DirectoryEmployeesWrite"))

sub CLOBUpdate(strTableCU, strCLOBColumnCU, strContentCU, 
strWhereClauseCU, strDSNCU)
	dim conDBOracleGenericWriteCU, strSQLCU
	dim intRowsAffectedCU, intCountSQLStringsCU, strSubStringCU
	dim objRegexpCU, objSubStringsCU, intCountSubStringsCU
	dim objSubStringCU
	const intSegmentLengthCU=2000
	set conDBOracleGenericWriteCU=server.CreateObject
("ADODB.Connection")
	if strContentCU="" then
		' - if empty string, write empty string
		conDBOracleGenericWriteCU.open strDSNCU
		conDBOracleGenericWriteCU.BeginTrans
		conDBOracleGenericWriteCU.Execute "update " & strTableCU 
& " set " & strCLOBColumnCU & "='' where " & strWhereClauseCU, 
intRowsAffectedCU
		conDBOracleGenericWriteCU.CommitTrans
		conDBOracleGenericWriteCU.close
	else
		' - write string one segment at a time using Oracle's 
dbms_lob.write function
		set objRegexpCU=new regexp
		objRegexpCU.global=true
		objRegexpCU.pattern="((\n|.){1," & intSegmentLengthCU 
& "})"
		set objSubStringsCU=objRegexpCU.execute(strContentCU)
		for intCountSubStringsCU=0 to objSubStringsCU.count-1
			strSubStringCU=objSubStringsCU.item
(intCountSubStringsCU)
			if intCountSubStringsCU=0 then
				' - write 1st segment
				strSQLCU = "update " & strTableCU & " 
set " & strCLOBColumnCU & "='" & strSubStringCU & "' where " & 
strWhereClauseCU
			else
				' - write subsequent segments
				strSQLCU = "declare Destination clob; "
				strSQLCU = strSQLCU & "begin "
				strSQLCU = strSQLCU & "select " & 
strCLOBColumnCU & " into Destination from " & strTableCU
				strSQLCU = strSQLCU & " where " & 
strWhereClauseCU & " for update; "
				strSQLCU = strSQLCU & "dbms_lob.write
(Destination, " & len(strSubStringCU) & ", "
				strSQLCU = strSQLCU & "dbms_lob.getlength
(Destination)+1, "
				strSQLCU = strSQLCU & "'" & replace
(strSubStringCU, "'", "''") & "'); "
				strSQLCU = strSQLCU & "commit; "
				strSQLCU = strSQLCU & "end; "
			end if

			conDBOracleGenericWriteCU.open strDSNCU
			conDBOracleGenericWriteCU.BeginTrans
			conDBOracleGenericWriteCU.Execute strSQLCU, 
intRowsAffectedCU
			conDBOracleGenericWriteCU.CommitTrans
			conDBOracleGenericWriteCU.close
		next
	end if
	set conDBOracleGenericWriteCU=nothing
end sub

  Return to Index