简体   繁体   English

您最喜欢调试 MS SQL 存储过程的方法是什么?

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

Most of my SPs can simply be executed (and tested) with data entered manually.我的大多数 SP 都可以使用手动输入的数据简单地执行(和测试)。 This works well and using simple PRINT statements allows me to "debug".这很好用,使用简单的 PRINT 语句可以让我“调试”。

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.从我的 web 应用程序中触发事情更容易。

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+注意:我假设使用 SQL Server 2005+

Profiler is very handy, just add SP:StmtStarting events, and filter the activity down to just your process by setting SPID=xxx. Profiler 非常方便,只需添加 SP:StmtStarting 事件,并通过设置 SPID=xxx 将活动过滤到您的进程。 Once you have it set up, it's a breeze to see what's going on.设置好之后,就可以轻而易举地看到发生了什么。

You can actually attach a debugger to your sql server:) - from vs, given you have configured that on your sql server.您实际上可以将调试器附加到您的 sql 服务器:) - 来自 vs,假设您已经在 sql 服务器上配置了它。

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 .查看此链接了解更多信息,注意您可以设置断点:) 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检查此链接以获取更一般的信息集: 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."更新:关于“然而,在某些情况下,涉及多个存储过程并且查找要输入的有效数据很乏味。从我的 web 应用程序中触发事情更容易。”

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.如果程序由 web 应用程序驱动,那么它是一个可以随时运行有效测试 + 输入的好地方。 If there are multiple apps that interact with the procedures, I would look at unit testing the procedures instead.如果有多个应用程序与程序交互,我会考虑对程序进行单元测试。

I prefer not to debug, I do test driven development instead, which almost eliminates the need to debug.我不喜欢调试,而是进行测试驱动开发,这几乎消除了调试的需要。

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.也就是说, MS KB 316549描述了如何使用 Visual Studio 来调试存储过程。

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.不显示来自 SQL PRINT 语句的 Output。

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.不过,在我作为 DBA 的角色中,这几乎就是我每天从应用程序开发人员那里遇到的情况。

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.确保在VS2005/2008中进入主存储过程,当遇到嵌套的function时,按F11(进入)进入...继续调试...从调试菜单上看不是很明显。

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.然后,每次有人更改复杂流程时,您就有一组可重现的测试来运行。

My team uses SPs by rule as our interface to the database;我的团队按规则使用 SP 作为我们与数据库的接口; we do it in a way that the application user can ONLY execute SPs (with our naming convention).我们这样做的方式是应用程序用户只能执行 SP(使用我们的命名约定)。

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.我们使用的一种效果很好的最佳实践是,某些测试脚本包含在 SP 注释中,并且必须在 SP 的每个版本或新 SP 的开发中执行。

You should always, ALWAYS test the SP as thoroughly as possible without any application layer involved (through Management Studio, for example).您应该始终尽可能彻底地测试 SP,而不涉及任何应用层(例如,通过 Management Studio)。

This trick is pretty handy:这个技巧非常方便:

Custom user configurable Profiler Events 自定义用户可配置 Profiler 事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM