SQL Server deleting old records

Can anyone help.
I have an old file getting larger and larger and I want to delete records older that 7 days old from it. However I have to ling to another table to determine the data of the record.

I also want to insert into a copy ofr the table just in cae I still need to use the data sometime in the future

It goes something like this

declare @cnt int

select @cnt = COUNT(*) from source s (nolock)
  inner join EXCEPTION lcdeh (nolock)
    on s.source_key = lcdeh.source_key
where s.process_start_date_time < convert(varchar(10),(getdate() - 7),103)

if @cnt > 0 then
  I want to insert into an existing copy of the table    COPY_EXCEPTION_HEADER
  and then delete the entries from the original (EXCEPTION_HEADER)

COPY_EXCEPTION_HEADER already exists and is identical in layout to EXCEPTION_HEADER

END

Ideally I don't want to have to specify all the columns i'm inserting.

any help would be appreciated.
Eric HarrisDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
begin tran
insert COPY_EXCEPTION_HEADER
select lcdeh.*
from source s (nolock)
inner join EXCEPTION lcdeh (nolock) on s.source_key = lcdeh.source_key
where s.process_start_date_time < convert(varchar(10),(getdate() - 7),103)

delete EXCEPTION
from source s (nolock)
inner join EXCEPTION lcdeh (nolock) on s.source_key = lcdeh.source_key
where s.process_start_date_time < convert(varchar(10),(getdate() - 7),103)
commit
0
cyberkiwiCommented:
I notice that your SQL is not entirely correct (it contains EXCEPTION table instead of _HEADER) but you can see where I'm going.

If your COPY_EXCEPTION_HEADER has an identity column, then change the insert part to

set identity_insert COPY_EXCEPTION_HEADER on;
insert COPY_EXCEPTION_HEADER
select lcdeh.*
from source s (nolock)
inner join EXCEPTION lcdeh (nolock) on s.source_key = lcdeh.source_key
where s.process_start_date_time < convert(varchar(10),(getdate() - 7),103);
set identity_insert COPY_EXCEPTION_HEADER off;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eric HarrisDeveloperAuthor Commented:
Thanks for getting back so Quickly.
I'll try that tomorrow as I don't have access to my SQL Server at the moment.
0
Eric HarrisDeveloperAuthor Commented:
Again apologies for not responding earlier.
Your solution worked brilliant.
Thanks for all your help
0
Eric HarrisDeveloperAuthor Commented:
Fast and accurate response. THanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.