Automatically Save Query Results to Excel - sql-server

I have a simple query that I want to run and save the results in excel file on daily basis.
I am trying to create a SSIS(SQL Server Integration Services) package to automate this thing.
My query is
select * from Customers c , Orders o where c.Company = 'Company A'
I was able to create a package and run it using Integration Service, but the issue is that when I make changes in the database and re run the package, instead of overwriting the existing excel, it simply appends the new rows. Is there any way that I can overwrite the existing excel?
The second issue is, I can't see the option to schedule the package under integration services. When I right-click on the package name, I see the following
There is no option to schedule the package.
Is there another way, I can achieve the same purpose? I tried creating a job also -
I choose the target file as an excel file which was already not present in the system -
After the job executes successfully, it creates the excel but I can't open it, it says the file is corrupted.
EDIT : My SSISDB doesn't show any packages. When I created the package, it showed me two options, either store it on sql server or file system, so I chose file system -
Steps how I created the package -
I right clicked the database -> Export Data -> specified the data source-> specified the above query-> saved the ssis on file system .
Initially, my package was not automatically present under the file system.I had to import it manually.
I followed the steps from here -
https://www.mssqltips.com/sqlservertutorial/202/simple-way-to-export-data-from-sql-server/

Related

Schedule the SSIS package to run on a schedule

I am building my first SSIS package which imports a flat file from a folder on File share into a SQL Sever DB table. I have another settings table on the SQL server DB where the Start time is stored, I need to query this table and run my package at that time. If the Settings table has 13:00 as start time, I need to run the SSIS package at 13:00 and check if the file exists in the file share, if the file exists it is imported in to DB else I need to send out an email notification to email alias.
I have built the package that can import the data from the FF to SQL server DB and it is working as expected. But wanted know if the scheduling piece is doable and how we can achieve that. Any suggestion/help is greatly appreciated
You should add the file exists and email steps to the SSIS package that way the one single package can take care of that.
As for the scheduling, you can do that yes.
Create an Agent job that runs say hourly with 2 steps
step 1 can call a stored proc or run your tsql code that does the checks you need, if you exit it with a success code it will then run step 2 which would be your SSIS package.

SSIS package to load XLSX to SQL fails when run in Agent with Unexpected Termination

Let me start by saying I've read just about everything on 'Unexpected Termination' errors the last few days and am none the wiser!
I have a number of XLSX files in a network folder, and have 2 packages in a project to read them and load to SQL server (2017) using Excel connector (ACE.16) on dev and prod.
One package loops through about 35 workbooks and appends them all to a single SQL table.
The second package simply reads a single (~5mb) file and writes to a SQL table (with a single data transformation along the way - its not that, I removed it and it still failed)
Both packages use same source folder.
Both packages write to same destination DB.
Both packages run fine on my local machine.
Both packages can be executed correctly from the SSISDB catalog on the server (Right-click-->Execute).
When scheduled in Agent (we have a proxy account with correct folder permissions) the looping package runs just fine, whilst the simpler package fails with 'Unexpected Termination'
Verbose logging reveals nothing, as does Event Viewer on the SQL box.
I started to look into other options such as converting to CSV first using Script Task and Excel Interop but we are a 365 site now so we'd need Office client installs on dev and production server (and not knowing much C# won't help!)
My next route is to see if I can get it to write to CSV destination successfully. If so then I might be able to go XLSX-->CSV-->SQL without having to use Interop or external libraries.
Unfortunately I've not been able to turn up anything further in my searches, so wondered if someone more enlightened than me might be able to suggest where to look next.

Create a Job for exporting a SQL Server view into Excel on a daily basis

I am using SQL Server; is there a way to create a scheduled Job which is going to take a view and export it into an Excel file every day?
With the addition of: creating a new folder named by the timestamp, and the file name will have the timestamp as part of its name as well, something like
C:/excel/221120170830/name221120170830.exl
I tried looking around but so far I couldn't find any way to do it.
Maybe I am missing something?
Yes, basically you need to combine 3 technologies:
SQL Server Agent Jobs
Powershell
Export Data Wizard/SSIS package
The idea is to create a job with as a first step, a Powershell script that checks if a folder exists and if not, creates it. The next step executes the SSIS package you have created, following the guidelines in the above link.
The tricky part may be uniquely naming your Excel file, but you first export the file to a temporary location and then, using another Powershell step, rename it and store it in the correct folder.

SSIS package doesn't write data to excel destination

I have an simple SSIS package and I'm trying to export same set of data from a table to both flat file and excel destination. The package works fine when I run locally and it creates both text file and excel file with data.
But when deployed to a different server the sql agent job runs fine and the log inside integrations services catalog for the package says it wrote like 9000 rows to excel, and a new excel file is also created but it doesn't write any data to it(blank with just headers). text file works fine and it has all data I need.
SSIS package flow:
I'm working with Sql server 2014, Visual studio 2013 with SSDT and used Excel 2007 in excel destination.
We had the same issue.
The solution is that the user, which runs the SSIS package, must have full access to c:\users\default.
You can check this by running sysinternals' process monitor on the machine that executes the SSIS job.
You can find more information here:
Empty Excel File permissions issue: SSIS Excel Destination buffers large record sets through C:\Users\Default - This post made me find this solution
https://www.csopro.de/biblog/2018/04/ssis-fehlerbehebung-bei-excel-destination-schreibt-keine-zeilen/ - my blog. Here I describe the issue - unfortunately in German]
I had the same problem writing to several worksheets in an Excel file from a scheduled SQL Agent job. It worked fine for about 4 months. Then suddenly with no changes to the package, one of the 5 worksheets was no longer populated with data. No error message generated and it worked fine on every test from Visual Studio and Data Tools (the old "BIDS" tools as we used to call it.)
I never did find a solution and it continues to not write any data to that single worksheet of the 5 in the Excel file. (So answers above about the Account that the job runs under from SQL Agent does not have the appropriate permissions is NOT a correct answer for this issue.)
Plus, a new package I built today is having the same issue, only this one has only a single worksheet. Again, works fine in the development environment, but no data appears in the destination file and no errors. Not only that, but the timestamp on the file is the same as the template file -- it seems that it never even TRIES to write to the file.
Checking each run log for the package in the Integration Services Catalog has an entry in each log that shows 9K+ records "written" for the dataflow task.
Lastly, if I change the destination file name, the SQL Agent job generates the expected error, so that rules out answers that guess that the path is wrong.
This is bizarre. And exasperating.
I have encountered odd behaviour when using scheduled SSIS packages which use the Excel object.
The fix for me, was to edit the Agent Job properties. On the Execution Properties tab, try enabling the "use 32-bit runtime" option and force the SSIS to run in 32-bit mode instead of 64-bit mode.

Is there a way to easily change the server name on several SSIS packages programmatically?

We are creating several SSIS packages to migrate a large database as part of a release cycle.
We may end up with about 5-10 SSIS packages.
As we have 4 environments (dev, QA, staging, production, etc.), is there an efficient way to change the destination server for each SSIS package as they go through the different server environments? Ideally, there could be a script that is run that would take as a parameter the server that was needed.
You could use a configuration file to store the connection strings for the servers. Then as you moved from environment to environment, you would simply change the config file. To simply create a config file, on the control surface of your package,
1) right click and choose Package Configurations from the context menu.
2) Check the box for Enable package configurations if it is not already selected,
3) then Click the Add... button.
4) Click next on the dialog,
5) then add a Configuration file name: and click next.
6) In the Objects View, Under Connection Managers, expand your connection, then expand Properties and check the box next to ConnectionString.
7) Then click next
8) then finish.
You now have an xml file named what you named it in step 5 above. You can edit this file with a text editor and change the connection string to map to whichever server you need it to before each run.
Once created you can share the config file between multiple packages as long as the objects referenced are named the same between the packages.
This is a rudimentary tutorial on configurations, there are many ways of saving configurations of which this is only one. For more information on configurations consult your favorite SSIS book
We use a config table that stores the configurations for the server. But config files work well too. We like the table because we are doing reporting on SSIS package meta data and it's easier to grab this data (along with a lot of other data we store as well) when stored in a table.
William Todd Salzman's answer covers most points. I have a couple more to add:
Make sure the pacakge ProtectionLevel property is DontSaveSensitive
If you are working with different shipping environments, then a SQL Server table as a source for the package configurations is maybe not for you, as you will require one central database containing all the connection strings for all the servers.
Having worked with package configurations retrieved from the registry, you will need to be aware that these settings are retrieved from the HKEY_CURRENT_USER hive. This has implications for when the package is run through a SQL Agent Job.

Resources