Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test stored procedure without affecting database

I need to run a test on stored procedure in a client's database. Is there anyway to test the stored procedure without affecting the data in the database?

For example, there is an insert query in the SP, which will change the data of the database.

Is there anyway to solve this problem?

like image 845
user1277680 Avatar asked Mar 19 '12 02:03

user1277680


1 Answers

You could run the stored procedure in a transaction. Use this script by placing your statements between the comment lines. Run the whole script, your transaction will be in an uncommitted state. Then, highlight the line ROLLBACK or COMMIT and execute either accordingly to finish.

Always have backups.

If possible work in a sandbox away from your clients data as a matter of principle.

Be aware that you could be locking data which could be holding up other sql statements by your client while you are deciding whether to commit or rollback.

BEGIN TRANSACTION MyTransaction
GO

-- INSERT SQL BELOW


-- INSERT SQL ABOVE

GO
IF @@ERROR != 0
BEGIN
        PRINT '--------- ERROR - ROLLED BACK ---------'
        ROLLBACK TRANSACTION MyTransaction
END
ELSE
BEGIN
        PRINT '--------- SCRIPT EXECUTE VALID ---------'
        PRINT '--------- COMPLETE WITH ROLLBACK OR COMMIT NOW! ---------'
        --ROLLBACK TRANSACTION MyTransaction
        --COMMIT TRANSACTION MyTransaction
END
like image 191
Valamas Avatar answered Oct 02 '22 14:10

Valamas