April 30, 2009 at 3:22 pm
I have a folder drop where apache server logs files are dropped. I need to periodically read the files and parse the lines and insert the info into a table.
What is the best approach? Should I parse the files in a sp ?
April 30, 2009 at 3:54 pm
Hi
I'm not sure if this is the best approach but it's definitely a geek-toy:
Copied and pasted from BOL (search for sp_addlinkedserver):
--Create a linked server.
EXEC sp_addlinkedserver txtsrv, 'Jet 4.0',
'Microsoft.Jet.OLEDB.4.0',
'c:\data\distqry',
NULL,
'Text'
GO
--Set up login mappings.
EXEC sp_addlinkedsrvlogin txtsrv, FALSE, Admin, NULL
GO
--List the tables in the linked server.
EXEC sp_tables_ex txtsrv
GO
--Query one of the tables: file1#txt
--using a four-part name.
SELECT *
FROM txtsrv...[file1#txt]
Another good way might be BCP.
Greets
Flo
May 1, 2009 at 7:12 am
I'd probably use SSIS and execute the package in job.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 1, 2009 at 7:32 am
Great! Thanks..
May 1, 2009 at 7:59 am
I did something like this recently and tried a few approaches but eded up using LogParser, its a free tool from microsoft that reads log files and can output them to the database.
One advantage of log parser is that it can mantain its own checkpoints, so if you have a directory full of files log parser can remember what files it has processed and only process the new files.
May 3, 2009 at 6:11 pm
steveb (5/1/2009)
I did something like this recently and tried a few approaches but eded up using LogParser, its a free tool from microsoft that reads log files and can output them to the database.One advantage of log parser is that it can mantain its own checkpoints, so if you have a directory full of files log parser can remember what files it has processed and only process the new files.
Did you use it to import data into a table? If so, can you explain a bit more about how you did it? Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 4:54 am
I would go with SSIS and the following link should get to started
http://www.sqlis.com/post/Looping-over-files-with-the-Foreach-Loop.aspx
--Ramesh
May 4, 2009 at 11:53 am
Ramesh (5/4/2009)
I would go with SSIS and the following link should get to startedhttp://www.sqlis.com/post/Looping-over-files-with-the-Foreach-Loop.aspx
Not a bad recommendation... I just do it all from T-SQL. I hope to never learn how to even spell "SSIS". 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 12:04 pm
Jeff Moden (5/4/2009)
Ramesh (5/4/2009)
I would go with SSIS and the following link should get to startedhttp://www.sqlis.com/post/Looping-over-files-with-the-Foreach-Loop.aspx
Not a bad recommendation... I just do it all from T-SQL. I hope to never learn how to even spell "SSIS". 🙂
C'mon Jeff, SSIS is a nice tool, when used correctly, just like every other tool.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 4, 2009 at 2:44 pm
I ended up hacking what I wanted from here:
http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-text-files/
May 4, 2009 at 3:51 pm
psteja2000 (5/4/2009)
I ended up hacking what I wanted from here:http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-text-files/
Not bad... just be advised that there is absolutely no need to resort to the likes of xp_CmdShell or the horribly slow sp_OA* sprocs unless you need a whole lot of detail about the files. Try the following command and you'll see what I mean... you can probably guess its function once you see what it returns...
EXEC Master.dbo.xp_DirTree 'C:\',1,1
You can also capture the output from that in a table thusly...
[font="Courier New"] CREATE TABLE #MyDirectory
(
RowNum INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
ObjectName SYSNAME,
Depth TINYINT,
IsFile TINYINT
)
INSERT INTO #MyDirectory
(ObjectName, Depth, IsFile)
EXEC Master.dbo.xp_DirTree 'C:\',1,1
SELECT * FROM #MyDirectory[/font]
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 3:53 pm
Jack Corbett (5/4/2009)
Jeff Moden (5/4/2009)
Ramesh (5/4/2009)
I would go with SSIS and the following link should get to startedhttp://www.sqlis.com/post/Looping-over-files-with-the-Foreach-Loop.aspx
Not a bad recommendation... I just do it all from T-SQL. I hope to never learn how to even spell "SSIS". 🙂
C'mon Jeff, SSIS is a nice tool, when used correctly, just like every other tool.
Heh... Yep... I know. That's why I put a nice glass case around it and hung it on the wall instead of using it. 😛 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 3:57 pm
Jeff Moden (5/4/2009)
psteja2000 (5/4/2009)
I ended up hacking what I wanted from here:http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-text-files/
Not bad... just be advised that there is absolutely no need to resort to the likes of xp_CmdShell or the horribly slow sp_OA* sprocs unless you need a whole lot of detail about the files. Try the following command and you'll see what I mean... you can probably guess its function once you see what it returns...
EXEC Master.dbo.xp_DirTree 'C:\',1,1
You can also capture the output from that in a table thusly...
[font="Courier New"] CREATE TABLE #MyDirectory
(
RowNum INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
ObjectName SYSNAME,
Depth TINYINT,
IsFile TINYINT
)
INSERT INTO #MyDirectory
(ObjectName, Depth, IsFile)
EXEC Master.dbo.xp_DirTree 'C:\',1,1
SELECT * FROM #MyDirectory[/font]
And, sorry. Forgot to mention what the operands of that command are...
The first is obviously the top level directory you want to look at as if it were "level 1". It can either be a drive local to the server (as in my example) or it can contain a UNC (provided that the server is setup to actually "see" it).
The second operand is the number of levels deep you want to go in the directory structure. Considering the very limited data that is returned, it's usually a futile task to use anything other than "1" which is the current directory listed in the first operand.
The 3rd operand will return the "IsFile" column for any non-zero value. It's the operand that most "cheat sheets" on undocumented stored procedures usually miss.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 4:02 pm
Like Jackie Chan's Uncle says... "And one more thing..."
If the second operand evaluates to "0" (zero), you get the whole directory structure from the current directory mentioned in the first operand to the bottom of the well. And, if the server cannot see the directory in the first operand or it doesn't exist, no rows will be returned and no "Does Not Exist" warning will be given. The xp_DirTree command does not return the "." or ".." "files" either so you can't differentiate between and empty found directory and a not-found directory.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 4, 2009 at 4:05 pm
Jeff Moden (5/4/2009)
Ramesh (5/4/2009)
I would go with SSIS and the following link should get to startedhttp://www.sqlis.com/post/Looping-over-files-with-the-Foreach-Loop.aspx
Not a bad recommendation... I just do it all from T-SQL. I hope to never learn how to even spell "SSIS". 🙂
Too late, you already have! 😉
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply