Ah, the table modification is really not possible at this stage due to multiple teams using it and multiple levels of approval required. I need a quick-fix.
Main Topics
Browse All TopicsHi,
We have a stored-procedure on SQL Express 2005 which uses BCP to back-up the data from a table into a CSV file, and then deletes the data from the table. This is done in 2 lines:
EXEC xp_cmdshell 'BCP command to back up'
DELETE FROM TABLE_NAME
The issue is that if there is a single insert done to the table when the execution of the first statement has just finished but the second statement has not yet executed, then we might miss backing up that record in the file, and it would get deleted.
To resolve this in the most simple and effort-less manner, is there a simple lock table command which allows me to lock the table before the execution of the first statement, and then I can release the lock after the second statement finishes execution?
Thanks,
Mayank.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Then you have to use the table hint TABLOCKX like
SELECT * FROM TABLE (TABLOCKX)
DELETE FROM TABLE (TABLOCKX)
But make sure that both are within the same transaction as this will be applied to the same transaction.
If you lock the table using first statement in another transaction, then you wont be able to do the DELETE from second statement.
Hope this helps.
Award points to http:#a24623863
Business Accounts
Answer for Membership
by: rrjegan17Posted on 2009-06-14 at 07:42:15ID: 24623628
This issue needs to be solved using other Logics and Not Lock Logic.
1. Add another column in that table named Status.
2. Before executing the First command, Update all records with Status as 'Y' to indicate that those are the records to be backed up.
3. Backup Records with Status as 'Y'.
4. Delete records with Status as 'Y'.
Since new records inserted would be having Null Values, you wont be deleting out that record, and hope this solves your requirement.