Extract Data from SQL to Excel without overwriting the previous Data - sql

I was able to use SQL Server Agent to create a job that extract Data from SQL Server 2008 to Excel Format Daily. However, is there anyway to create a job that keep all the extracts separately without overwrite the previous files? I would really much appreciated your help.
Thank you.

When using OLE DB/Jet data provider for Excel, there is a way to specify the target worksheet name. A worksheet in Excel is a rough equivalent of a database table. One option is to use a different worksheet name each time (say, based on a current date). Another option would be to append the data to the existing worksheet, if that's what you're after. If SQL Server Agent job does not allow you to do something like that, then you may want to create a small app instead.

While Copying from OLEDB to Excel, You can dynamically pass the file name through the expressions based on the Datetime.
Ex: Filepath+Date1_mmddyy_hhmmss
Every time the file generates, It will create a file with new file name and Excel file should be passed as an expression.

Related

How can I copy MS SQL database data for a date range to be appended to DB on another computer?

I have a User Interface program written with VB.Net that collects instrumentation data from some PLC's and stores it in a MS SQL database. I need to be able to copy records from the DB based on a range of dates and save them in a file on a thumb drive. Then the file will need to be imported to a DB on another computer for analysis. I know SSMS can do a backup and restore but I don't think it can be based on a date range.
Simplest route I can think of would be (if the two DB are the same layout, table names etc) to download data, using a DataAdapter, into a DataTable and write it to disk with DataTable.WriteXml() then at the other end, DataTable.ReadXml() to get it from file back into a DataTable and write it into the destination DB with a DataAdapter. You'll need even fewer lines of code if you use strongly typed datatables (create a dataset)

VBA to send a SQL string to a Tableau Connection

I have an excel work book that allows users to update multiple SQL statements at once based on data entered into cells.
Then the users copy the updated SQL and go into Tableau and paste it into the corresponding custom SQL data source in tableau and refresh it.
Is there a way to send the updated SQL code directly to the corresponding connection in Tableau?
If the table name is changing, that presents a challenge.
I have two suggestions.
Modify the twb xml.
A Tableau workbook file is simply an XML file. One part of the xml contains the connection information, including your custom SQL. VBA has some libraries for manipulating XML. You can write some custom VBA code to modify the XML that contains the custom SQL.
Use VBA to create/alter a view in teradata
Teradata allows database views. With VBA, you can connect to teradata and create/alter a view that changes based on your parameters from Excel. The key is to keep the view name constant, then Tableau will not need to be changed each time the underlying view definition is changed.
Of the two, my first choice would be the second option. It's cleaner and doesn't require distributing a new workbook file each time.

Saving results of a SQL Server stored procedure to an Excel format programatically

I need to save the results of a SQL Server stored procedure into an Excel file.
How can I do this programatically, as part of the same stored procedure - preferably as the last step of the stored procedure?
Please note that I do not have a separate web application. I want to perform this save functionality to Excel, as part of the stored procedure itself.
This is a bit long for a comment.
My first suggestion is to save the data as a delimited file, which can easily be imported into Excel. This should be fine for archival purposes, for instance, or for sending the results around by email.
The next suggestion is to save the results in a table, and to have a data connection to that table. You can then have a query in the Excel file that automatically refreshes the data. This is handy for having special formatting and charts in the Excel file.
Another option is to use openrowset() to directly write to an Excel file. The process would often be something like copying a template file to a new location and then writing the data to it.
The best method really depends on what you want to do with the results.

Export to excel from SQL Server 2000 using Query Analyzer code

What's the easiest way to export data to excel from SQL Server 2000.
I want to do this from commands I can type into query analyzer.
I want the column names to appear in row 1.
In Query Analyzer, go to the Tools -> Options menu. On the Results tab, choose to send your output to a CSV file and select the "Print column headers" option. The CSV will open in Excel and you can then save it as a .XLS/.XLSX
Manual copy and paste is the only way to do exactly what you're asking. Query Analyzer can include the column names when you copy the results, but I think you may have to enable that somewhere in the options first (it's been a while since I used it).
Other alternatives are:
Write your own script or program to convert a result set into a .CSV or .XLS file
Use a DTS package to export to Excel
Use bcp.exe (but it doesn't include column names, so you have to kludge it)
Use a linked server to a blank Excel sheet and INSERT the data
Generally speaking, you cannot export data from MSSQL to a flat file using pure TSQL, because TSQL cannot manipulate anything outside the database (using a linked server is sort of cheating). So you usually need to use some sort of client application anyway, whether it's bcp.exe, dtswiz.exe or your own program.
And as a final comment, MSSQL 2000 is no longer supported (unless your company has an extended maintenance agreement) so you may want to look into upgrading at some point.

SQL SSIS Help. Import an excel sheet into a temp table

I have a farily simple task of taking an Excel sheet and importing it into a SQL 2005 database table. I need to create an SSIS task for this. The Excel sheet does not have all the columns I need to make the insert directly into the permanent sql table, but I know how I could link out to other tables and get the columns that are missing. So I was wondering how I could import the Excel sheet into a #tempTable (or #VariableTable) and then one in a temp table I could just write my SQL Insert code (using the temptable as well as the other tables that I will link on) in a basic Execute SQL Task. But I am having trouble figuring out how to do this with SSIS. When I drag my excel source and try to link it to a SQL Server Destination the drop down doesn't have an option for temptables.
The SSIS way of doing this would be to use a Merge or Lookup transform. I don't think that you can put things into a temp table like that, but you could have an ExecuteSQL task that creates an actual table that you can then drop at the end of the package. You can then have your package use that.
During the design time you might need to have the table in place to link things up, but it shouldn't need to be there when you actually run the package.
First, you'll need to create the staging table for the excel worksheet. Open SSMS, right click the database, choose tasks, import data. Set the import source as excel. Browse to the file. Set the destination as SQL Server. You can accept the table name or name it as you wish. I suggest naming it something useful. Depending on your understanding of data types and what is in the excel sheet, it may take you a while to get this right. Eventually, you will have a table that will accept the contents of the excel sheet.
Second, create your ssis package by using an excel source and sql serve or oledb destination.
take Execute SQL task in control flow to create the target staging table of excel sheet surce.
in data flowuse excel source and oled db target to staging table create in the first step.
3.In control flow use merge or join statement to your excel targeted staging table with other source table to final target table.
thanks
prav