Okay I am newbie to SSIS and so I am trying to figure out what is going wrong over here.
Here is the situation. I have to develop a SSIS package to copy some tables from one DB to another in its entirety. That means the first time I run the package I need to check if the table exists and if it does not, create the table and and copy the data.Thereafter I have to copy the data everyday based on the delta for that day. One of the columns in the table is the date field and it records the date and time when the row was written. So what I want to do is select the max date in the destination table and the max date in the source table and copy the data based on the date difference.
So I have created the package that works every time on the first execution and copies all the data.The package contains 2 Execute SQL tasks followed by a Data Flow Task. (I will post the picture once the forum verifies my account and lets me post the picture)
IF EXISTS (SELECT * FROM information_schema.tables WHERE table_name = 'dbo.t_Agent')
SELECT MAX(DbDateTime) from t_Agent
END
ELSE
BEGIN
CREATE TABLE [t_Agent] (
[DbDateTime] datetime,
[TargetID] int,
[Duration] int,
[Code] int
)
SELECT MAX(DbDateTime) from t_Agent
END
So my assumption is that the first time the package is executed it will see that there is no table and so it creates the table and copies all the data from the source to the destination. And this works fine.
However if I execute/run the package the second time, that is when the package fails with the following error message
[Execute SQL Task] Error: Executing the query "IF EXISTS(SELECT * FROM information_schema.tables ..." failed with the following error: "There is already an object named 't_Agent' in the database.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I understand that after the first time I run the package the table is already created in the destination and the data is copied there. But if I am not mistaken the IF EXISTS line should be true and it should execute the SELECT MAX(DbDateTime) from t_Agent line and exit.
Is my understanding correct? What am I doing wrong?
I really appreciate any help.
Thanks.