Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am developing an upload mechanism, where user will upload excel file into sql table using a button click.
The excel file has columns Ref #, EndDate,StepName and StepResponse.
And my SQL table has columns Ref#, Login_Date, Decision_Date StepName and StepResponse.
The excel file will be uploaded daily in the database.
The Column StepName in excel file can have records like BureauPull or Level2.
Ref# is unique column in excel file. Excel file is being downloaded from another system and it is downloaded and then uploaded into SQL table on daily basis.
Now lets assume, i have a Case with a Ref#='abc123' and StepName = 'BureauPull' and whenever StepName = BureauPull then the EndDate will be mapped with Login_Date in SQLTable and whenever the StepName = Level2 the EndDate will be mapped with Decision_Date.
Now lets say when i download this excel file on the second day and the StepName of same case i.e. Ref#='abc123' changes from 'BureauPull' to 'Level2' so when i upload this file in SQL table then i need to update the previous uploaded case aswell. Now the EndDate will be mapped to Decision_Date.
I hope i have explained it well, if not please don't hesitate to ask and i will try to clear any confusion.
Looking forward to your usual support.


Thanks

What I have tried:

Have searched through internet but unable to find any relevant solution
Posted
Updated 13-Jun-17 3:13am

1 solution

Upload your excel file to a stagetable with the same columns as the Excel file.
Use MERGE[^] to insert or update to the final table.
The next day you truncate the stagetable before inserting the next Excel file.
 
Share this answer
 
Comments
Faran Saleem 19-Jun-17 3:32am    
Thanks for your reply.
I am trying to do that, i have constructed the following query

MERGE od_test2 T --tareget
USING OD_TEST S --source
ON (S.ref = T.REF)
WHEN MATCHED
THEN UPDATE
SET T.LOGIN_DATE =
(CASE
WHEN s.stepname IN ('BUREAUPULL') THEN CONVERT(VARCHAR,s.[ENDDATE])
ELSE CONVERT(VARCHAR,s.[LOGIN_DATE])
END S.LOGIN_DATE),
(CASE
WHEN s.stepname IN ('l2','l3') THEN CONVERT(VARCHAR,s.[ENDDATE])
ELSE CONVERT(VARCHAR,s.[DECISION_DATE])
END S.DECISION_DATE)

WHEN NOT MATCHED BY TARGET
THEN INSERT (Target_Column_Names)
VALUES (Source_Column_Names)

WHEN NOT MATCHED BY SOURCE
THEN DELETE;


But the above query bis giving me syntax error. Can you help me resolve it?
i am trying to use case with UPDATE. When the stepname is BureauPull the EndDate should be inserted in Login_Date and when stepname is L2 or L3, EndDate should get inserted in Decision_Date
Jörgen Andersson 19-Jun-17 4:07am    
You only set one target column in the Update Clause. You probably need something like this:
MERGE od_test2 T --target
USING OD_TEST S --source
ON S.ref = T.REF
WHEN MATCHED THEN UPDATE
SET T.LOGIN_DATE = (
CASE
WHEN s.stepname IN ('BUREAUPULL') THEN CONVERT(VARCHAR,s.[ENDDATE])
ELSE CONVERT(VARCHAR,s.[LOGIN_DATE])
END S.LOGIN_DATE
)
,T.DECISION_DATE = (
CASE
WHEN s.stepname IN ('l2','l3') THEN CONVERT(VARCHAR,s.[ENDDATE])
ELSE CONVERT(VARCHAR,s.[DECISION_DATE])
END S.DECISION_DATE
)
WHEN NOT MATCHED BY TARGET
THEN INSERT (Target_Column_Names)
VALUES (Source_Column_Names)

WHEN NOT MATCHED BY SOURCE
THEN DELETE;
Faran Saleem 19-Jun-17 4:55am    
I have tried this but it is giving me syntax error near CASE ending bracket

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900