我有以下存储过程:

ALTER PROCEDURE [dbo].[CheckAdminExists]
    @SID NVARCHAR(50),
    @AdminName NVARCHAR(MAX)
AS
    SELECT
        Administrator.ID
    FROM
        Administrator
    WHERE
        Administrator.SID = @SID
        AND Administrator.Name = @AdminName
GO

现在,我想用这样的代码创建另一个SP:
IF NOT NULL (EXECUTE CheckAdminExists 'S-1','Admin')
--do something
ELSE
--do something else

正确的语法是什么?

最佳答案

您不应为此使用存储过程,而应使用函数来检查管理员是否存在。

然后在新的存储过程中,调用CheckAdminExists函数:

CREATE FUNCTION [dbo].[CheckAdminExists] (@SID NVARCHAR(50), @AdminName NVARCHAR(MAX))
RETURNS BIT
AS
BEGIN
DECLARE @RetVal INT

SELECT @RetVal = COUNT(Administrator.ID)
FROM
    Administrator
WHERE
    Administrator.SID = @SID
    AND Administrator.Name = @AdminName

IF @RetVal > 0
BEGIN
   RETURN 1
END
    RETURN 0
END

Then in your stored procedure call the function:

DECLARE @AdminExists BIT
SELECT @AdminExists = [dbo].[CheckAdminExists]

IF @AdminExists
BEGIN
   -- your code
END
ELSE
BEGIN
  -- your code
END

关于sql - IF/ELSE取决于存储过程的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10248217/

10-10 16:29