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.
Author |
Topic |
boogybaz
Starting Member
8 Posts |
Posted - 2007-03-15 : 04:19:14
|
I am trying to import a number of text files into a table within SQL server. They are all the same format, but obviously with different filenames. What I need to do, is import every file within the directory, and put the filename as a column within the table.I am new to using the DTS packager, and this looks like the best way to do what I need, but I have no idea where to start, I have looked at various tutorials on the internet and am trying to get this one to work.[url]http://www.databasejournal.com/features/mssql/article.php/1461661[/url]But it is coming up with errors in the first script at the line Set fso = CreateObject("Scripting.FileSystemObject") Set fsoFolder = fso.GetFolder(sFolder) The other thing is, this way of doing it doesn't look like it will import the filename..Is there a better way of doing the import, or is the above link a good starting place.Thanks. |
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2007-03-15 : 05:22:28
|
Write the filename to a global variable in the loop:DTSGlobalVariables("MyFileNameVariable").Value = fsoFile.Name You should then be able to use this as a source for a column transformation on the datapump task (it's been a while since I've used DTS, so I can't be more specific!)What error are you getting from the fso stuff?Mark |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-15 : 05:24:36
|
This might give you an insight how to get the files you wantCREATE TABLE #Files ( FileID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, FileName VARCHAR(256) )INSERT #Files ( FileName )EXEC xp_cmdshell 'dir "c:\windows\system32\*.*" /b /s /a:a'DELETEFROM #FilesWHERE FileName IS NULLSELECT * FROM #FilesDROP TABLE #Files Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-15 : 05:31:42
|
Or thisCREATE TABLE #Files ( FileID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, FileName VARCHAR(256) )INSERT #Files ( FileName )EXEC xp_cmdshell 'dir c:\windows\system32\*.* /b /s /a:a'DELETEFROM #FilesWHERE FileName IS NULLDECLARE @FileID INT, @bulk_cmd VARCHAR(1000)SELECT @FileID = MAX(FileID)FROM #FilesWHILE @FileID > 1 BEGIN SELECT @bulk_cmd = 'BULK INSERT {DestinationTableNameHere} FROM ''' + FileName + '''' FROM #Files WHERE FileID = @FileID --EXEC (@bulk_cmd) PRINT @bulk_cmd SET @FileID = @FileID - 1 ENDDROP TABLE #Files Peter LarssonHelsingborg, Sweden |
|
|
boogybaz
Starting Member
8 Posts |
Posted - 2007-03-15 : 06:03:59
|
Thanks Peso, playing with the code now The error from the FSO object is"Error Code0: Error Source = Microsoft VBScript Runtime Error Error Description: INvalid procedure call or argumentError on line 19" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-15 : 06:05:20
|
What do you think BULK INSERT do?Peter LarssonHelsingborg, Sweden |
|
|
boogybaz
Starting Member
8 Posts |
Posted - 2007-03-15 : 06:09:43
|
I just saw that, and edited reply to reflect... Sorry Peso :) |
|
|
|
|
|
|
|