Referencing a dynamically created table in SSIS "source assistance" - sql

I have an SSIS project that does the following:
Executes a sql task (in the control flow) that creates (and populates with data) 4 tables. After that executes I have a Data Flow task that references those 4 tables, joins them, then uses the destination assistant to create a new table called "Step". Then, those 4 tables are deleted leaving only the "Step" table.
However, when I try to run it it throws an error because technically I am referencing 4 tables that do not yet exist. How do I work around this?

I would do this the same way you have to handle temp tables in a data flow source.
Use a stored procedure for your datasource, and start it with something like:
IF 1=0
SELECT
{dummy-valued column list that matches the column names and datatypes of your expected output}
ELSE
{your real stored procedure code}

Related

Moving multiple table data from one database to another database by stored procedure using SSIS packages

I have a stored procedure which gets data from table trans of database "A" and inserts into multiple tables (trans, trans_log, trans_history) in database "B" and then this procedure just deletes that data from the trans table in database "A". This is just an archive process. The procedure RETURNs 0 if successful.
Previously it was scheduled as a SQL job, but now I need to create an SSIS package for it. How to create SSIS package for this? I know that I need to create source and destination connection in a dataflow, but I am stuck at mapping columns, my procedure does not any return columns it just inserts into multiple table of another database.
How do I define source and destination or how do I perform this in SSIS package?

Update another table when SSIS data flow finishes

I have a simple package where I pull down every table from a remote source DB into my local server DB.
Every data flow task is simple source to destination.
The problem I have is occasionally the download will stop and I won't get all the tables, or sometimes tables won't fully pull down all data.
All I want to do is have a table with all table names that I need to pull down.
After each table in my data flow completes I need to update a flag in my new table of table names so that there is a 1 for every table that fully downloads from the source to the destination. Any help would be appreciated.
A simple approach is to add an Execute SQL Task after each Data Flow Task that updates the table containing a LastOperationDate which will have the last execution time.
UPDATE AuditTable
SET LastExecutionDate=GETDATE()
WHERE TableName LIKE #param
The #param is the name of the table in the data destination of the concerned data flow.

Is there a way to use dynamic SQL in a view?

I have 5 tables and view pairs.
Example:
Table dynamics.Customer (100 columns)
View staging.CustomerView (20 columns)
The CustomerView is built from staging.Customer table and other staging tables, not dynamics.Customer.
I want to compare all rows with all fields that exists in staging.CustomerView to dynamics.Customer to see if changes has occurred to staging. I want this to show as a column in my staging.CustomerView.
Since I have 5 tables and view pairs I tried to create a function with dynamic SQL that got all rows from staging view and compare it to dynamics table. Then I called that function from each view. I got an error executing the function, as it used sp_executesql.
Is there a way to create a function that dynamically gets each column and compares it? The view is used in Azure Data Factory to Copy data into Dynamics365 and because of performance, I only want to insert/update the rows that has changed.
Views are strongly typed, and cannot be created dynamically.
you can write trigger which records changes to another log table on updates and query that table.

SQL Temporary Table Title different from original

I made a stored procedure which I want to store the results of it in a "temporary table"
I named the table
#SalesDiff
HOWEVER,
the title of table under temporary tables folder is shown as
#SalesDiff___________________________________________________________00000000023F
Below is basic query format
SELECT * INTO #SalesDiff FROM (
SELECT A, B, C
FROM
(.... ) D
Temporary tables only exist for the lifespan of the connection that creates them. When the SQL engine creates your table for you, it appends information to the end to distinguish your version of #SalesDiff from the version generated by the other three users running the same stored procedure.
Within your connection, the engine keeps track of the version information automagically, so you don't need to worry about the extension.

How Temp Table works in Stored Procedures (global & local )

Image #1: I am creating a stored procedure for inserting records into #t1.
In the same session I am executing the Localtemp1 stored procedure for any number of times, and it works fine:
Image #2: again executing the stored procedure in another session & works fine as well:
Image #3: now creating stored procedure for inserting records into ##tt. For the first execution of globaltemp1 stored procedure, it works well:
Image #4: but when I executed it a second time, it is showing errors (does not exist in DB):
Image #5: then I closed the session where globaltemp stored procedure was created, and in a new session, I executed the stored procedure, and it works well for the first time:
Image #6: but when I execute it a second time, again it is showing errors (does not exist in DB):
What I know is scope of local temp & global temp, but in stored procedures, they were completely different
Can someone tell me
Execution of localtemp1 stored procedure for many times gives output but while executing globaltemp1 sp for the first time gives output and second time results in an error
As far as I know, after execution of stored procedure temptable gets dropped. Then why localtemp1 stored procedure is getting executed across all sessions and many number of times?
Why globaltemp1 stored procedure is executing once and for second time showing an error?
Final one, Globaltemp stored procedure shows output in another session for the first time only when created session was closed
I mean
56 ----> globaltemp sp was created
57 ----> to get o/p i need to close 56
58 ----> to get o?p i need to close 57 ( WHY ??? )
I am a beginner at SQL and please, someone make me understand because if I don't find logic & correct reason I could not dive into another topic.
The concept of temp table is to hold records temporarily. It's some kind of an array where you can store multiple records using the same variable.
When you create a Temp Table, actually it is being created in the tempdb of the corresponding server. Even if you are naming it as just #temp, the name on which it was created on the tempdb will be having some additional parameters like your database name from which the table was created and your session id etc.
I just created the following temp table in my master database
and this is how it was named in the tempdb
still, in my database, I can access it using the name #temp. But The limitation of such temp table is that they are local and can be accessed only from that session, So if I try to access this #temp from any other Query Window (Session) even on the same database, I won't be able to access it. That's where we use Global temp tables. So If I add one more # to the table name then it becomes global temp table which can be accessed across the sessions. It is still created on the Tempdb but like this
Whenever you close the query window/session both Local and Global temp tables are automatically dropped.
So in the case of stored procedures, the starting and ending time of the sp is treated as one session. So once the sp execution is completed all the temp tables created inside that sp is dropped. So you can not use one temp table that was created by one SP in another one.
Hope this helps
Local temporary tables are visible only in the current session, and global temporary tables are visible to all sessions. Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them. The association between a task and a table is maintained only for the life of a single Transact-SQL statement. This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.
If you have absolutely have to have a global temp table available your best solution would be to create a permanent table and then drop the table at the end of the stored procedure. You can check for it's existence before creating it:
IF OBJECT_ID('dbo.yourtablenamehere', 'U') IS NOT NULL
DROP TABLE dbo.yourtablenamehere;
The differences between a temp table and a permanent table are really not that much different, mostly that the temp table drops automatically. If you are using this in an application that is calling this procedure, it might be best to have the application load the temp table into array and do the comparisons for you since it can maintain the array while executing and re-executing stored procs.
There is a good reason to write to a temp table instead of creating a table then dropping it... access rights. If you are creating tables to be used by people who are only granted read only privileges, they will not be able to create the table.