How to trace T-SQL function calls

后端 未结 9 1315
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 04:43

I\'m trying to debug a rather complicated formula evaluator written in T-SQL UDFs (don\'t ask) that recursively (but indirectly through an intermediate func

相关标签:
9条回答
  • 2020-12-15 05:44

    Why not use SQL Profiler with statement level events added?

    Edit: Add events for Stored Procedures : SP:Stmt Starting or SP:Stmt Completed Use variables to debug if needed, i.e. set @debug='i am here'; UDF's, while not technically stored procedures, will get traced with the statement level events.

    0 讨论(0)
  • 2020-12-15 05:47

    Can you take your function, and make a second copy of it, but returning a table type with an additional column for your debug information.

    For example, the mySum function below

    CREATE FUNCTION mySum
    (   
        @param1 int,
        @param2 int
    )
    RETURNS INT AS
    BEGIN
        DECLARE @mySum int
    
        SET @mySum = @param1
    
        SET @mySum = @mySum + @param2
    
        RETURN @mySum
    
    END
    GO
    SELECT dbo.mySum(1, 2)
    

    Would turn into

    CREATE FUNCTION mySumDebug
    (   
        @param1 int,
        @param2 int
    )
    RETURNS @myTable TABLE
    (
        [mySum] int,
        [debug] nvarchar(max)
    )
    AS
    BEGIN
        DECLARE @debug nvarchar(max)
    
        SET @debug = 'Declare @mySum variable. '
        DECLARE @mySum int
    
        SET @debug = @debug + 'Set @mySum = @param1(' + CONVERT(nvarchar(50), @param1) + ') '
        SET @mySum = @param1
    
    
        SET @debug = @debug + 'Add @param2(' + CONVERT(nvarchar(50), @param2) + ') to @mySum(' + CONVERT(nvarchar(50), @mySum) + ') '
        SET @mySum = @mySum + @param2
    
        SET @debug = @debug + 'Return @mySum variable. '
    
        INSERT @myTable (mySum, debug) VALUES (@mySum, @debug)
    
        RETURN
    END
    GO
    SELECT mySum, debug FROM dbo.mySumDebug(1, 2)
    

    Not an ideal solution, but useful just to return some text to help track down a bug.

    0 讨论(0)
  • 2020-12-15 05:49

    This looks like what you need but it's only available in team/pro versions of Visual Studio.

    0 讨论(0)
提交回复
热议问题