5
我正在權衡使用三種不同方法之一將存儲過程中的單個標量值返回到C#例程中的潛在性能影響。誰能告訴我哪些是「更快」,最重要的是,爲什麼?SQL Server性能結果集vs輸出參數與返回值
方法1:
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
方法2:
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
方法3:
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
另一個精度。使用方法3(RETURN),您僅限於INTEGER類型。如果你想返回另一種數據類型,你應該使用方法2或方法1. –
我感謝所有的幫助。我個人傾向於#2。但是,由於我在#2和#3中都返回了一個INTEGER,所以我可以看到#3的表現最佳。如果我不得不返回某種類型的字符串,我會使用#2。當然,如前所述,只有在需要結果集的情況下,我纔會使用#1。 –