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
 T-sql to read from a file?

Author  Topic 

Yellowdog
Starting Member

34 Posts

Posted - 2009-01-26 : 13:13:53
I am trying to create an ssis package that will import 4 different files into a temp table for later processing and need to find a way to determine what procedure to call for the specific type.

All of the files have similar data but different headers so I figured I could just sniff the file to determine the file type and call the correct procedure from there, only problem is I cannot seem to figure out how it is that I can get this done.


I am new to sql and don't know all the tricks yet so any help would be appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-26 : 13:16:38
You can read a file through xp_cmdshell. I typically use the type command and put the results into a table via INSERT INTO/EXEC.

INSERT INTO SomeTable(Column1)
EXEC master.dbo.xp_cmdshell 'type C:\SomeFile.txt'

This is just an example of using T-SQL to read a file. I would probably use VBScript in your case instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-26 : 13:20:55
An SSIS package?
Use a FOR EACH LOOP to iterate through all files (an option) if you don't know the exact file names.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-26 : 13:28:33
Peter, he wants to "sniff" the file.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-26 : 13:38:11
It should be possible to add a data flow task to read only one record, the header record for "sniffing" the file.
Then depending on the result, execute the correct stored procedure.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Yellowdog
Starting Member

34 Posts

Posted - 2009-01-26 : 15:26:33
Ok well I am more confused than ever on this.

After some time reading about xp_cmdshell and it seems like it is a powerful tool but after running a few commands I cannot seem to get it to just insert a few columns into a temp table. Unfortunately I am not too familiar with building ssis packages and I am having a difficult time wrapping my brain around how I can manage to get this done.

When you talk about using a for each loop to itereate through the file can you explain a little bit about how this is done? I see the tool, I am just unsure how this process works. Is it as simple as adding a directory and a file type and using an insert statement to get just a limited amount of data into the temp table?

Thanks again for all the help on this
Go to Top of Page
   

- Advertisement -