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 |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-10-15 : 08:35:52
|
| Guys,I have a utility on each server that runs every quarter to generate HTML report of the server (like what is OS patached to, CPU utilization, drives etc).Is there any way to import html files into SQL database and query it. In the sense that view source of each HTML can it be loaded into the database as XML datatype and query the same. if so please provide any inputs to load such files and query them.Thanks |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2008-10-15 : 09:46:56
|
| I used a function created here:http://www.simple-talk.com/code/WorkingWithFiles/uftReadFileAsTable.txtTo use you need to enable ole automation:sp_configure 'show advanced options', 1;GORECONFIGURE;GOsp_configure 'Ole Automation Procedures', 1;GORECONFIGURE;GOOnce you have function created this will import as XML:DECLARE @itemDoc xml SET @itemDoc = (SELECT line FROM [dbo].[uftReadfileAsTable] ('C:\temp','test.html') FOR XML Path, type) --SELECT @itemDoc INSERT INTO dbo.A (text) (SELECT @itemDoc as Test)The only issue you may have is each line in file becomes a node in XML. If this is a problem try using REPLACE to remove nodes and insert into a varchar(max) column instead. |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2008-10-15 : 10:15:00
|
| I have modified Phil Factor's function so that the entire HTML file is inserted into a single row so the XML INSERT puts entire file into a node.Here is the modified function:alter FUNCTION [dbo].[uftReadfileAsTable](@Path VARCHAR(255),@Filename VARCHAR(100))RETURNS @File TABLE([LineNo] int identity(1,1), line varchar(8000)) ASBEGINDECLARE @objFileSystem int ,@objTextStream int, @objErrorObject int, @strErrorMessage Varchar(1000), @Command varchar(1000), @hr int, @String VARCHAR(8000), @YesOrNo INT, @Counter INTset @Counter =0select @strErrorMessage='opening the File System Object'EXECUTE @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUTif @HR=0 Select @objErrorObject=@objFileSystem, @strErrorMessage='Opening file "'+@path+'\'+@filename+'"',@command=@path+'\'+@filenameif @HR=0 execute @hr = sp_OAMethod @objFileSystem , 'OpenTextFile' , @objTextStream OUT, @command,1,false,0--for reading, FormatASCIIWHILE @hr=0 BEGIN if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='finding out if there is more to read in "'+@filename+'"' if @HR=0 execute @hr = sp_OAGetProperty @objTextStream, 'AtEndOfStream', @YesOrNo OUTPUT IF @YesOrNo<>0 break if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='reading from the output file "'+@filename+'"' if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Readline', @String OUTPUTset @Counter = @Counter + 1IF @Counter = 1 BEGIN INSERT INTO @file(line) SELECT @StringENDIF @Counter > 1 BEGIN UPDATE @file SET line= line + ' ' + @StringEND ENDif @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='closing the output file "'+@filename+'"'if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Close'if @hr<>0 begin Declare @Source varchar(255), @Description Varchar(255), @Helpfile Varchar(255), @HelpID int EXECUTE sp_OAGetErrorInfo @objErrorObject, @source output,@Description output,@Helpfile output,@HelpID output Select @strErrorMessage='Error whilst ' +coalesce(@strErrorMessage,'doing something') +', '+coalesce(@Description,'') insert into @File(line) select @strErrorMessage endEXECUTE sp_OADestroy @objTextStream -- Fill the table variable with the rows for your result set RETURN END |
 |
|
|
|
|
|
|
|