Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to test a stored procedure?

Tags:

sql-server

Like many companies that require all access be through stored procedures, we seem to have a lot of business logic locked away in sprocs. These things are just plain hard to test, and some of them have become silly long. Does anyone out there have a set of best practices that can make it a little easier to confidently test these things?

At present we maintain 30 or so "Problem" databases that we run against. This isn't always particularly well documented and it sure isn't automated.

like image 890
Dan Blair Avatar asked Sep 29 '08 02:09

Dan Blair


People also ask

How do I know if a SQL stored procedure is successful?

Return Value in SQL Server Stored Procedure In default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.

How do I test a procedure in SQL Server?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure.


2 Answers

A colleague swears by the TSQLUnit testing framework. May be worth a look for your needs.

like image 68
Ben Hoffstein Avatar answered Sep 19 '22 01:09

Ben Hoffstein


We had a very thin Data Access layer which basically facaded stored procedures to look like C# methods. Our NUnit test-suite then had SetUp/TearDown to create/rollback a transaction and test methods that called into DAL. Nothing fancy, and proved to be easier to maintain than TSQLUnit test-suite.

like image 22
Constantin Avatar answered Sep 20 '22 01:09

Constantin