无需向开发人员授予系统管理员的 SQL Server 调试器权限

GWR*_*GWR 5 sql-server permissions debugging sql-server-2014

BOL 文档说运行调试器需要 sysadmin 权限。

我 90% 肯定没有解决此要求的方法,但我想我会问以防万一有人找到了一种方法来授予调试器权限而不授予系统管理员权限。

当您有一个开发团队需要逐步执行带有变量等的复杂游标循环以调试某些方面时,人们会怎么做?

大多数商店甚至不允许开发人员在开发服务器上拥有系统管理员权限,并且许多商店不允许开发人员使用他们自己的开发人员 sql server 版本在他们的本地机器上保留企业数据的副本,例如由于 PII 和数据安全原因。

不知道为什么调试器会这样设置。

所以,我很好奇其他人如何处理类似场景中的 Debugger 权限请求。

你在你的环境中做什么?

Joh*_* N. 4

您可以将declare @idebug int变量添加到存储过程中,然后在需要相关信息时围绕重要位进行编码。

您的存储过程将看起来有点像这样:

CREATE PROCEDURE [dbo].[uspDoSomething]
...
@iiDebug int = 0
...
AS
...
BEGIN
    /* debugging configuration */   
    declare @debug int

    /* debug settings
    1 = turn on debug information
    2 = turn on all possible outputs
    4 = turn on transaction handling

    e.g.: Adding an @iDebug paramter of 6 will turn on transaction handling 
    and turn on all possible output information

    e.g.: Adding an @iDebug value of 1 will turn on debugging information
    */ 

    set @debug = @iiDebug
    ....
    if @debug & 1 = 1  print 'Checking variables...'

                    /* If general output has been turned on print output*/ 
                    if @debug & 2 = 2 
                        BEGIN
                            PRINT 'Debug comment here' + convert(varchar(100), @iRetVal) + 'Debug comment here' + convert(varchar(20),getdate())
                        end

                    close <cursor_name>
                    deallocate <cursor_name>

                    RETURN(@iRetVal)
    ...
    END
    ...
END
Run Code Online (Sandbox Code Playgroud)

这只是如何完成此操作的一个示例。

然后您可以使用以下方式调用存储过程:

execute uspDoSomething @iiDebug = 3
Run Code Online (Sandbox Code Playgroud)

...这将提供基本(按位1)和详细(按位2)信息,具体取决于您插入相关代码的位置。

我在运行存储过程时遇到了一次问题,该存储过程没有产生正确的结果,我必须调试各个语句,因此我只需在存储过程中输入各种调试级别,并在需要时使用相关值运行存储过程,具体@iiDebug取决于我需要的信息水平。

输入值示例:

@iiDebug = 1 -- > Basic "where am I in the sproc" information
@iiDebug = 2 -- > Print of @nvSQL values
@iiDebug = 4 -- > Run individual execution of statements in BEGIN and COMMIT transactions
Run Code Online (Sandbox Code Playgroud)

代码示例(输入变量@iiDebug存储在@debug存储过程代码中):

set @debug = @iiDebug
...
...
if @debug & 4 = 4
BEGIN
    begin tran mojo
END
if @debug & 2 = 2 then print @nvSQL

exec @iRetVal = sp_executesql @nvSQL
if @iRetVal <> 0 
BEGIN
    /* If transactions have been turned on then rollback if failed */
    if @debug & 4 = 4
    BEGIN
        rollback tran mojo
    END

/* If transactions have been turned on then commit on success */
if @debug & 4 = 4
BEGIN
    commit tran mojo
END
Run Code Online (Sandbox Code Playgroud)

这些只是简单示例,说明如何在无法访问 SQL Server 调试器或所需权限的情况下引入调试。

注意:
它可能会有点消耗性能,最好从生产中删除。