Dear,
Good afternoon.
In this post I will demonstrate how to export data from a SQL Server table to an HTML file or a variable of type VARCHAR, where two very similar Procedures will be presented, where one generates the HTML as a physical file on disk and the other writes the HTML generated in an OUTPUT variable.
I also made a post about this same functionality, but using the CLR, which allows exporting a query to HTML faster and more practical than using OLE Automation. If you want to know more, visit the post SQL Server - How to Email a Query Result in HTML Format Using CLR (C #).
Prerequisites
If you only need to use the procedure where HTML is returned as an OUTPUT variable, you can skip the prerequisites.
For the procedure where a physical file is generated on disk, I need you to have created the procedure stpWrite_File_Filewhich I talked about her in the post File Operations Using OLE Automation in SQL Server.
Remember that to use OLE Automation, this feature must be enabled on the server. If not enabled, see the post Enabling OLE Automation via T-SQL on SQL Server how to do this.
Creating a test data mass
1 2 3 4 |
IF (OBJECT_ID('tempdb..##Teste') IS NOT NULL) DROP TABLE ##Teste SELECT * INTO ##Teste FROM master.INFORMATION_SCHEMA.TABLES |
Returning HTML as an OUTPUT variable
This functionality should be used when you need to handle or perform some action with the generated HTML, such as sending an email with the data in this table, for example.
Example of use:
1 2 3 4 5 6 7 |
DECLARE @HTML VARCHAR(MAX) EXEC dbo.stpExporta_Tabela_HTML_Output @Ds_Tabela = '##Teste', -- varchar(max) @Ds_Saida = @HTML OUT -- varchar(max) PRINT @HTML |
Example usage with centered text and sorting results by TABLE_NAME column:
1 2 3 4 5 6 7 8 9 |
DECLARE @HTML VARCHAR(MAX) EXEC dbo.stpExporta_Tabela_HTML_Output @Ds_Tabela = '##Teste', -- varchar(max) @Ds_Saida = @HTML OUT, -- varchar(max) @Ds_Alinhamento = 'center', @Ds_OrderBy = 'TABLE_NAME' PRINT @HTML |
Source code
View source
Exporting HTML to File
This functionality should be used when you need to generate a physical file on disk or network containing the contents of a table in HTML format.
Example of use:
1 2 3 |
EXEC dbo.stpExporta_Tabela_HTML @Ds_Tabela = '##Teste', -- varchar(max) @Ds_Arquivo_Saida = 'C:\Teste.html' -- varchar(max) |
Source code
View source
That's it, guys.
To the next!
I already found it in another post
Oops, wonderful!
Hello Dirceu, I couldn't find the procedure code 'dbo.stpEscreve_Arquivo_FSO' that appears
Hi Jorge,
All right?
First, thanks for stopping by! The code of this SP is in the post https://www.dirceuresende.com/blog/operacoes-com-arquivos-utilizando-ole-automation-no-sql-server/, as I explained there in the “Prerequisites” part, right at the beginning of the post 🙂
If you have any questions, do not hesitate to ask!
Hug!
Dirceu, good morning. Come accompanying your blog, congratulations excellent tips.
Ask me a question, I'm setting up a query to be sent via email, I already have everything configured, I can have more than one table in sending the email?
After the N ”I added a + to add another table:
N ”+
In Detailed Billings: DUTRA '+
N ”+
I am asking you this as the email is being received blank.
Thank you.
I managed to solve lol.
I added several tables, and at the time of print put as isnull too.
That's it, Thiago! You can also do this by creating 2 variables: One will get the EXEC result for each table and sum the result in the other variable at the end, using ISNULL ().