What's your favored method for debugging MS SQL stored procedures?

↘锁芯ラ 提交于 2019-12-31 22:42:07

问题


Most of my SPs can simply be executed (and tested) with data entered manually. This works well and using simple PRINT statements allows me to "debug".

There are however cases where more than one stored procedure is involved and finding valid data to input is tedious. It's easier to just trigger things from within my web app.

I have a little experience with the profiler but I haven't found a way to explore what's going on line by line in my stored procedures.

What are your methods?

Thank you, as always.

Note: I'm assuming use of SQL Server 2005+


回答1:


Profiler is very handy, just add SP:StmtStarting events, and filter the activity down to just your process by setting SPID=xxx. Once you have it set up, it's a breeze to see what's going on.




回答2:


You can actually attach a debugger to your sql server :) - from vs, given you have configured that on your sql server.

Check this link for more info, notice you can set break points :) https://web.archive.org/web/20090303135325/http://dbazine.com/sql/sql-articles/cook1.

Check this link for a more general set of info: http://msdn.microsoft.com/en-us/library/zefbf0t6.aspx

Update: Regarding "There are however cases where more than one stored procedure is involved and finding valid data to input is tedious. It's easier to just trigger things from within my web app."

I suggest you set up integration tests, focused on the specific methods that interact with those procedures. If the procedures are being driven by the web app, it is a great place to have valid tests + inputs you can run at any time. If there are multiple apps that interact with the procedures, I would look at unit testing the procedures instead.




回答3:


I prefer to just use stored procs for dataset retrieval, and do any complex "work" on the application side. Because you are correct, trying to "debug" what's happening inside the guts of a many layered, cursor-looping, temp-table using, nested stored proc is very difficult.

That said, MS KB 316549 describes how to use visual studio to debug stored procs.

According to this article, there are a number of limitations to debugging in this fashion:

  • You cannot "break" execution.
  • You cannot "edit and continue."
  • You cannot change the order of statement execution.
  • Although you can change the value of variables, your changes may not take effect because the variable values are cached.
  • Output from the SQL PRINT statement is not displayed.

Edit: Obviously, if you are the person making this stored proc, then don't make it "many layered, cursor-looping, temp-table using, and nested". In my role as a DBA, though, that's pretty much what I encounter daily from the app developers.




回答4:


I prefer not to debug, I do test driven development instead, which almost eliminates the need to debug.




回答5:


As far as not knowing what the valid inputs would be, you need to test a wide range of inputs including especially invalid inputs. You should define your test cases before you write your procs. Then you have a reproducable set of tests to run every time someone changes the complex process.




回答6:


My team uses SPs by rule as our interface to the database; we do it in a way that the application user can ONLY execute SPs (with our naming convention).

One best practice that we use, that works well, is that certain test scripts are contained within the SP comments, and must be executed on each rev of an SP, or development of a new SP.

You should always, ALWAYS test the SP as thoroughly as possible without any application layer involved (through Management Studio, for example).




回答7:


This trick is pretty handy:

Custom user configurable Profiler Events




回答8:


Make sure you step into main stored proc in VS2005/2008, when it encounters a nested function, hit F11 (step into ) to enter in...continue debugging... It was not very obvious from the debug menu.



来源:https://stackoverflow.com/questions/615747/whats-your-favored-method-for-debugging-ms-sql-stored-procedures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!