Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have two table in SQL Server 2008, SMS_bill and SMS_submit. Records in SMS_submit is read one by one and inserted into SMS_bill, and then sent to another application program to be processed, and deleted after reading. The code is:
SQL
CREATE PROCEDURE [sp_sms_submit] 
	@id int,
	@result int 
AS
BEGIN
	SET NOCOUNT ON;
	insert into SMS_bill (UserNumber, MessageCoding, MessageContent)
	select UserNumber, MessageCoding, MessageContent 
	from SMS_submit 
	where id = @id
	delete from SMS_submit 
	where id=@id
END
GO


If the record count is not large, for example less than 20, the code runs Ok everytime. But if the record count is large, for example more than 100, there will be about 2% records which were not deleted and left in SMS_submit. Could anyone give me some tips?
Posted
Comments
Sandeep Mewara 3-Mar-13 10:16am    
Do you see any error for the not-deleted rows? Any log about it? What happen when you try to run the query to delete them. Tracked anything?
Ankit_Sharma1987 3-Mar-13 10:17am    
Have you used Primary Key RelationShip?
daiwuju 4-Mar-13 4:39am    
Yes, id is the Primary Key.
RDBurmon 3-Mar-13 10:48am    
Did you setup ROWCOUNT before ran this procedure ?

1 solution

Is it possible for another process to insert one or more rows into SMS_Submit while this stored procedure is running? If so, that would explain why some rows are not deleted. It would also mean that you will have some rows that get deleted before they are inserted into SMS_Bill which means you will lose data.

To ensure that only those rows that have been inserted into SMS_Bill are deleted from SMS_Submit and that if there is a problem the database will remain consistent, you should do the following:
1. Add a column named ToBeTransferred to SMS_Submit table
2. Use this code to do the transfer of rows from SMS_Submit to SMS_Bill
SQL
-- Marks the starting point of a transaction
BEGIN TRANSACTION MOVESMSROWS WITH MARK;
-- Mark rows that will be moved
Update SMS_Submit Set ToBeTransferred=1 Where id=@id;
-- Move the rows that have been marked for movement
Insert into SMS_Bill (UserNumber, MessageCoding, MessageContent)
Select UserNumber, MessageCoding, MessageContent from SMS_Submit 
    Where id = @id AND ToBeTransferred=1;
-- Delete the rows that have been moved
Delete from SMS_Submit where id=@id AND ToBeTransferred=1;
--Commit changes
COMMIT TRANSACTION MOVESMSROWS;


See BEGIN TRANSACTION (Transact-SQL)[^]
 
Share this answer
 
v3
Comments
Maciej Los 3-Mar-13 14:02pm    
This is it!
+5!
daiwuju 4-Mar-13 7:46am    
Thank your very much, Mike Meinz. I have tried your code, and failed. Now, I plan to add some code to check whether the SMS_sumit is empty, that is if not empty, do the procession again.
Mike Meinz 4-Mar-13 8:03am    
How did my code fail? Do I have a syntax error? Which statement?

You did not answer my question. Is it possible for another process to insert one or more rows into SMS_Submit while your stored procedure is running?
daiwuju 4-Mar-13 20:52pm    
First, I am very sorry for the use of word "fail". The records left in the table has been reduced greatly to about 0.4% when the record count is more than 1200 if your code runs. I think your last question must be the answer to my problem - there must an other stored procedure that does something to the table. I will check the whole project now.

Thanks!
Mike Meinz 5-Mar-13 7:49am    
Thanks for your response. Please reply here to let us know the root cause.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900