Export excel data to sqlserver - sql-server

What's the correct way of exporting data from Excel 2013 file to SQL Server database? The data from the Excel file should be transferred into SQL when saving excel file to a database.
I know many answers for this are available but my question is bit different: every time the excel data changes, or the user clicks on save button the data in the database also needs to be updated.

The easiest way to do this is with an SSIS package. SSIS (SQL Server Integration Services) is a package built in to SQL which allows transformations between data formats.
You can create a package by right-clicking on your target database in SQL Server Management Studio and selecting Tasks > Import Data. In the wizard that comes up asking for a data source choose "Microsoft Excel" from the top drop-down labelled as Data Source, then follow the wizard through. You'll have the choice of importing the Excel data into a new table or mapping it into an existing table.
If you want do this programmatically, you can save your package at the end of the wizard and then invoke it via code. But that's a different question.

What you want is not possible (as long as I know off). You can use SSIS package to migrate the Excel sheet into SQL Server, but is imposible to determine if someone "click save" or do some changes on the excel file. SSIS package can be programed to run on schedule or by demand. You should investigate SSIS packages. It is not easy to learn, but do what you need.

Maybe you would like to try a tool I have developed? It's an Excel Add-In that exports Excel data to SQL Server.
There is a feature to automatically export the data to SQL Server every time you press the save button in Excel. If you need to update the database every time a cell value change, you will need to add a few lines of VBA-code that will push the data to SQL Server.
To beta testers I currently give away a free license, so if you are interested in testing it out, send me an email:)
www.sqlpreads.com

Related

How to periodically update SQL Server table from an Excel sheet

For my .NET Application I have an Excel file that is used as the data source. This Excel file is updated every hour. My current back-end database technology is MS Access. The Access file has a linked table to the mentioned Excel file. I would like to switch over my back-end to SQL Server.
My question is how can I create a linked table in SQL Server such that I always have the current data in my SQL Server database. Right now I have managed to import the Excel sheet through the import wizard in SQL Server Mgmt. Studio. But obviously this is static (not updated hourly like the excel sheet).
Can I create some sort of procedure that runs every hour, reads the Excel sheet and import to my SQL Server database? How would I go about doing that? Or any other method as long as my table in SQL Server gets updated on an hourly basis with data from the Excel sheet.
One, do not code read "in process" from the excel file. That is a disaster waiting to happen. Some user opens the file, locks it, and now your sql-server starts failing. :) That is a bad scenario.
You should create an ETL package to run (hourly)?
https://learn.microsoft.com/en-us/sql/integration-services/ssis-how-to-create-an-etl-package?view=sql-server-ver15
What is SQL Server Integration Services (SSIS)? MicrosoftSQL Server
Integration Services (SSIS) is a platform for building
high-performance data integration solutions, including extraction,
transformation, and load (ETL) packages for data warehousing. SSIS
includes graphical tools and wizards for building and debugging
packages; tasks for performing workflow functions such as FTP
operations, executing SQL statements, and sending e-mail messages;
data sources and destinations for extracting and loading data;
transformations for cleaning, aggregating, merging, and copying data;
a management database, SSISDB, for administering package execution and
storage; and application programming interfaces (APIs) for programming
the Integration Services object model.
data sources and destinations for extracting and loading data;
Deploy and Schedule:
https://www.mssqltips.com/sqlservertutorial/9069/deploy-and-schedule-an-sql-server-integration-services-ssis-package-step-by-step/
Scheduling the SSIS Package with SQL Server Agent
Manually executing packages is one thing, but normally you will schedule packages so your ETL can run in a specific time windows
(probably at night). The easiest option is SQL Server Agent. You can
right-click on the Jobs node to create a new job:

How to export result of SQL server - "New Data Comparison" to an Excel file?

I am using Visual Studio 2017.
Functionality - Open Visual Studio - Go to Tools - Click SQL Server -
New Data Comparison
After mentioning source and target database and running the tool, I get results -
(Different Records, Identical Records)
Is there a way to export this data result to Excel?
Harshit Virdi has noted he has access to Microsoft SQL Management Studio. There are two easy ways I know of to export results into Excel:
Option 1 the "right click on your results" option:
Run your query like normal in Microsoft SQL Management Studio. Then right click on the results like this:
.
Choose "Save Results As..." and if you pick CSV (comma separated value) you'll be able to open that in Excel.
Option 2 would be to Export from a table or view directly. Right click on the database (not the table) you want to export from, choose tasks, and Export Data...:
A wizard will appear (ha!)
Choose your Data Source (SQL Native Client)
Pick your server and database
Choose your Destination
Select Excel, and pick a path. I keep it at Excel Version 97-2003 but you can try different ones
Now you can choose to either write a query or pick a table, the choice is yours!
Hit next and finish it up and boom, you have your data in Excel.
The export data wizard is rather powerful, you can also transfer data between servers very fast and easily. It's a tool worth learning more about. You may have noticed on the last steps of the wizard you can save your 'code' as an SSIS package. That way you could run the same export again, or automate it. However that goes beyond the scope of this question. Good luck!

SQL Server Management Studio Issues

I accidentally deleted YEARS of data in SQL Server Management Studio from a table. Unfortunately the person in this position before me didn't back anything up. Nor did I before I tried to fix an issue. I understand that it cannot be retrieved from SQL but I have all the data I need in a separate file on my desktop. Is their anyway to get that data and input it back into the table that is in SQL? Or is there a query I can run to input the data again into the table? I'm not sure if I am making any sense :/
You can also used Management Studio without SSIS. Right click on the database in MS and select Tasks -> Import Data. You should then be able to select the type of source (flat file) and the format. The rest of the wizard is pretty self-explanatory.
If it is a flat file like .txt or .csv or even an Excel file like(.xls), you can build an SSIS package and dump the data to a new table. Depends, on what kind of data you have in your hand.

Find and log missing/wrong named columns in Excel using SSIS 2012

I am new to SSIS. I am using SSIS 2012 to transfer data from Excel(COZYROC Excel Source Plus component) to SQL server database(OLE DB Destination). My requirement is whenever columns in the excel are not matched with mapped columns or any columns are missing, I should log the error message in the database.
Please help to resolve this problem.
I don't believe that is possible.
SSIS (and SSRS and other applications) require a 'contract' between source and destination such that any changes to the source will throw a mapping error and force the developer to re-map the data flow.
SSIS is not capable of a scenario such as 'if the source columns change, pump the rest and log the changes to another path'.
This is also an example of why Excel makes a terrible data source for a normalized database/ETL project, because users can easily change the Excel doc in such a way that would break data mapping.
Good luck.

Mimic DTS Copy SQL Server Objects Task

I am in the process of migrating a web application database from SQL Server 2000 to SQL Server 2008. Currently there is a DTS package that is used to deploy content changes from a staging database to the production database.
The DTS package is using a Copy SQL Server Objects task with the following options selected: Copy Data (Append Data) and Use Collation. The specific tables to copy are selected in the "Select Objects" dialog.
Because this is the only DTS package we have, it doesn't make much sense to learn and implement an SSIS solution, IMO, so I want to recreate the functioning of the DTS package using only T-SQL.
Writing the Insert and Select is not a problem. What I need to know is how the "Append Data" option works.
Is it looking at each row in the source, finding matching rows in the destination, comparing and updating as necessary OR is is ignoring existing rows and simply appending new rows?
If it is indeed comparing and updating, is it safe to use the SQL Server Checksum function on the data as a method of comparison against the target or is there a better way? Ideally, I'd like to avoid any schema changes.
Please check this msdn article : Migrating DTS Packages to Integration Services
You might be able to migrate the single DTS package to SSIS package very easily using the tool noted in the article.

Resources