SSIS Export 1 table to multiple Excel sheets - sql-server

I want to export a large table from SQL Server to excel using SSIS. I have a col called Num that I want all the same values into the same excel sheet e.g. all the 1s in 1 sheet etc. How to do this with SSIS? Do I write a SQL command text?
Num
--
1
1
2
2
2
3
4
4

Export data from SQL Server to Excel and Excel file via using SSIS package :
Configure ADO.NET connection :
To create a “Source” connection, right click “Connection Managers” and select “New ADO.NET Connection”.
a. Server Name – is the hostname or IP address of the database server where SQL is installed.
b. Authentication – is an “authentication type” used to connect Server that is selected on “Server Name” drop-down box. It can be either SQL Authentication or Windows Authentication.
c. Database Name – is the name of the database which we are going to use for data extracting.
Configure ADO.NET Source :
To populate data from SQL Server table, you must configure ADO.Net source component.
Double-click on “Data flow task” to do that. The window “Configure Data flow task” will open. Alternatively, you can select “Data Flow” that is located next to “Control Flow”. As we already configured SQL connection, we will drag and drop ADO.NET Source to data flow window.
Then select “SQL Command”, it will open a multi-line text box where we can write the Ad-Hoc query to extract data from the database.
Configure excel destination
Once we configure ADO.NET source, we will configure Excel destination.
In SSIS tool-box, drag and drop excel destination in the data-flow window and rename it as “Sales Excel File”.
a. Excel connection manager – used to connect the excel file.
b. Data access method. Excel worksheet is considered as a table or view. If you select “Table or View”, drop down box “Name of excel sheet” will be filled with all worksheets that are created in the excel workbook.
c. Name of excel sheet. By default, this drop-down box is disabled. It will be enabled automatically when we select “Table or View” as data access mode. We also can select appropriate excel worksheet.
Then, choose :
a. Excel File Path – the location of the excel file.
b. Excel version – can select excel version from drop down box.
In order to export data from SQL table to excel file, you must map the fields of SQL table and Excel file.
To do that, drag blue arrow beneath the ADO.NET component and drop it on Excel Destination component.
Double click on “Excel destination” to map the fields. “Excel destination editor” will open. Select “Mappings” option from the left pane of the dialog box. In mapping, input columns will be the columns of SQL Query output and destination columns will be the columns of the excel file. It is suggested to keep the same name of SQL Query output and excel columns. This will make mapping much easier because we do not need to map fields manually. SSIS will map columns automatically.

Related

Cannot import long text from Excel to SQL Server using SSIS

Environment:
Microsoft® Excel® for Microsoft 365 MSO (Version 2112 Build 16.0.14729.20254) 64-bit
Microsoft SQL Server 2019 (RTM-CU14) (KB5007182) - 15.0.4188.2 (X64)
Microsoft Visual Studio 2019
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Office\16.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows set to 0
In Excel I formated the cell as "Text" and I also filled up the 2nd and 3rd rows with some super-long dummy text... (1st row is title row)
When I go to Excel Source/Advanced editor, I can set the Output to Unicode text stream [DT_NTEXT] or anything else but the External column cannot be changed to something else than Unicode string [DT_WSTR] (255 characters) despite the setting in registry that normally should allow it and despite having super-long strings in first two rows (other than the 1st row which holds the field names)
Then of course when I try to execute the SSIS task it throws a normal truncation error.
Question: What I am doing wrong or what else should be done here to actually be able to import the data? By the way, this is supposed to be automated at some point.
Since an Excel workbook is not a database, the OLE DB provider tries to detect the most relevant metadata from the Excel worksheet and read it as tabular data, which is mostly inaccurate when handling medium and large Excel files. After spending years on creating SSIS packages, I will convert the Excel file to a CSV file and import it using a Flat File Connection Manager instead. Or I will use a C# script to import the data.
I. Converting Excel to CSV
You can automate the process of converting Excel to CSV using a C# script:
Converting XLSX file using to a CSV file
Convert .xlsx & .xls to .csv
How to Convert Excel to CSV using Interop
After converting the Excel file to a CSV file, you can dynamically import it using a Flat File Connection Manager:
Dynamic Flat File Connections in SQL Server Integration Services
II. Using a C# script
It is good to check the following class, which is a part of the SchemaMapper project:
SchemaMapper - MsExcelImport.cs
Besides, a step-by-step guide on how to use this library can be found in the following link:
Import data from multiple files into one SQL table step by step guide
III. Editing the Excel connection string
If you don't have the choice to convert Excel to flat files, then you can force the Excel connection manager to ignore headers from the first row by adding IMEX=1 to tell the OLEDB provider to specify data types from the first row (which is the header - all string most of the time).
To edit the connectionstring property, click on the Excel Connection Manager and press on the F4 key. In the Properties Tab, you can edit the connectionstring property.
SSIS Excel Import Columns with More or Less than 255 Characters
IV. Changing columns length from advanced editor
Try changing the Excel Source column metadata from the advanced editor:
In SSIS excel datasource not taking more characters than 255
Importing Excel using SSIS may cause a headache! You can check the following question:
Workaround for exporting data to Excel with more than 255 columns
Dynamically Creating Excel table through SSIS
SQL Server Import Wizard doesn't support importing from excel sheet with more than 255 columns
Importing Excel Data Seems to Randomly Give Null Values
Failing to read String value from an excel column
Importing Excel Data Seems to Randomly Give Null Values
SSIS - Excel data shows as scientific notations and Null Values

How reset existing sql server connection in excel workbook

I have created an Excel workbook with SQL server connection and loaded data using SQL commands. I want to use the same excel file in a different machine, there I want to change the database credentials. In our excel file, there are more than 10 SQL server connections and sheets, So recreate the file is very difficult. Is there any way to reuse the file with a different database?
I have copied the excel file with all .odc files, But I couldn't find an option to reset the connection to the new database.
I see two possibilities:
Edit the ODC files manually with a text editor
In Excel, use "Data" -> "Connections" to edit the connections

Create and write in text file with SSIS

I am totally new to the SSIS. I have a requirement to create a text file on specified location and write the data in it which is selected from SQL table.
How to do that?
Is there any example which will help me?
The below steps should help you to get a basic SSIS package working:
Create new Integration Services Project in Business Intelligence Development Studio. There should be a default SSIS package in it called Package.dtsx. Open the package.
In the bottom of the screen, there should be an area for connection managers. Right click on it to get a list of different connection managers.
Click on New OLEDB Connection. This will be the connection manager for your source database. Set the data source (server or instance name), authentication mode and credentials and the initial catalog (database name).
Again right click in the connection managers area and select New Flat File Connection.This will be the connection manager for the destination flat file. You can create a text file (.txt or .csv) and add in the headers for your data e.g. EmployeeID,EmployeeName,EmployeeSalary. Check the "column names in the first data row" checkbox to make sure your headers are identified as such.
Now select a Data Flow Task and drag it to the Control Flow screen.
Open the Data Flow tab. Drag 2 items from Toolbox to the screen - OLEDB Source and Flat File Destination.
In the Source, use the OLEDB connection manager created earlier. Then change the Data Access mode to SQL command. Now you can type your own command to select data. Make sure that the header data in your flat file is the same as the column headers from your select query.
In the Destination, use the Flat File connection manager created earlier. Go to the Mappings section and map the columns from your query output to the headers in your text file.
Now save and run the package. You should see that the destination text file is populated with data from your query.

Import multiple Excel files into SQL Server 2008 R2 using SSIS Packages?

I have to insert data into two tables from two different excel sheets using SSIS packges.
So, please some one tell me How to import multiple Excel files into SQL Server 2008 R2 using SSIS Packages?
since you have only 2 excel files and only two tables I don't see why use a foreach loop. Just follow this link and do it directly
If you need to insert from both excel files to both databases you can use a multicast component to create a copy of a dataset
Use Foreach Loop Container --> Foreach File Enumerator. Then put your Data Flow in Foreach Loop Container.
Table structure and sheet name have to be equal in every Excel file.
Here is nice tutorial:
http://bi-polar23.blogspot.com/2007/08/loading-multiple-excel-files-with-ssis.html
You can create and use variables to hold Excel file path, then implement the package within a for-each-loop container to loop trough each excel files in the folder. Make sure that the files are having similar data format and same extension.
Create an integration service project in Visual Studio and follow the steps below:
Right Click on canvas and add Variable say "FilePath" and set type
as String
Drag a for-each loop to canvas
Right click on for-each-loop and select "Edit" and add path and file extension and then select the variable from "Variable Mapping" tab as shown:
Add a Data-flow task in to the loop and double click to open it.
Add an Excel source and OLEDB destination (if destination is SQL Server). By right clicking on these controls and choosing "Edit", you can select appropriate folder for Excel and its Data Sheet as well as specify SQL server database credentials in the destination. Connect source and destination using blue arrow. Check and modify mappings between columns, if needed.
Create an expression to read each file to the variable. This can be done by selecting created "ExcelConnectionManager" and select its "Expression" property and expand to set variables to "ExcelFilePath"
And that is it. You are ready to execute the package by coming to Control flow tab and select "Start".
Ref: https://www.encorebusiness.com/blog/import-data-from-multiple-excel-files-sql-ssis/

How to change destination table name in SQL Server Import and Export Wizard

I use SQL Server Import and Export Wizard to transfer data from a remote server to my local server.
First step, Choose a Data Source.
Second step, Choose a Destination.
Third step, Specify Table Copy or Query, there are 2 options
I choose Write a query to specify the data to transfer because I need to organize the data on remote server first and then transfer the result.
Then I specify the SQL statement I want to run on remote server.
I found that the destination table name on my local server is forced to be 'Query'.
How can I change this from Query to something I specify?
On the screen after you select the query you get the 'Select Source Tables and Views' screen. On this screen you can select which table the data is written to in the destination column. This drop down will let you select any table in the destination database. So you will need to create your destination table in the destination database before using the wizard.
You can also edit column mappings and what happens to existing data by using the 'Edit Mappings...' button.
You are basically creating an ssis package via a wizard. Create the package manually, create a table destination variable, figure out a way to populate the table destination variable, and choose "Table or view name from variable" (choosing that variable) for the destination.

Resources