I am writing a purge routine for a database that I have. I select all records that have aged to a certain threshold into a temp table, now I want to remove some of these items, then in the end after everything is sorted remove the remaining records from the original table. Each record has a unique guid.
So to delete at the end I think I just need to do:
DELETE * FROM orig_table where orig_table.guid IN temp_table
My Question is after I select the records into the temp table I need a count foreach record on field CIFPan, in C# I would do something like this:
foreach ( CIFPan var in temp_table ) {
@count = select count(*) from orginal_table where CIFPan = var
if @count > 1 DELETE * FROM orig_table WHERE CIFPan = var
}
Obviously just sudo code but can I do something like that in the SQL server stored procedure? I was thinking maybe Some sort of UNION/JOIN/SELECT combination?
Thanks for the Help.