Dynamic SQL Server Bulk IMport Syntax For File with Column Names in the First Row.

I need to modify a SQL Server Bulk Import SPROC statement slightly and I'm not sure of the syntax.

This is the statement I have used in the past
	set @sql = 'Insert into Balance_Import
	select * from openrowset( bulk '''+@ImportFileNameAndLoc+''' ,FORMATFILE = '''+@FormatFileNameAndLoc+''') as a'

Open in new window


The variables '@ImportFileNameAndLoc' and '@FormatFileNameAndLoc' are passed to the SPROC as parameters

It worked for file being imported that did not have field names in the first record of the imported file.

The import file I am working with now has field names in the first line so I need to add the  Firstrow=2 into the above statement.

How do I revise the original statement to add the 'Firstrow=2'?

If it helps here is the hard coded version of the Bulk import
--Insert into Balance_Import
--select * FROM OPENROWSET( BULK 'I:\My Documents\Access_Databases\JordanDelinq\TestDirectory\Balances\MJOLibrary Summaries_TabDelim.txt', firstrow = 2, 
--FORMATFILE = 'I:\My Documents\Access_Databases\JordanDelinq\TestDirectory\SQLImportFormatFiles\Balance_TabDelimited_Import_MArkW01.txt') as a

Open in new window

LVL 1
mlcktmguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Add another parameter : @firstrow char(1) = 1

Make it the last parameter and you dont need to use it unless it is > 1 (ie 2 or more)

Means it will default to "1" if not supplied and you dont need to include

-- the parameters 
declare @firstrow char(1) = '2'
declare @ImportFileNameAndLoc varchar (500) = 'c:\mrwtest\ee\MJOTestSummariesTabDelimited.txt'
declare @FormatFileNameAndLoc varchar(500) = 'c:\mrwtest\ee\balance_import_format.txt'
-- then inside the proc

declare @sql varchar(max)

set @sql = 'Insert into Balance_Import
	select * from openrowset( bulk '''+@ImportFileNameAndLoc+''''+ iif(@firstrow > 1, ' , FIRSTROW='+@firstrow,'')+' , FORMATFILE = '''+@FormatFileNameAndLoc+''') as a'

print @sql

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlcktmguyAuthor Commented:
Thanks You
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.