Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to load many .csv files to a database

Author  Topic 

lordoftrades
Starting Member

15 Posts

Posted - 2012-08-29 : 11:37:24
Hello

I have searched around and found some different codes, but as I'm completely new to this game I don't get much out of the stuff written in the codes. And I'm not able to see what I'm doing wrong when it doesn't work.

I've tried this one, that is supposed to load all .csv files from a folder. Even though I don't get error-messasges I'm being given message with the path followed by No Access. Like this:

BULK INSERT CSVTest4 FROM 'C:\Users\Espen\Desktop\Intraday\Textfiles\IQ\Intraday\Ingen tilgang.' WITH (FIELDTERMINATOR = ',')

Here's my code:

--Performance improver.
SET NOCOUNT ON
--Local variable declaration; dont edit anything here.
DECLARE @nSQL VARCHAR(8000)
DECLARE @DIR_COMMAND VARCHAR(1024)
DECLARE @path VARCHAR(1024)
DECLARE @Temp_Table VARCHAR(64)

-- Put the drive where your csv files are placed here
SET @path = 'C:\Users\Espen\Desktop\Intraday\Textfiles\IQ\Intraday\'
-- DO NOT EDIT this command ;
--this will only absorb the filenames from the above path
SET @DIR_COMMAND = 'dir /b "' + @path + '"'

--YOUR_TABLE_NAME
SET @Temp_Table = 'CSVTest4'

DECLARE @FileList table(OutputList varchar(500) NULL)
INSERT INTO @FileList
exec xp_cmdshell @DIR_COMMAND

IF (RIGHT(@Path,1) = '\')
SET @Path = LEFT(@Path,LEN(@Path) - 1)

SET @nSQL = ''
SELECT @nSQL = @nSQL +
N' BULK INSERT ' + CAST(@Temp_Table AS nvarchar(100))
+ N' FROM ''' + CAST(CASE WHEN CHARINDEX('\',OutputList) = 0
THEN @Path + '\' + OutputList
ELSE OutputList
END as nvarchar(255)) + N''''
+N' WITH (FIELDTERMINATOR = '','')' + CHAR(10)
FROM @FileList
WHERE OutputList IS NOT NULL

PRINT @nSQL
-- Comment the above PRINT statment and uncomment the following
--EXEC statment to execute the task
--EXEC (@nSQL)


Hope someone see the missing link somewhere.

Kind regards
Espen

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-29 : 11:41:14
There's a simpler method using the command line, especially if you're only importing to a single table:

http://weblogs.sqlteam.com/robv/archive/2003/09/28/181.aspx
Go to Top of Page

lordoftrades
Starting Member

15 Posts

Posted - 2012-08-29 : 11:47:13
Thanks, that seemed very nice and clean.

I ofcourse managed to get an error message even on this one...


for %a in (c:\Users\Espen\Desktop\Intraday\Textfiles\IQ\Intraday\*.csv) do bcp testminidatabase..CSVTest4 in %a -T -Smyserver -c


Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'for'.

Is it the Smyserver that I should edit?

Kind regards
Espen
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-29 : 11:47:33
SSIS is a good alternative to create a package you can call all the time. Also, if you are using SQL server 2005 or higher. you can perform this task in minutes.



--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

lordoftrades
Starting Member

15 Posts

Posted - 2012-08-29 : 11:50:05
Ok, I've read about it, don't know how it works though. But I thought it was left out from the SQL Express version?

Kind regards
Espen
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-29 : 12:04:34
quote:
Originally posted by lordoftrades

Ok, I've read about it, don't know how it works though. But I thought it was left out from the SQL Express version?

Kind regards
Espen



Yes, that's correct.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

lordoftrades
Starting Member

15 Posts

Posted - 2012-08-29 : 12:06:54
quote:
Originally posted by xhostx

quote:
Originally posted by lordoftrades

Ok, I've read about it, don't know how it works though. But I thought it was left out from the SQL Express version?

Kind regards
Espen



Yes, that's correct.

--------------------------
Joins are what RDBMS's do for a living



Then I'm stuck... I can't get the other short code running properly, and I don't see the logic in the code language yet... Spend too much time in VBA world.

:-)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-29 : 14:15:19
You'd have to run my suggestion via xp_cmdshell:
EXEC master..xp_cmdshell 'for %a in (c:\Users\Espen\Desktop\Intraday\Textfiles\IQ\Intraday\*.csv) do bcp testminidatabase..CSVTest4 in %a -T -Smyserver -c'
Keep in mind when using xp_cmdshell, the C:\ drive references the SERVER's C:\ drive, not your local computer's. You really should be calling this from a command prompt on your local computer, not from a SQL batch via SSMS.
Go to Top of Page
   

- Advertisement -