Possible to get the line number of the currently executing sproc in SQL Server?

不想你离开。 提交于 2020-01-04 05:43:28

问题


A few years back I worked in a Sybase/Delphi environment, using the BDE to connect to the DB server. We had a little Delphi app that, given the name of a currently executing stored procedure, could tell you what line of that sproc was currently being executed. This was exceptionally useful for debugging sprocs that seemed to be hanging.

I'd like to use this functionality in SQL Server, but I can't remember whether it was a Sybase or a BDE feature. Is this functionality available in SQL Server and if so, what command(s) do I need to use to view this information?


回答1:


You can use something like

SELECT
    CASE
        WHEN statement_end_offset = -1
        THEN text
        ELSE SUBSTRING(text,statement_start_offset/2,(statement_end_offset- statement_start_offset)/2)
    END,
    statement_end_offset, statement_start_offset
FROM    sys.dm_exec_requests
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE session_id = 53 --Or whatever!

The statement_start_offset and statement_end_offset are character offsets that map to the statement currently being executed rather than a line number.

A single line can contain multiple executable statements and a single statement can span multiple lines.




回答2:


If you are using SQL Server 2008, you can debug stored procs much like you can debug C# code. You can set break points and execute statement by statement. You can do this from within SSMS.



来源:https://stackoverflow.com/questions/4550342/possible-to-get-the-line-number-of-the-currently-executing-sproc-in-sql-server

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