Creating blank/deleting rows form excel using SSIS
0
votes
0
answers
2129
views
I have an .xlsx file I import data from nightly.
Once the import is complete I delete the tables(sheets/tabs) in the excel file using an Execute SQL Task so that the file is blank for the user but contains correct header rows:
DROP TABLE
sheet1$
GO
DROP TABLE sheet2$
GO
I then re-create the tables (sheets/tabs) using another Execute SQL Task on completion of the last:
CREATE TABLE sheet1$
(
Column 1
LongText,
Column 2
LongText
)
GO
CREATE TABLE sheet2$
(
Column 1
LongText,
Column 2
LongText,
Column 3
LongText,
Column 4
LongText,
Column 5
LongText,
Column 6
LongText
)
GO
The problem I have is that say 10 rows existed on day 1. 10 records are imported. The tables(sheets/tabs) are deleted. The tables(sheets/tabs) are re-created but for some reason the number of rows are still part of the sheet. So for instance if on day 2 no new records where added to the file then the import imports 10 blank rows. It's like they are being cleared but excel is holding some sort of reference to data rows.
If the file has not been added to for a few days then it just continues to import 10 blank rows each night filling up the table with nonsense rows.
Obviously I can't use the following otherwise I wouldn't have to drop and re-create.
DELETE * FROM sheet1$
What am I missing/doing wrong?
There are many workarounds I can think off such as:
An sql script that deletes NULL rows from the SQL table where they are imported to but this messes up the auto increment ID field which I need to be the same as the record count.
So I could:
Import to an interim temp table and only import rows that contain data to the final table.
OR
After import copy data to another table, delete NULL rows, truncate original table and import back (retains ID increment)
...etc etc
I am sure the workarounds are not needed and complicating the solution.
Thanks
Asked by Round
(123 rep)
May 11, 2020, 09:16 AM