考虑以下T-SQL代码片段:

CREATE PROC dbo.SquareNum(@i INT OUTPUT)
AS
BEGIN
    SET @i = @i * @i
    --SELECT @i
END
GO

DECLARE @a INT = 3, @b INT = 5
EXEC dbo.SquareNum @a OUTPUT
EXEC dbo.SquareNum @b
SELECT @a AS ASQUARE, @b AS BSQUARE
GO
DROP PROC dbo.SquareNum

结果集为:
ASQUARE     BSQUARE
----------- -----------
9           5

可以看出,@b不是平方的,b/c它没有作为输出参数传递(传递参数时没有OUTPUT限定词)。

我想知道是否有一种方法可以在存储过程主体(在本例中为dbo.SquareNum主体)内进行检查,以查看是否确实将一个参数作为OUTPUT参数传入了吗?

最佳答案

   ------ THIS WILL GIVE YOU THE BOTH VALUE IN squared------

    CREATE PROC dbo.SquareNum(@i INT OUTPUT)
    AS
    BEGIN
        SET @i = @i * @i
        --SELECT @i
    END
    GO

    DECLARE @a INT = 3, @b INT = 5
    EXEC dbo.SquareNum @a OUTPUT
    EXEC dbo.SquareNum @b OUTPUT
    SELECT @a AS ASQUARE, @b AS BSQUARE
    GO
    DROP PROC dbo.SquareNum


  -----TO CHECK STORED PROCEDURE BODY-----

    SELECT OBJECT_NAME(object_id),
           OBJECT_DEFINITION(object_id)
    FROM  sys.procedures
    WHERE OBJECT_DEFINITION(object_id) =(SP_NAME)

关于sql-server - T-SQL存储过程-检测参数是否作为OUTPUT提供,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36691604/

10-13 05:53