Create stored procedure to make satic html content

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 13
Reputation: fayola is an unknown quantity at this point 
Solved Threads: 0
fayola's Avatar
fayola fayola is offline Offline
Newbie Poster

Create stored procedure to make satic html content

 
0
  #1
Jun 16th, 2009
hey,
first off I'd like to state that I am pretty new to ms sql and stored procedures tho I have worked with sql for quite some time.

My employer would like me to write a script which takes html content from a mssql db (originally delivered dynamically) and create static pages from them and also would like those tables locked until the script is run again. Now not sure if this will (should) fly because it may be limiting the admins to make changes to their content only during specific times.

But nonetheless, it has been requested. I just found out that ms sql can have access to the servers filesystem, which is cool because the web site and mssql are hosted on the same server.

Now I came a across a simple stored procedure that writes contents to a specified file:
  1. CREATE PROCEDURE sp_OutputtoFile(@FileName VARCHAR(255), @Text1 VARCHAR(255)) AS
  2. DECLARE @FS INT, @OLEResult INT, @FileID INT
  3.  
  4.  
  5. EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
  6. IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject'
  7.  
  8. --Open a file
  9. execute @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 1
  10. IF @OLEResult <> 0 PRINT 'OpenTextFile'
  11.  
  12. --Write Text1
  13. execute @OLEResult = sp_OAMethod @FileID, 'WriteLine', NULL, @Text1
  14. IF @OLEResult <> 0 PRINT 'WriteLine'
  15.  
  16. EXECUTE @OLEResult = sp_OADestroy @FileID
  17. EXECUTE @OLEResult = sp_OADestroy @FS

What I would like is to have content from certain tables in their own folder and have an html file for each record in the tables. So for instance, say I have five tables, Article, Resource, Advice, Job, Company, then I would like to create folders (or goto predefined folders) for each table and have files for each record so that it's labled with it's primary key ID. So in the Articles folder I'd like to have article_1234.html, article_1235.html, article_1236.html ....

I'm sure you get the just of it. Is this possible with a stored procedure and if so, can anyone post some sample code I can use as a base?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 143
Reputation: cutepinkbunnies is an unknown quantity at this point 
Solved Threads: 8
cutepinkbunnies's Avatar
cutepinkbunnies cutepinkbunnies is offline Offline
Junior Poster

Re: Create stored procedure to make satic html content

 
0
  #2
Jun 16th, 2009
This old function I made may help as it demonstrates the concepts of looping with SQL (without a yucky cursor!).

  1. CREATE function [dbo].[fn_Is_Uppercase]
  2. (@string VARCHAR(1000))
  3. RETURNS BIT
  4. as
  5.  
  6. BEGIN
  7. DECLARE @r BIT
  8. DECLARE @l INT
  9. DECLARE @i INT
  10.  
  11. --trim it up
  12. SET @string = mrm.dbo.TRIM(@string)
  13.  
  14. --need to check every character
  15. SET @l = len(@string)
  16. SET @i = 0
  17. SET @r = 1
  18.  
  19. while @i <= @l + 1 AND @r = 1
  20. BEGIN
  21. if ASCII(SUBSTRING(@string,@i - 1, 1)) <> ASCII(UPPER(SUBSTRING(@string,@i - 1, 1)))
  22. BEGIN
  23. SET @r = 0
  24. END
  25. SET @i = @i + 1
  26. END
  27.  
  28. return @r
  29. END

This may not be the most efficient way to iterate through the characters, but it gets the job done.

I would suggest setting up a table variable with the columns you want, along with an identity (1,1) or (0,1) column to iterate through it. For each pass through the loop, write to a file in a given directory. You can check which directory/file to write to on each iteration and while properly incrementing the LCV.

Hope this helps.
Last edited by cutepinkbunnies; Jun 16th, 2009 at 7:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: fayola is an unknown quantity at this point 
Solved Threads: 0
fayola's Avatar
fayola fayola is offline Offline
Newbie Poster

Re: Create stored procedure to make satic html content

 
0
  #3
Jun 18th, 2009
Thanks this helps a lot! Like you said I just need to set it to loop the set of records for each table type that will be mapped it it's own folder, so loop within a loop. Marking as solved!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC