我正在权衡使用三种不同方法之一将单个标量值从存储过程返回到我的 C# 例程的潜在性能影响。谁能告诉我其中哪个“更快”,最重要的是,为什么?
方法一:
CREATE PROCEDURE GetClientId
@DealerCode varchar(10)
AS
BEGIN
SET NOCOUNT ON
SELECT ClientId
FROM Client
WHERE ClientCode = @DealerCode
END
-- this returns null if nothing is found,
-- otherwise it returns ClientId in a ResultSet
方法二:
CREATE PROCEDURE GetClientId
@DealerCode varchar(10),
@ClientValue int out
AS
BEGIN
SET NOCOUNT ON
set @ClientValue = -1
set @ClientValue = (SELECT ClientId
FROM Client
WHERE ClientCode = @DealerCode)
END
-- this returns -1 for ClientValue if nothing is found,
-- otherwise it returns ClientId
-- the value for ClientValue is a scalar value and not a ResultSet
方法三:
CREATE PROCEDURE GetClientId
@DealerCode varchar(10)
AS
BEGIN
SET NOCOUNT ON
declare @ClientValue int
set @ClientValue =
(SELECT ClientId FROM Client WHERE ClientCode = @DealerCode)
if @ClientValue is null or @ClientValue = 0
return -1
else
return @ClientValue
END
-- this uses the return value of the stored procedure;
-- -1 indicates nothing found
-- any positive, non-zero value is the actual ClientId that was located