I have a control flow where I create a temp database and table with a T-SQL Command. When I add a dataflow I would like to query the table but I can’t because the table doesn’t exist to grab information from. When I try I get errors about logging in because the database doesn’t exist (yet). I have set the connection manager’s property DelayValidation to True.
If I create the database and table manually and then add the dataflow with query and drop the database, it sticks but it doesn’t seem like a clean solution.
If there is a better way to create a temporary staging database and query it in dataflows please let me know.
Solution:
Set the property
RetainSameConnectionon theConnection ManagertoTrueso that temporary table created in one Control Flow task can be retained in another task.Here is a sample SSIS package written in
SSIS 2008 R2that illustrates using temporary tables.Walkthrough:
Create a stored procedure that will create a temporary table named
##tmpStateProvinceand populate with few records. The sample SSIS package will first call the stored procedure and then will fetch the temporary table data to populate the records into another database table. The sample package will use the database namedSoraUse the below create stored procedure script.Create a table named
dbo.StateProvincethat will be used as the destination table to populate the records from temporary table. Use the below create table script to create the destination table.Create an SSIS package using
Business Intelligence Development Studio (BIDS). Right-click on the Connection Managers tab at the bottom of the package and clickNew OLE DB Connection...to create a new connection to access SQL Server 2008 R2 database.Click
New...on Configure OLE DB Connection Manager.Perform the following actions on the Connection Manager dialog.
Native OLE DB\SQL Server Native Client 10.0from Provider since the package will connect to SQL Server 2008 R2 databaseMACHINENAME\INSTANCEUse Windows Authenticationfrom Log on to the server section or whichever you prefer.Select or enter a database name, the sample uses the database nameSora.Test ConnectionOKon the Test connection succeeded message.OKon Connection ManagerThe newly created data connection will appear on Configure OLE DB Connection Manager. Click
OK.OLE DB connection manager
KIWI\SQLSERVER2008R2.Sorawill appear under the Connection Manager tab at the bottom of the package. Right-click the connection manager and clickPropertiesSet the property
RetainSameConnectionon the connectionKIWI\SQLSERVER2008R2.Sorato the valueTrue.Right-click anywhere inside the package and then click
Variablesto view the variables pane. Create the following variables.A new variable named
PopulateTempTableof data typeStringin the package scopeSO_5631010and set the variable with the valueEXEC dbo.PopulateTempTable.A new variable named
FetchTempDataof data typeStringin the package scopeSO_5631010and set the variable with the valueSELECT CountryCode, StateCode, Name FROM ##tmpStateProvinceDrag and drop an
Execute SQL Taskon to the Control Flow tab. Double-click the Execute SQL Task to view the Execute SQL Task Editor.On the
Generalpage of the Execute SQL Task Editor, perform the following actions.Create and populate temp tableOLE DBKIWI\SQLSERVER2008R2.SoraVariablefrom SQLSourceTypeUser::PopulateTempTablefrom SourceVariableOKDrag and drop a
Data Flow Taskonto the Control Flow tab. Rename the Data Flow Task asTransfer temp data to database table. Connect the green arrow from the Execute SQL Task to the Data Flow Task.Double-click the
Data Flow Taskto switch to Data Flow tab. Drag and drop anOLE DB Sourceonto the Data Flow tab. Double-click OLE DB Source to view the OLE DB Source Editor.On the
Connection Managerpage of the OLE DB Source Editor, perform the following actions.KIWI\SQLSERVER2008R2.Sorafrom OLE DB Connection ManagerSQL command from variablefrom Data access modeUser::FetchTempDatafrom Variable nameColumnspageClicking
Columnspage on OLE DB Source Editor will display the following error because the table##tmpStateProvincespecified in the source command variable does not exist and SSIS is unable to read the column definition.To fix the error, execute the statement
EXEC dbo.PopulateTempTableusing SQL Server Management Studio (SSMS) on the databaseSoraso that the stored procedure will create the temporary table. After executing the stored procedure, clickColumnspage on OLE DB Source Editor, you will see the column information. ClickOK.Drag and drop
OLE DB Destinationonto the Data Flow tab. Connect the green arrow from OLE DB Source to OLE DB Destination. Double-clickOLE DB Destinationto open OLE DB Destination Editor.On the
Connection Managerpage of the OLE DB Destination Editor, perform the following actions.KIWI\SQLSERVER2008R2.Sorafrom OLE DB Connection ManagerTable or view - fast loadfrom Data access mode[dbo].[StateProvince]from Name of the table or the viewMappingspageClick
Mappingspage on the OLE DB Destination Editor would automatically map the columns if the input and output column names are same. ClickOK. ColumnStateProvinceIDdoes not have a matching input column and it is defined as anIDENTITYcolumn in database. Hence, no mapping is required.Data Flow tab should look something like this after configuring all the components.
Click the
OLE DB Sourceon Data Flow tab and press F4 to viewProperties. Set the propertyValidateExternalMetadatato False so that SSIS would not try to check for the existence of the temporary table during validation phase of the package execution.Execute the query
select * from dbo.StateProvincein the SQL Server Management Studio (SSMS) to find the number of rows in the table. It should be empty before executing the package.Execute the package. Control Flow shows successful execution.
In Data Flow tab, you will notice that the package successfully processed 6 rows. The stored procedure created early in this posted inserted 6 rows into the temporary table.
Execute the query
select * from dbo.StateProvincein the SQL Server Management Studio (SSMS) to find the 6 rows successfully inserted into the table. The data should match with rows founds in the stored procedure.The above example illustrated how to create and use temporary table within a package.