Here's how you can connect Excel to an SQL database
- Both Excel and SQL databases have their pros and cons.
- But most people are more familiar with Excel than a database.
- For this reason, you might want to turn your Excel into a sort of SQL database "front end."
Microsoft Excel is one of the best spreadsheet programs on the market, but there comes a time when even it cannot provide all the data handling and processing you need. While this is usually an excellent time to leap into a database, there is a way to keep your beloved Excel while also getting the added benefit of a relational database.
Welcome to the world of Excel-SQL connectivity. But where do you begin? Let's find out.
Before we begin, if you are familiar with databases and spreadsheets, you may want to skip ahead a few sections.
What are the benefits of connecting Excel to an SQL Server?
Before we get stuck into the main topic, you might be wondering why you'd even bother trying to connect an Excel spreadsheet to an SQL database.
Well, you can take advantage of a database's much better way of handling data and combine it with Excel's much easier way of customizing, analyzing, formatting, and displaying data.
You get, in other words, the best of both worlds.
As we've previously written, spreadsheet applications like Microsoft Excel are excellent software for data collation and fundamental analysis. But it isn't as powerful as a database like SQL Server in several ways, as you are about to find out.
We'll cover the main points briefly below.
1. Data volume and data type handling are far superior with a database
One of the main benefits of a database is the sheer volume of data it can handle. If you have large data sets, then spreadsheets can struggle to handle it, and sharing such data is far from convenient when workbooks balloon it to megabytes.
Because databases are far superior at storing information and can handle large amounts of information, they can reach sizes that would be far too much for a spreadsheet to handle. Spreadsheets also tend to have limits on how many records they can hold, but databases do not.
For information, according to Microsoft, Excel caps out at around 1,048,576 rows by 16,384 columns. Not only can databases surpass this, but they can also be accessed many times with little to no degradation in performance.
While a little harder to quantify in terms of rows (i.e., records), typical SQL servers can handle up to 2,147,483,647 objects of all kinds (tables, queries, all data from all forms, etc.). For most applications, this is effectively limitless.
However, that does depend on the kind of data you store. In other words, if you have fields with loads of text or commonly embed images within records, this will drastically reduce the total amount of records a database can handle.
Unlike databases, spreadsheets can take up much more space on your hard drive to store data. When a spreadsheet has a lot of data or a lot of fields (thousands of rows), it can also be hard to read. While filters and custom views can help, they are very cumbersome compared to the query capabilities of a relational database.
Databases can also handle documents and other types of data that spreadsheets may not be able to handle.
2. Data editing is much more efficient using a database
Another of the main benefits of databases over spreadsheets is the data editing functions that databases can provide. This is especially true if the same or related information is kept in multiple records or spreadsheets.
One key example would be data sets requiring you to select data from a list. On a spreadsheet, this would typically need to be stored on a separate sheet to provide the source of a lookup for a cell. In a relational database, on the other hand, the separate sheet could be turned into its own table and linked to that field on the main table where the data is collected.
You can easily add and remove rows from such a table, and the rest of the database can be updated automatically. Data can also be amended en masse with databases using relatively simple "update" queries.
3. Database user access and security blows spreadsheets out of the water
Another major benefit of databases over spreadsheets is the ability for multiple users to access or add new data sets simultaneously. While this is quasi-possible with shared spreadsheets, the process is more clunky.
In fact, according to Microsoft, an SQL Server can handle up to 32,767 user connections at any one time!
For this reason, databases are the best choice when you want to share information and have a team collaborate on it. Since more than one person can access and update the database simultaneously, it is more efficient and may be less likely to lead to mistakes.
4. Data integrity is better with a database
The main difference between databases and spreadsheets is how data is kept safe. Relational databases use standard rules to ensure that the data they store is correct and easy to find. Database fields can only usually accept certain data types, formats, or lengths.
For example, free text fields can be set to accept a certain maximum number of characters, or number fields can only accept values in a particular format.
Technically called "referential integrity," this principle of databases ensures that the data stored is correctly formatted and consistent. These rules include using primary keys and setting up relationships between data tables. Using valid or reference values is another way to limit data and stop people from making mistakes when entering data.
5. Databases help reduce data replication
If the above is not enough to convince you, then a database's ability to reduce data replication is another key benefit compared to spreadsheets. This is especially the case if you use a "template" Excel spreadsheet to create multiple copies of a spreadsheet for a large number of projects.
With a database. you only need to enter data once, and this same information can be accessed and referenced as often as you'd like.
Spreadsheets, by comparison, often result in data being copied or entered more than once (aka duplicated), and the same data can also (usually) be kept in different spreadsheet files. For example, you may have one spreadsheet of accounts for each month. A database, however, can store all months on the same table and summarize by the current or past months with a simple query.
This makes it hard to make sure that the data is correct when a change is needed. A database can also eliminate the problems that come with having a lot of spreadsheets with similar information and having to keep many copies of spreadsheets for version control.
6. Query speed is one of the main benefits of databases
Even though data can be sorted and filtered in spreadsheets, and there are ways to reduce data replication with spreadsheets, a database has much more powerful querying tools that can get all records that match specific criteria, cross-reference records in different tables, and do complex aggregate calculations across multiple tables.
What's more, this can be done relatively quickly as, unlike spreadsheets, databases are built explicitly so that a user can look up information without having to load all of it into a computer's memory. For this reason, databases usually work faster than spreadsheets when dealing with large amounts of data.
Databases also allow you to sort and show data in ways that would be almost impossible to do with a spreadsheet. Using stored procedures, many databases also have ways to automate how queries and reports are made.
Comparisons across tables are also far easier on a database than comparing many different, separate spreadsheets. It is a different world by comparison.
When data are kept in a centralized relational database, it's easy to query, analyze, and report on the data. Since the same quality standards will be used for all datasets by the database, decisions can be made confidently.
How are Excel spreadsheets better than databases?
We hope you've gained an understanding of the power of databases now. But, you might now be wondering, what is the point of using a spreadsheet in the first place?
Wouldn't it be better to simply dispense with Excel and gather, store, and handle all your data using the database alone? The short answer is yes, but since databases can be pretty complex to create and use in their "raw" form, they are not ideal for the average user.
Most people are more familiar with Excel, making it an ideal "frontend" to a database.
If you are unaware, frontends are a graphical user interface, making it easier to gather, store, and/or retrieve and display data from a database. Things like applications and website forms are prime examples of frontends used to interact with a database that you may already be very familiar with.
Specifically designed for ease of use by most users, these are the best way to allow a non-database veteran to communicate with a database without needing advanced technical skills. But, there are other benefits to using a spreadsheet like Excel in this way too.
Regarding data entry, spreadsheets like Excel are hard to surpass. Excel's most powerful features are the ability to quickly transition rows and columns, copy and paste data, filter, and sort on the fly.
It is fairly intuitive, and most people can master it relatively rapidly. Attempting to do the same on a database can be very challenging, especially if you don't want general users to have access to the raw data tables.
Other benefits of spreadsheets over databases include, but are not limited to: -
- Spreadsheets, like Excel, tend to have a relatively shallow learning curve to get to grips with them.
- Spreadsheets do offer more accessible ways to format data and visualize it. For example, creating graphs and charts is usually a much simpler process than all the required queries and forms that are often required on a database.
- Spreadsheets often come with preset data manipulation functions that can be easily accessed and understood.
- Spreadsheet formatting and design are considerably easier using programs like Excel. Building data entry forms or reports can be taxing using a pure database.
So, as we mentioned above, it is a good idea to get the best of both worlds and benefit from the advantages of both databases and spreadsheets. Connecting them makes the Excel spreadsheet fancy, readily customizable, and editable frontend.
What's not to like?
How can you send Excel data to an SQL Server?
When you consider connecting an Excel workbook to an SQL database, the first consideration is whether or not the SQL database is located locally on an internal network or remotely hosted on a website. This is very important to know, as the process of connecting them varies widely in complexity.
The former, i.e., connecting to a "local" SQL database, is the simpler of the two options, so we'll deal with that first.
How do you connect Excel to an SQL server on the same network?
Ensure you've planned the tables required on the database for data collection and have matching tables for any preset data used for things like dropdown lists on Excel.
From here, you have a couple of options to choose from. We'll highlight some of the most common ones below.
1. Use Excel's built-in SQL connection tool
Main benefit: Best for non-advanced users to set up a connection between SQL and Excel
One option is to connect directly through Excel's database connection tool. This is one of the simplest methods and allows you to retrieve and write data to any number of tables on the database.
To do this, use any of the wizards available through the "Data" tab of your Excel spreadsheet. When you click on the tab, find the "Get Data" option and choose the most appropriate database option, such as "From Azure SQL Database." You will want to choose the SQL option from the "From Database" option on the dropdown list.
From there, simply follow the instructions, and choose which tables you want to import and where to save them (i.e., on an existing worksheet or create a new one for each table).
Make sure you also save the connection details for use later. Excel will guide you through this process too.
This is a great "no frills" way to connect an SQL database to Excel, but it is mainly intended to import data into Excel rather than "write" to the database. It is possible to do this, but it will require you to write some Macros/VBA functions with triggers.
This can be a little involved, but there are plenty of helpful tutorials.
2. Set up an ODBC to connect Excel to SQL
Main benefit: Best for large datasets or multi-table connections between SQL and Excel
This method creates an "open database connectivity" (ODBC) to the SQL Server. An ODBC is a multi-application driver that enables third-party software (like Excel) to connect to and talk to a database.
For reference, ODBC is one of the best ways to connect an SQL database to Excel as it is fast and stable, which is superb for large datasets or multiple connected tables. Using an ODBC with Excel effectively converts the workbook into a mini-relational database, which is excellent.
Another benefit of ODBC is that you transfer only the records you need from a particular SQL table rather than bulk import all records. This is preferred for small tables (like the ones you use to store standard options for a dropdown), but adding a new project to a thousands+ log of projects would be counterproductive.
To do this, search Windows OS for the ODBC Wizard, and follow the instructions.
Make sure you take note of the ODBC driver needed for your particular version of SQL Server and Windows OS. They vary, so check out this guide to learn how to get your hands on the required ones.
You will also need information on the SQL Server's network IP and user login details for the database. Like Windows and other operating systems, you can create users with varying access rights to the database, including administrator (full access) and basic user (limited read and write capabilities).
You will likely also need to know the names of the databases on the SQL Server. With all that in hand, follow the instructions to set up the ODBC connection.
With that done, you can open up your Excel spreadsheet and follow the same process as 1. above, except that the data source will be an ODBC connection.
With all that done, some additional steps are needed to get your Excel workbook up and ready to share data with an SQL database, but the results are worth the wait.
3. You can look into the ADO method
Main benefit: Best for connecting multiple databases to Excel
Another option to consider is ActiveX Data Objects or ADO for short. This method gives developers a powerful and logical way to access, edit, and update data from various sources like Excel and SQL Server.
ADO comes into its own if you want to connect multiple SQL databases to a single file, like an Excel workbook.
Most of the time, ADO is used to query a table or tables in a relational database, get the results and show them in an application, and maybe let users change the data and save their changes. However, according to Microsoft, with the right tweaks, it can also be used to do some, or all, of the following: -
- Using SQL to ask questions about a database and show the answers.
- Saving data from a database into an XML file.
- Running XML-described commands and getting an XML stream.
- Putting data into an XML or binary stream.
- Put data in database tables so users can look at it and change it.
- Creating parameterized database commands and using them more than once.
- Run some stored procedures.
- Use Recordset, a flexible structure that can be made on the fly to hold, navigate, and change data.
- Run database operations that involve transactions.
- Using run-time criteria to filter and sort local copies of database information.
- Get hierarchical results from databases and work with them.
- Binding database fields to components that understand data.
- Making Recordsets that are far away and not connected.
ADO gives you many options and settings to give you this freedom. so it is worth considering.
4. The SSIS method might be a bit over the top
Main benefit: Best for automating/scheduling large data dumps between Excel and SQL
Another option is to consider using an SQL Server Integration Service (SSIS). It is one of the most common methods used to extract, transform, and load (usually abbreviated to ETL) data. ETL processing is standard in data warehouse applications but can also be used for your Excel workbook and SQL.
- You can get information from almost any source
- You can change the data in different ways, such as changing it from one type to another, changing it to uppercase or lowercase, running calculations, etc.
- Load information into almost any source
- Set up a process flow
To this end, you can think of SSIS as a way to bulk move data between different sources. You can do almost any task you like, but you will need to put in lots of time to get started, and it will still take more time to maintain and change the solution.
However, the benefits of it include good versatility and plenty of available features. For most Excel workbooks, SSIS might be a little OTT, however.
5. Get a plug-in to do it all for you
Main benefit: Best for people with limited developer experience
If the above sounds like too much work, you can always consider getting your hands on a third-party Excel plugin to do all the heavy lifting. This is the most common option for people with limited developer experience, but it comes with a financial cost for the plugin's license.
There are a few different plug-in options, so make sure you weigh the pros and cons of each before making a decision.
How do you connect Excel to a remote SQL database?
Connecting to a remote SQL database can be trickier due to the inherent security concerns of sending data over an internet connection. But that's not to say it is not possible.
There are a few options available, but it is important to note that it may not be possible at all, for various reasons. The most common reason is that your Web Hosting Service doesn't permit remote connections other than VPNs. If this is the case, you will need to devise a workaround.
Another common problem is that there's a firewall or anything else that avoids remote computer connections to your website.
But that being said, you do have a few options.
1. Remotely connect to your local SQL server over the internet with ODBC
Suppose you have various remote agents who use a spreadsheet for data entry and analysis. You could have them send an email or share their spreadsheets using a cloud server or email attachment, but there is a far more efficient way of getting their data - ODBC once again.
The setup is similar to accessing a local server, but you must configure your network's firewall to make this possible. However, this can have some serious security risks, so ensure you follow instructions to the letter or consult an IT professional to help you out.
Once set up, you should be able to connect to and share data with your network SQL server over the internet from anywhere in the world with an internet connection. Just ensure that you keep a strict policy of securing the server's IP to prevent malicious access by third parties.
2. There is, of course, a plugin for that too
Just like any software problem you can think of, some bright spark out there has probably created a solution. And remote Excel to SQL connection is no exception.
With just a few minutes of searching on the internet, you will run into a few options. Most act as plugins you install and authorize to access your Excel spreadsheets.
From there, simply follow the instructions, and get your Excel talking to a remote SQL database in no time.
3. If all else fails, make a workaround
If none of the above options work for you, there are other workarounds you can try, but most will not allow full automated connectivity to a database. You may need, for example, to develop automated methods of dumping data as .csv files ready for upload to the database and Excel spreadsheet.
You could then, for example, automate the creation and storage of these files in a shared network folder/cloud server and create ways to automatically/periodically look for and impart them into Excel or SQL.
And that is your lot for today.
Excel and relational databases like SQL both have their pros and cons. Still, Excel alone is far inferior to a database when storing large data sets and reducing data redundancy, as we've explained above.
However, it is much more technically challenging to produce great GUIs for users who need to connect to database tables.
For these reasons, as we've explained above, get the best of both worlds and combine the two into the ultimate data-crunching tag team!