I have provided a Sample flat file structure which I am trying to automate
using Bulk Insert.
Col1|Col2|Col3
a|b|c
aa|bb|cc
99|Foot|2
First row is the header and the last row is the footer. I want to skip both
of them and insert this into a table.
I guess for header i can make use of 'FirstRow' attribute ... but how can i
ignore the footer.
FYI, approx there would be 25K to 50K of rows in each data file which I am
trying to automate.
Regards
Pradeep
Pradeep
you can use Lastrow parameter
Lastrow Specifies the number of the last row to load. The default is
0, which indicates the last row in the specified data file
This works pefectly for me
bulk insert t1
from 'C:\test.txt' with (firstrow = 2,lastrow = 3,FIELDTERMINATOR
='|',
ROWTERMINATOR ='\n'
)
Thanks and Regards
Amish Shah
For the last row, if you don't know the number of rows in the file, or it
can change, the easiest thing is to just import the row, then DELETE it
afterwards.
If you know the number of rows in the file then you can use LASTROW.
Regards
Pradeep
Funny, I have not seen this request very often, but it seems like fairly
normal thing to do. Something LASTROW = -2 to indicate a row from the end
would be natural. Maybe you should put it on
http://connect.microsoft.com/sqlserver/feedback?
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
-- You could do this with xp_cmdshell
!!type c:\temp\temp.sql|find /i /c /v "they'll never find this" >
c:\temp\temp.txt
GO
-- Pick up the file
DECLARE @int INT
;WITH cte AS ( SELECT 0 x UNION ALL SELECT x + 1 FROM cte WHERE x < 9 )
SELECT @int = SUBSTRING( y, min_pos, xlen )
FROM
(
SELECT
x.y,
PATINDEX( '%[0-9]%', x.y ) AS min_pos,
MAX( CHARINDEX( CAST( c.x AS CHAR(1) ), x.y ) ) AS xlen
FROM cte c CROSS JOIN OPENROWSET( BULK 'c:\temp\temp.txt', SINGLE_CLOB ) x(y)
GROUP BY x.y
) z
SELECT @int