Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Could not find stored procedure 'sp_ExecuteSQL'January #Meme15 Assignment – Twitter, the Blog, taking over the universe »
    comments

    If you are a blogger, you want to spend time on writing the content not on formatting tables and prettifying syntax so that it looks nice. The syntax is taken care of because we use the GeSHi syntax highlighter. formatted output is another story

    If you want to show the output of a query in a html table you have a couple of options. Let's first take a look at a simple query

    1. SELECT TOP 10 name,create_date
    2. FROM msdb.sys.procedures
    3. ORDER BY create_date

    You can of course use the PRE tag to format that somewhat, here is what it looks like

    name	                             create_date
    sp_sqlagent_is_srvrolemember	     2010-02-06 04:12:12.100
    sp_verify_category_identifiers	     2010-02-06 04:12:12.100
    sp_verify_proxy_identifiers	     2010-02-06 04:12:12.107
    sp_verify_credential_identifiers     2010-02-06 04:12:12.110
    sp_verify_subsystems	             2010-02-06 04:12:12.130
    sp_verify_subsystem_identifiers	     2010-02-06 04:12:12.137
    sp_verify_login_identifiers	     2010-02-06 04:12:12.140
    sp_verify_proxy	                     2010-02-06 04:12:12.143
    sp_add_proxy	                     2010-02-06 04:12:12.150
    sp_delete_proxy	                     2010-02-06 04:12:12.157

    What I want is a table, here is what that looks like

    namecreate_date
    sp_sqlagent_is_srvrolemember9/29/2010 10:13:10 AM
    sp_verify_category_identifiers9/29/2010 10:13:10 AM
    sp_verify_proxy_identifiers9/29/2010 10:13:10 AM
    sp_verify_credential_identifiers9/29/2010 10:13:10 AM
    sp_verify_subsystems9/29/2010 10:13:11 AM
    sp_verify_subsystem_identifiers9/29/2010 10:13:11 AM
    sp_verify_login_identifiers9/29/2010 10:13:11 AM
    sp_verify_proxy9/29/2010 10:13:11 AM
    sp_add_proxy9/29/2010 10:13:11 AM
    sp_delete_proxy9/29/2010 10:13:11 AM

    In order to accomplish that, I can grab the query result and put TD and TR tags around the columns and rows. I can also do something like the following

    1. SELECT TOP 10 '<tr><td>',name,'</td><td>',create_date,'</td></tdr>'
    2. FROM msdb.sys.procedures
    3. ORDER BY create_date

    But what if it is a stored proc like xp_fixeddrives? You can of course also use Excel, paste the results in there, add columns and put the TD and TR tags in those columns. but there is a better/easier/faster way. You can use PowerShell to do this for you, you can use the ConvertTo-Html Cmdlet

    Here is a simple powershell script that accepts an instance parameter, runs the query and then generates a html file as output

    1. Param($InstanceName)
    2.  
    3. $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    4. $SqlConnection.ConnectionString = "Server=$InstanceName;Database=master;Integrated Security=True"
    5.  
    6.  
    7. $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    8. $SqlCmd.Connection = $SqlConnection
    9. $SqlCmd.CommandTimeout = 0
    10. $SqlCmd.Connection.Open()
    11.  
    12.  
    13.  
    14. $SqlCmd.CommandText = "SELECT TOP 10 name,create_date
    15.                         FROM msdb.sys.procedures
    16.                         ORDER BY create_date"
    17.                        
    18. $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    19. $SqlAdapter.SelectCommand = $SqlCmd
    20. $DataSet = New-Object System.Data.DataSet
    21. $SqlAdapter.Fill($DataSet)
    22.  
    23.  
    24. $FilePath    = "C:\PowerShellOutput\" + $InstanceName+ "_formattedOutput.htm"
    25. $Title = $InstanceName+ " Formatted Results for Blog"
    26. $DataSet.Tables[0]  | convertto-html -property name, create_date -title $Title > $FilePath

    Save the file as FormatOutput.ps1, I have it saved in my D:\Powershell\ folder, the file will save the output in my C:\PowerShellOutput\ folder

    The script has a parameter that is the name of the SQL Server instance that you want to run the query against, you specify that with -InstanceName after the filename and you then supply the name of your instance, in my case it is localhost

    Run the PowerShell script like this

    1. powershell.exe "D:\Powershell\FormatOutput.ps1" -InstanceName localhost

    You can run it from a command prompt or from the PowerShell Integrated Scripting Environment (ISE)

    Now go in your folder where the file has been created, if you open it in notepad or if you do view source when you open it in a browser, you will see something like this

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <title>localhost FreeSpace</title>
    5. </head><body>
    6. <table>
    7. <colgroup>
    8. <col/>
    9. <col/>
    10. </colgroup>
    11. <tr><th>name</th><th>create_date</th></tr>
    12. <tr><td>sp_sqlagent_is_srvrolemember</td><td>9/29/2010 10:13:10 AM</td></tr>
    13. <tr><td>sp_verify_category_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    14. <tr><td>sp_verify_proxy_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    15. <tr><td>sp_verify_credential_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    16. <tr><td>sp_verify_subsystems</td><td>9/29/2010 10:13:11 AM</td></tr>
    17. <tr><td>sp_verify_subsystem_identifiers</td><td>9/29/2010 10:13:11 AM</td></tr>
    18. <tr><td>sp_verify_login_identifiers</td><td>9/29/2010 10:13:11 AM</td></tr>
    19. <tr><td>sp_verify_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    20. <tr><td>sp_add_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    21. <tr><td>sp_delete_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    22. </table>
    23. </body></html>

    All I have to do now is create this part of the code in my blogpost

    1. <div class="tables">
    2. <table>
    3. <! Paste stuff here  !>
    4. </table>
    5. </div>

    I then copy and paste the rows from the html table between the table tags on line 3 in the previous code example

    Here is what I end up with

    1. <div class="tables">
    2. <table>
    3. <tr><th>name</th><th>create_date</th></tr>
    4. <tr><td>sp_sqlagent_is_srvrolemember</td><td>9/29/2010 10:13:10 AM</td></tr>
    5. <tr><td>sp_verify_category_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    6. <tr><td>sp_verify_proxy_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    7. <tr><td>sp_verify_credential_identifiers</td><td>9/29/2010 10:13:10 AM</td></tr>
    8. <tr><td>sp_verify_subsystems</td><td>9/29/2010 10:13:11 AM</td></tr>
    9. <tr><td>sp_verify_subsystem_identifiers</td><td>9/29/2010 10:13:11 AM</td></tr>
    10. <tr><td>sp_verify_login_identifiers</td><td>9/29/2010 10:13:11 AM</td></tr>
    11. <tr><td>sp_verify_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    12. <tr><td>sp_add_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    13. <tr><td>sp_delete_proxy</td><td>9/29/2010 10:13:11 AM</td></tr>
    14. </table>
    15. </div>

    And that will then look like this

    namecreate_date
    sp_sqlagent_is_srvrolemember9/29/2010 10:13:10 AM
    sp_verify_category_identifiers9/29/2010 10:13:10 AM
    sp_verify_proxy_identifiers9/29/2010 10:13:10 AM
    sp_verify_credential_identifiers9/29/2010 10:13:10 AM
    sp_verify_subsystems9/29/2010 10:13:11 AM
    sp_verify_subsystem_identifiers9/29/2010 10:13:11 AM
    sp_verify_login_identifiers9/29/2010 10:13:11 AM
    sp_verify_proxy9/29/2010 10:13:11 AM
    sp_add_proxy9/29/2010 10:13:11 AM
    sp_delete_proxy9/29/2010 10:13:11 AM

    Pretty cool and simple

    Next steps
    I am no PowerShell expert and I am sure that the script in this post can be improved, leave me a comment if you see a major mistake

    Of course you don't want to hard code the query either, you could make the script generic so that you can supply the query as a parameter instead

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    617 views
    Instapaper

    2 comments

    Comment from: Aaron Nelson [Visitor] Email · http://sqlvariant.com/wordpress/
    Aaron Nelson Dennis,

    Might I suggest this alternative approach for those readers who haven't done much work with PowerShell yet?

    Get-PSSnapin *SQL* | Add-PSSnapin


    Invoke-Sqlcmd -ServerInstance localhost -Database msdb -Query "
    SELECT TOP 10 name,create_date
    FROM sys.procedures
    ORDER BY create_date" |
    ConvertTo-Html -property name, create_date
    01/19/12 @ 09:33
    Comment from: SQLDenis [Member] Email
    SQLDenis Aaron, here is how I modified your script to do the output to the file


    Get-PSSnapin *SQL* | Add-PSSnapin

    $InstanceName = "localhost"
    $FilePath = "C:\PowerShellOutput\" + $InstanceName+ "_formattedOutput.htm"
    $Title = $InstanceName+ " Formatted Results for Blog"

    Invoke-Sqlcmd -ServerInstance $InstanceName -Database msdb -Query "
    SELECT TOP 10 name,create_date
    FROM sys.procedures
    ORDER BY create_date" |
    ConvertTo-Html -property name, create_date -title $Title > $FilePath

    01/19/12 @ 10:32

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)