Reading Time: 5 minutesHello people,
Good night!
In this post, I will talk about a very interesting feature of SQL Server that works as a major differential of the database, which is the ability to natively integrate with Excel, allowing you to query and manipulate spreadsheets through the database without having to from any other external resources.
To make this possible, you need to install the OLEDB ACE or JET drivers to integrate with Excel. To learn more about this, visit the post. SQL Server - How to Install the Microsoft.ACE.OLEDB.12.0 and Microsoft.Jet.OLEDB.4.0 Drivers.
Sample worksheet that will be used in this post:


Importing Excel Data into the Bank How to Import an Excel Spreadsheet into SQL Server
The most commonly used form for sure is reading data from an Excel spreadsheet to the SQL Server database. For this, we will use OPENROWSET and ACE OLEDB 12.0 driver, since my operating system and my bank are in 64 bits version and using an Office 2016 spreadsheet (JET plugin supports up to 2003 version).
The basic command for reading is like this:
| SELECT * FROM OPENROWSET ( 'Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Teste.xlsx;', 'SELECT * FROM [Aba1$]' ) |
Example:

Another way, which makes this integration much easier, is to create a Stored Procedure to make the data easier to use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | CREATE PROCEDURE [dbo].[stpImporta_Excel]( @Caminho VARCHAR(5000), @Aba VARCHAR(200), @Colunas VARCHAR(5000) ) AS BEGIN DECLARE @Exec VARCHAR(MAX) SET @Exec = 'SELECT * from OPENROWSET (''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;Database=' + @Caminho + ';'', ''SELECT ' + @Colunas + ' FROM [' + @Aba + '$]'') A' EXEC(@Exec) END |
And then you can use it like this:

You can return only specific columns as well:

Or import the result into a database table:

Exporting Bank Data to Excel How to enter / export data from SQL Server to Excel
Quite simply, you can also enter data from our database into an Excel spreadsheet. The basic syntax is as follows:
| INSERT INTO OPENROWSET ( 'Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Teste.xlsx;', 'SELECT * FROM [Aba1$]' ) SELECT 'Paulo', 32, 2584.44 |
Insert Example:

Query example proving that the record was entered:

Inserting from a table:

Once again, we can use a Stored Procedure to facilitate day-to-day operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | CREATE PROCEDURE [dbo].[stpInsere_em_Excel]( @Caminho VARCHAR(MAX), @Aba varchar(200), @Tabela varchar(200), @Colunas varchar(MAX) ) AS BEGIN IF (@Colunas = '*') BEGIN SELECT @Colunas = isnull(nullif(@Colunas,'*') + ',','') + b.name FROM sysobjects a WITH(NOLOCK) JOIN syscolumns b WITH(NOLOCK) ON a.id = b.id WHERE a.xtype = 'U' AND a.name = @Tabela END DECLARE @Exec VARCHAR(MAX) SET @Exec = 'INSERT INTO OPENROWSET (''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;Database=' + @Caminho + ';'', ''SELECT ' + @Colunas + ' FROM [' + @Aba + '$]'') ' + 'SELECT ' + @Colunas + ' FROM ' + @Tabela EXEC(@Exec) END |
Example of using procedure:

Updating Excel Data by Bank How to update data from an Excel spreadsheet through SQL Server
Like other operations, you can also update data from an Excel spreadsheet through SQL Server. The basic syntax is this:
| UPDATE A SET A.Nome = 'Teste 1' FROM OPENROWSET ( 'Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Teste.xlsx;', 'SELECT * FROM [Aba1$]' ) A WHERE A.Nome = 'Paulo' |
Example:

Example with JOIN using local database tables:

Stored Procedure to make everyday life easier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | CREATE PROCEDURE [dbo].[stpAtualiza_em_Excel]( @Caminho varchar(max), @Aba varchar(200), @Tabela varchar(200), @Colunas_Join varchar(max), @Colunas_Update varchar(max) ) AS BEGIN DECLARE @join VARCHAR(MAX) , @update VARCHAR(MAX); SELECT @join = ISNULL(@join + ' and ', '') + 'a.' + LTRIM(RTRIM(s)) + ' = b.' + LTRIM(RTRIM(s)) FROM dbo.fncQuebra_Texto(@Colunas_Join, ',') AS a; SELECT @update = ISNULL(@update + ',', '') + 'a.' + LTRIM(RTRIM(s)) + ' = b.' + LTRIM(RTRIM(s)) FROM dbo.fncQuebra_Texto(@Colunas_Update, ',') AS a; DECLARE @Exec VARCHAR(MAX) SET @Exec = 'UPDATE A ' + 'SET ' + @update + ' FROM OPENROWSET (''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;Database=' + @Caminho + ';'', ''Select * From [' + @Aba + '$]'') A' + ' JOIN ' + @Tabela + ' b' + ' ON ' + @join EXEC(@Exec) END |
FncQuebra_Text function required to use SP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | CREATE FUNCTION [dbo].[fncQuebra_Texto] ( @str NVARCHAR(4000) , @separator CHAR(1) ) RETURNS TABLE AS RETURN ( WITH tokens ( p, a, b ) AS ( SELECT 1, 1, CHARINDEX(@separator, @str) UNION ALL SELECT p + 1, b + 1, CHARINDEX(@separator, @str, b + 1) FROM tokens WHERE b > 0 ) SELECT p - 1 zeroBasedOccurance , SUBSTRING(@str, a, CASE WHEN b > 0 THEN b - a ELSE 4000 END) AS s FROM tokens ); |
SP Usage:

Unfortunately, erasing Excel spreadsheet data from SQL Server is not possible. If you try, you will encounter this error message:
OLE DB provider “Microsoft.ACE.OLEDB.12.0” for linked server “(null)” returned message “Deleting data in a linked table is not supported by this ISAM.”.
Msg 7345, Level 16, State 1, Line 1
That's it folks!
A hug and thanks for visiting.
sql server import read export update data from excel spreadsheet to bank import read export data spreadsheet datasheet
sql server import read export update data from excel spreadsheet to bank import read export data spreadsheet datasheet
Goodnight! I have a base in Excel and I brought it to SQL, but how do I update the entire table in SQL? Example I added another line in excel with more information and now I want it to bring in SQL.
TOP !!… I'll look here how to do this with CSV files,
Thanks