Hi Tim,
If you have MSSQL, I'd create a DTS package for this, and call it by creating a SQL Server Agent job. The DTS environment is tailor-made for this sort of operation.
<deep breath> However, if you don't want to go that route or you don't have access to such tools, here's the approach:
I assume your text file is carriage-return delimited, and that you've loaded the contents of the text file into a variable, which I'll call "INSRECS".
<cfloop index="thisrow" list="#INSRECS#" delimiters="#chr(13)#">
<cfset passdata=thisrow>
<Cfstoredproc dbname="IIMAK" datasource="sql" procedure="gensp_HandleCDL">
<cfprocparam type="IN" cfsqltype="CF_SQL_LONGVARCHAR" dbvarname="@p_vdatarow" value="#passdata#">
<cfprocresult name="result">
</cfstoredproc>
<cfloop>
<MSSQL-biased solution>
Now, the solution lies in using dynamic SQL within your SP to create and issue the INSERT statement. Using the example call above:
use MyDB
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'gensp_HandleCDL' AND type = 'P')
DROP PROCEDURE gensp_HandleCDL
GO
CREATE PROCEDURE gensp_HandleCDL
@p_vdatarow varchar(1024)
AS
set nocount on
declare @l_vSQL varchar(2048)
set @l_vSQL = 'INSERT INTO Mytable (--field names here, corresponding to the passed info)
VALUES ('+@p_vdatarow+')'
exec(@l_vSQL)
RETURN @@ERROR
</MSSQL-biased solution>
To handle varying datatypes (obviously you have text in there), do an inner loop inside the main CFLOOP based on the datarow list, and set a counter. Reconstruct the list with single quote marks in the right places:
<Cfset counter=0>
<cfset returnval=""
<cfloop index="thisval" list="#thisrow#">
<cfset counter=counter+1>
<cfif counter is 1 or counter is 2>
<cfset returnval=returnval&"'"&#thisval#&"',">
<cfelse>
<cfset returnval=returnval&#thisval#&",">
</cfif>
</cfloop>
<cfset passdata = mid(returnval,1,len(returnval)-1>
Enjoy.
Phil Hegedusich
Senior Web Developer
IIMAK
-----------
A priest, a lawyer, and a rabbi walk into a bar. The bartender looks up and says "What is this, a joke?