Hi, I have an application using Entity Framework. The database was created with cascadeDelete set to true. This is because every 6 months data from the database needs to be purged.
Every couple of months I have to import a spreadsheet (importeddata), run a statement to copy the spreadsheet to a temp table:
insert into ImportTempTable (Column1,Column2 Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12)
select F1, CAST(F2 AS BIGINT), F3, F4, F5, F6, F7, F8, F9, CAST(F10 AS BIGINT), F11, 1
from importeddata
where F3 LIKE '_____-___'
I then run the following statements to essentially bring in data from the ImportTempTable into TheTable but remove any duplicates. What I've been noticing is that even though these statements are executed against one main table data is going missing from other tables. For example, TheTable has a FK in TableA. Checking the row count before and after the statements are executed shows a decrease in row count after. I believe this is because the cascadeDelete set on the tables. Is there a way to "ignore" the cascadeDelete when running these statements?
--INSERT new items
INSERT INTO TheTable (Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10, Column11, Column12)
select Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10, Column11, 1
from ImportTempTable as src
where not exists (
select 1 from TheTable as dst
where isnull(src.Column2, '') = isnull(dst.Column2, '')
and isnull(src.Column3, '') = isnull(dst.Column3, '')
and isnull(src.Column4, '') = isnull(dst.Column4, '')
and isnull(src.Column5, '') = isnull(dst.Column5, '')
)
--DELETE removed items
DELETE src
from TheTable as src
where not exists (
select 1 from ImportTempTable as dst
where isnull(src.Column2, '') = isnull(dst.Column2, '')
and isnull(src.Column3, '') = isnull(dst.Column3, '')
and isnull(src.Column4, '') = isnull(dst.Column4, '')
and isnull(src.Column5, '') = isnull(dst.Column5, '')
)
--UPDATE items
update dst
set Column1 = src.Column1,
Column6 = src.Column6,
Column7 = src.Column7,
Column8 = src.Column8,
Column9 = src.Column9,
Column10 = src.Column10,
Column11 = src.Column11
from TheTable as dst
join ImportTempTable as src
on isnull(src.Column2, '') = isnull(dst.Column2, '')
and isnull(src.Column3, '') = isnull(dst.Column3, '')
and isnull(src.Column4, '') = isnull(dst.Column4, '')
and isnull(src.Column5, '') = isnull(dst.Column5, '')
- Moved by Lydia ZhangMicrosoft contingent staff, Moderator Wednesday, June 24, 2015 7:51 AM more appropriate