Link to home
Start Free TrialLog in
Avatar of DarinAlred
DarinAlred

asked on

How do I create a Stored Procedure to produce a table from a tab delimited text file

We have a report that creates a .iif file for Quickbooks import into a Journal Entry.

The file conents look like:
------------------------------------------------------------------------------------
!TRNS[\t]TRNSTYPE[\t]DATE[\t]ACCNT[\t]CLASS[\t]AMOUNT[\t]NAME[\t]DOCNUM[cr][lf]
!SPL[\t]TRNSTYPE[\t]DATE[\t]ACCNT[\t]CLASS[\t]AMOUNT[\t]NAME[\t]DOCNUM[cr][lf]
!ENDTRNS[cr][lf]
TRNS[\t]GENERAL JOURNAL[\t]01/01/2100[\t]PrimaryAcct[\t][\t]-55942.36[\t]EntryName[\t]EntryNum[cr][lf]
SPL[\t]GENERAL JOURNAL[\t]01/01/2100[\t]DetailAcct[\t][\t]663.33[\t]EntryName[\t]EntryNum[cr][lf]
SPL[\t]GENERAL JOURNAL[\t]01/01/2100[\t]DetailAcct[\t][\t]358.06[\t]EntryName[\t]EntryNum[cr][lf]
ENDTRNS[cr][lf]
------------------------------------------------------------------------------------

The file name is usually: filename.iif

Due to a issue with a 3rd party software occassionally the file is created with a Detail Account number missing.

I am trying to build a stored procedure on a SQL Express database that pulls this file into a temp table that can then be queried for null values.

Each time I create the procedure and run it I get zero rows returned.  Also selecting all from the temp table gives me an invalid object name error.  The procedure command completes successfully, yet there are no records in the temp table.

What should I do to get this procedure working?  OR should I use a different approach?


So far this is my code:
 
------------------------------------------------------------------------------
--=====================================================================
SET QUOTED_IDENTIFIER OFF
go
CREATE PROCEDURE QBIMport
-- Parameters input into the procedure
@PathFileName varchar(100)
AS
 
CREATE TABLE #QBExport
(
TRNS VARCHAR(2000),
TRNSTYPE VARCHAR(2000),
DATE VARCHAR(2000),
ACCNT VARCHAR(2000),
CLASS VARCHAR(2000),
AMOUNT VARCHAR(2000),
NAME VARCHAR(2000),
DOCNUM VARCHAR(2000)
)
 
--Step 1: Build Valid BULK INSERT Statement
DECLARE @SQL varchar(2000)
DECLARE @FIELDTERMINATOR varchar(2000)
DECLARE @ROWTERMINATOR varchar(2000)
 
SET @FIELDTERMINATOR = "\t"
SET @ROWTERMINATOR = "\r\n"
 
BEGIN
  -- Valid format:
  SET @SQL = "BULK INSERT #QBExport FROM '"+@PathFileName+"' 
WITH (FIRSTROW = 4,MAXERRORS = 0,FIELDTERMINATOR = '"+@FIELDTERMINATOR+"',ROWTERMINATOR = '"+@ROWTERMINATOR+"') "
END
 
--Step 2: Execute BULK INSERT statement
EXEC (@SQL)
--=====================================================================
--Execute procedure
 
EXEC QBIMport 'c:\testqbglexport.iif'
 
//////////////////////////////////////////////////////////////////////////////////////////////

Open in new window

Avatar of TimSledge
TimSledge
Flag of United States of America image

You might find the plug-in offered by SpreadsheetGear.com helpful.  I use it to create SQL tables from Excel spreadsheets.  www.spreadhseetgear.com is the url.
Avatar of DarinAlred
DarinAlred

ASKER

Tim,

Thanks for the suggestion.  However, I am looking for something I can automate without having to make a purchase.

BTW- the link is misspelled.

No problem.  Sorry about the link spelling.

I am currently experimenting with the following:

1st I am using a BCP format file
--bcp FORMAT FILE CREATION

9.0
8
1       SQLCHAR       0       2000       "\t"     1     TRNS     ""
2       SQLCHAR       0       2000     "\t"     2     TRNSTYPE             ""
3       SQLDATETIME       0       2000     "\t"     3     DATE        ""
4       SQLCHAR       0       2000      "\t"   4     ACCNT     ""
5       SQLCHAR       0       2000      "\t"   5     CLASS     ""
6       SQLDECIMAL       0       2000      "\t"   6     AMOUNT     ""
7       SQLCHAR       0       2000      "\t"   7     NAME     ""
8       SQLCHAR       0       2000      "\r\n"   8     DOCNUM     ""

Next

I am using a Global Temp Table structure:

CREATE TABLE ##QBExport
(
TRNS VARCHAR(2000),
TRNSTYPE VARCHAR(2000),
DATE NVARCHAR(10),
ACCNT VARCHAR(2000),
CLASS VARCHAR(2000),
AMOUNT MONEY,
NAME VARCHAR(2000),
DOCNUM VARCHAR(2000)
)

BULK INSERT ##QBExport
FROM 'C:\testqbglexport.txt'
WITH ( FIRSTROW = 4, LASTROW FORMATFILE = 'c:\TestFormatFileNew.Fmt');

However, because my file ends with a footer I am now inserting a counter format file which I got off of another EE answer ( https://www.experts-exchange.com/questions/23349870/Ignoring-last-row-of-Flat-File-when-doing-BULK-INSERT-SQL-Sever-2005.html) mainly because I get an "unexpected end of file" error.

--define a format file for single line per row
--9.0
--1
--1      SQLCHAR       0      8000     "\r\n"   1     Myrecord            ""
--
-- now count the records
--
declare @lastrow int
set     @lastrow = (SELECT count(*) FROM OPENROWSET( BULK 'c:\MyData.txt', FORMATFILE = 'c:\MyFormat_Counting.fmt', MAXERRORS=10) AS a ) - 1
--
-- now use that count to define last record, have to build up dynamic sql, load into staging area
--
if exists (select * from information_schema.tables where table_name = 'mystagingtable') drop table mystagingtable
declare @sqlcmd varchar(max)
set     @sqlcmd = 'SELECT a.* into MyStagingTable FROM OPENROWSET( BULK ''c:\MyData.txt'', FORMATFILE = ''c:\MyFormat.fmt'', MAXERRORS=10, FIRSTROW = 1, LASTROW = '+convert(varchar,@lastrow)+') AS a '
--
exec   (@sqlcmd)
--
select * from MyStagingTable

I will be fiddling with this to see if it can work today.
ASKER CERTIFIED SOLUTION
Avatar of DarinAlred
DarinAlred

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial