代码之家  ›  专栏  ›  技术社区  ›  Matthew Murdoch

如何连接和调试正在运行的SQL Server存储过程?

  •  6
  • Matthew Murdoch  · 技术社区  · 17 年前

    我正在调查 an odd error from a SQL Server 2005 stored procedure

    因此,我希望能够:

    • 在存储过程中设置断点
    • 等待外部调用该过程并命中断点
    • 逐步完成存储过程

    这可能吗?如果可能,如何实现?

    5 回复  |  直到 9 年前
        1
  •  3
  •   Lucas    17 年前

    您可以从VisualStudio中尝试“服务器资源管理器”,但需要将sqlserver配置为允许调试。以下是一些信息: http://www.4guysfromrolla.com/articles/051607-1.aspx

        2
  •  1
  •   Eppz    17 年前

    使用 SQL Profiler 查看调用过程时发生的情况以及传入的参数。

        3
  •  1
  •   KM.    17 年前

    如果您无法单步执行代码,有两种方法:

    #1连接字符串,并插入到日志文件中。

    DECLARE @Loginfo varchar(7500)
    

    在代码执行过程中,将调试信息附加到其中:

    SET @LogInfo=ISNULL(@LogInfo)+'#01> @x='+COALESCE(CONVERT(varchar(10),@x),'NULL')
    ..
    SET @LogInfo=ISNULL(@LogInfo)+'#02>'
    ..
    SET @LogInfo=ISNULL(@LogInfo)+'#03> top loop'
    

    在日志表中插入值(…,@LogInfo)

    根据事务的使用情况,特别是您的错误,您可能可以多次插入,而不必担心回滚,因此您需要根据您的情况进行更改。

    CREATE PROC log_message
         @Message         varchar(255)
        ,@FileName        varchar(100)
        ,@OverWrite       char(1) = 'N'
    AS
    
    /*
    Log messages to server side text files from stored procedures/triggers/sql scripts
    
      Input parameters:
          Message   - message to put in the log file 
          FileName  - path and name of the file to log the message into
          OverWrite - 'Y'=overwrite entire file with current message
                      'N'=append current message onto end of file
    
      Return code:
          0 - everything was fine
          1 - there was an error
    
    
            NOTE: the command to log the message can not be longer than 255 characters,
                  as a result the message and file name should be less than 245 chars combined
    
    
      Example: EXEC log_message 'Duplicates found','C:\logfile.txt', 'N'
          append the "Duplicates found" message onto the server's "C:\logfile.txt" file
    
    */
    
    BEGIN
        SET NOCOUNT ON
    
        DECLARE @ExecuteString    VARCHAR(255)   --command string can only be 255 chars long
        DECLARE @ReturnValue           int
    
        --build command string
        SET @ExecuteString = RTRIM('echo ' + COALESCE(LTRIM(@Message),'-')
            + CASE WHEN (@OverWrite = 'Y') THEN ' > ' ELSE ' >> ' END + RTRIM(@FileName))
    
        --run command string
        EXEC @ReturnValue=master..xp_cmdshell @ExecuteString
        --IF @ReturnValue!=0
        --    PRINT 'command failed, return value='+CONVERT(varchar(40),@ReturnValue)
    
        RETURN @ReturnValue
    
        SET NOCOUNT OFF
    END
    

    通过代码对该过程进行零星调用,将所需内容写入服务器上的文件中

        4
  •  1
  •   Community Mohan Dere    9 年前

    使现代化 :我不确定是否有方法连接到正在运行的存储过程。您可以使用探查器获取正在执行的语句的实时跟踪(SP:StmtStarting)。也请查看 Apex SQL Debug

    如果您有Visual Studio,则很容易调试:

    Debugging Stored Procedures in Visual Studio 2005

    更多答案如下: What’s your favored method for debugging MS SQL stored procedures?

        5
  •  0
  •   Bill Martin    17 年前