SQLTeam.com | Weblogs | Forums

Import CSV file from Excel


#1

I'm trying to import a CSV file to a database using the BULK command. I've created a small procedure that accepts two parameters - path to CSV file and Table to insert data to.

CREATE PROC import_excel
(@tblName VARCHAR(10), @path VARCHAR(100))
AS
DECLARE @cmd VARCHAR(200)
BEGIN
SET @cmd = 'BULK INSERT '+ @tblName +
' FROM ' + @path +
' WITH
(
FIRSTROW = 2 ,
MAXERRORS = 0 ,
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n''
)'
EXECUTE(@cmd)
END

But I am unable to execute it. It throws Incorrect syntax near ' ' I'm pretty sure it has something to do with concatenating strings and parameters, I just can't figure out the problem.

Thank you


#2

' FROM ' + @path should be
' FROM ' + @path +


#3

Oh my bad. The + was just a typo.
I am still unable to execute it.
I edited my post


#4

add PRINT @cmd and post what the contents of @cmd is?


#5

you need to change things to

' FROM ''' + @path +
''' WITH
(
so you can get quotation marks around file source

BULK INSERT sweet FROM 'c:\impor\csv.csc' WITH
(
FIRSTROW = 2 ,
MAXERRORS = 0 ,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

#6

That worked
thank you