如何调试tsql存储过程?

发布于 2024-12-07 03:38:16 字数 293 浏览 0 评论 0原文

如何调试 tsql 存储过程。我已经尝试过以下链接。

http://msdn.microsoft.com/en- us/library/ms241871(v=vs.80).aspx

但我无法达到断点。有没有更好的调试方法。我的环境是

Sql Express 2008、Visual Studio 2010

How do I debug a tsql Stored procedure. I have tried the following link.

http://msdn.microsoft.com/en-us/library/ms241871(v=vs.80).aspx

But I am unable to hit the break point. Is there a better way to debug. My environment is

Sql Express 2008, Visual Studio 2010

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

王权女流氓 2024-12-14 03:38:16

我发现 SQL Managment studio 中的调试器不可靠,因为它非常依赖于数据库服务器上的正确权限,而这些权限并不总是可用。

我使用的一种替代方法是将存储过程转换为长查询。我首先将所有参数移至变量声明并设置它们的值。例如,以下内容

    ALTER PROCEDURE [dbo].[USP_ConvertFinancials] (@EffectiveDate datetime, @UpdatedBy nvarchar(100)) 
AS
BEGIN

    DECLARE @PreviousBusinessDay datetime

将变为

DECLARE @Value int, 
        , @EffectiveDate datetime = '01-Jan-2011
        , @UpdatedBy nvarchar(100) = 'System'

这允许我从顶部开始在存储过程中运行查询。当我向下浏览查询时,我可以通过简单地选择变量并从顶部重新运行查询来检查变量的值:

SELECT @Value 

我还可以注释掉 INSERT-SELECT 语句的 INSERT 部分,以查看插入到表和表中的内容变量。

使用此方法,存储过程中的错误通常会变得非常明显。一旦查询正确运行,我只需将代码复制到我的过程中并重新编译即可。

祝你好运!

I have found the debugger in SQL Managment studio unreliable as it's so dependant on having the correct permissions on the db server which are not always available.

One alternate method I use is to convert the stored proc into a long query. I start by moving any parameteres to variable declarations and set their values. For examples the following

    ALTER PROCEDURE [dbo].[USP_ConvertFinancials] (@EffectiveDate datetime, @UpdatedBy nvarchar(100)) 
AS
BEGIN

    DECLARE @PreviousBusinessDay datetime

would become

DECLARE @Value int, 
        , @EffectiveDate datetime = '01-Jan-2011
        , @UpdatedBy nvarchar(100) = 'System'

This allows me to run the queries within the stored procedure starting from the top. As I move down through the queries, I can check the values of variables by simply selecting them and rerunning the query from the top:

SELECT @Value 

I can also comment out the INSERT portion of INSERT-SELECT statements to see what is being inserted into tables and table variables.

The bug in the stored proc usually becomes quite evident using this method. Once I get the query running correctly I can simply copy the code to my proc and recompile.

Good luck!

猫七 2024-12-14 03:38:16

您可以尝试Sql Profiler,它不允许像“此时中断”这样的经典调试,但会为您提供有关查询/SP 执行的每个步骤所发生情况的详细信息。

不幸的是,微软没有为其提供Express Edition版本的Sql Server。
但是:)有一个很好的(相对而言,因为它没有提供微软中存在的大量过滤标准)并且免费的替代方案 - SQL Server 2005/2008 Express Profiler

You can try out Sql Profiler, it does not allows a classical debugging like "break at this point" but gives you an information in great detail about what is going on on each step of a query/SP execution.

Unfortunately Microsoft does not provide it with Express Edition version of Sql Server.
BUT :) There is a good (relatively because it does not provide a lot of filtering criterias which exists in Microsoft's one) and free alternative - SQL Server 2005/2008 Express Profiler.

晚雾 2024-12-14 03:38:16

调试存储过程。

  1. 检查逻辑是否合理。
  2. 使用断点来帮助查找问题。
  3. 尽量按照复杂的流程进行模块化设计。
  4. 将任务分成多个简单的任务。
  5. 使用一个主存储过程来控制顶层,并使用多个子存储过程逐步完成工作。

  6. 根据优化,使用执行计划、SS Profiler 和 DTA 工具。

Debug a stored procedure.

  1. check the logic whether it makes sense or not.
  2. use break point to help find issues.
  3. try to do the modular design as per complex process.
  4. divide the task into multiple simple ones.
  5. use a master stored procedure to take control on the top, and use several child stored procedures to do the job step by step.

  6. As per the optimization, use execution plan, SS Profiler and DTA tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文