I've the following stored procedure:
我有以下存储过程:
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
Now I would like to create another SP with a code like that:
现在我想用这样的代码创建另一个SP:
IF NOT NULL (EXECUTE CheckAdminExists 'S-1','Admin')
--do something
ELSE
--do something else
What's the right syntax for doing it?
这样做的正确语法是什么?
2 个解决方案
#1
11
You should not use a stored procedure for this and use a function instead to check if the Admin Exists.
您不应该使用存储过程,而是使用函数来检查Admin是否存在。
Then in your new stored procedure call the CheckAdminExists function:
然后在新的存储过程中调用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
#2
3
Indeed, use FUNCTION
.
确实,使用FUNCTION。
But if you need to return more than 1, use OUTPUT
parameters.
但是,如果需要返回多于1,请使用OUTPUT参数。
#1
11
You should not use a stored procedure for this and use a function instead to check if the Admin Exists.
您不应该使用存储过程,而是使用函数来检查Admin是否存在。
Then in your new stored procedure call the CheckAdminExists function:
然后在新的存储过程中调用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
#2
3
Indeed, use FUNCTION
.
确实,使用FUNCTION。
But if you need to return more than 1, use OUTPUT
parameters.
但是,如果需要返回多于1,请使用OUTPUT参数。