2015-12-18 65 views
-1

空我有這個存儲過程:SQL Server存儲過程返回,而不是INTT

@upc AS VARCHAR(13) = NULL, 
@cert_code AS VARCHAR(15) = NULL, 
@vendor AS INT = NULL, 
@count AS INT = 0 OUTPUT, 
@store AS VARCHAR(3) = NULL, 
@corporate AS VARCHAR = Null, 
@zone AS VARCHAR = Null, 
@region AS VARCHAR = Null, 
@class AS VARCHAR = Null 
AS 
BEGIN -- Added for new functionality 
    SET NOCOUNT ON; 

    If @store IS NOT NULL 
    BEGIN 
     -- Get the storeID from Stores 
     DECLARE @storeID AS INT 

     SELECT @storeID = StoreID 
     FROM Enterprise..Stores 
     WHERE Store = @store 
    END 

    IF @storeID IS NOT NULL 
    BEGIN 
     IF @cert_code IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsStore 
      WHERE (StoreID = @storeID) AND (upc = @upc) 
     END 
     ELSE 
     BEGIN 
      IF @vendor IS NULL 
      BEGIN 
       SELECT @count = COUNT(*) 
       FROM Enterprise..ProductsStore 
       WHERE (StoreID = @storeID) AND (cert_code = @cert_code) 
      END 
      ELSE 
      BEGIN 
       SELECT @count = COUNT(*) 
       FROM Enterprise..ProductsStore 
       WHERE (StoreID = @storeID) 
        AND (cert_code = @cert_code) AND (vendor = @vendor) 
      END 
     END 
    END 
END 

If @zone IS NOT NULL 
BEGIN 
    IF @cert_code IS NULL 
    BEGIN 
     SELECT @count = COUNT(*) 
     FROM Enterprise..ProductsZone 
     WHERE (upc = @upc) 
    END 
    ELSE 
    BEGIN 
     IF @vendor IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsZone 
      WHERE (cert_code = @cert_code) 
     END 
     ELSE 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsZone 
      WHERE (cert_code = @cert_code) AND (vendor = @vendor) 
     END 
    END 
END 

IF @region IS NOT NULL 
BEGIN 
    IF @cert_code IS NULL 
    BEGIN 
     SELECT @count = COUNT(*) 
     FROM Enterprise..ProductsRegion 
     WHERE (upc = @upc) 
    END 
    ELSE 
    BEGIN 
     IF @vendor IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsRegion 
      WHERE (cert_code = @cert_code) 
     END 
     ELSE 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsRegion 
      WHERE (cert_code = @cert_code) AND (vendor = @vendor) 
     END 
    END 
END 

IF @class IS NOT NULL 
BEGIN 
    IF @cert_code IS NULL 
    BEGIN 
     SELECT @count = COUNT(*) 
     FROM Enterprise..ProductsClass 
     WHERE (upc = @upc) 
    END 
    ELSE 
    BEGIN 
     IF @vendor IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsClass 
      WHERE (cert_code = @cert_code) 
     END 
     ELSE 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsClass 
      WHERE (cert_code = @cert_code) AND (vendor = @vendor) 
     END 
    END 
END 

IF @corporate IS NOT NULL 
BEGIN 
    IF @cert_code IS NULL 
    BEGIN 
     SELECT @count = COUNT(*) 
     FROM Enterprise..ProductsMaster 
     WHERE (upc = @upc) 
    END 
    ELSE 
    BEGIN 
     IF @vendor IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsMaster 
      WHERE (cert_code = @cert_code) 
     END 
     ELSE 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsMaster 
      WHERE (cert_code = @cert_code) AND (vendor = @vendor) 
     END 
    END 
END 
GO 

當我有@store IS NOT NULL它計數及然而返回按預期的方式INT,當@corporate之一,@zone@region@class被設置爲NOT NULL,我得到的輸出爲null,而不是像預期的INT。

這裏有什麼問題?

+0

你在這裏有點太多了。你能否爲你的output = null情況下的每個變量傳遞一個參數值?我不確定有多少人在任何時候都是空的。 – Quantumplate

+0

你也可以跟蹤哪些選擇語句是問題?也許用SELECT 999逐一替換它們,看它是否返回999而不是null?如果這沒有幫助,可以嘗試刪除位,直到你有一個較小的情況返回NULL,這樣我們可以將其中的一部分排除在外? – Quantumplate

+0

如果你在開始時添加了SET @count = 0(在SET NOCOUNT ON之後),你的proc是否工作?你可能沒有'ELSE'分支在那裏撿起它 –

回答

2

我不知道,如果它的一個錯字的錯誤還是其他什麼東西,但請你開始,爲IF結束,ELSE
從我看到這組驗證碼作爲結束的比賽中的過程結束主存儲過程的BEGIN。

@upc AS VARCHAR(13) = NULL, 
@cert_code AS VARCHAR(15) = NULL, 
@vendor AS INT = NULL, 
@count AS INT = 0 OUTPUT, 
@store AS VARCHAR(3) = NULL, 
@corporate AS VARCHAR = Null, 
@zone AS VARCHAR = Null, 
@region AS VARCHAR = Null, 
@class AS VARCHAR = Null 
AS 
BEGIN -- Added for new functionality 
    SET NOCOUNT ON; 

    If @store IS NOT NULL 
    BEGIN 
     -- Get the storeID from Stores 
     DECLARE @storeID AS INT 

     SELECT @storeID = StoreID 
     FROM Enterprise..Stores 
     WHERE Store = @store 
    END 

    IF @storeID IS NOT NULL 
    BEGIN 
     IF @cert_code IS NULL 
     BEGIN 
      SELECT @count = COUNT(*) 
      FROM Enterprise..ProductsStore 
      WHERE (StoreID = @storeID) AND (upc = @upc) 
     END 
     ELSE 
     BEGIN 
      IF @vendor IS NULL 
      BEGIN 
       SELECT @count = COUNT(*) 
       FROM Enterprise..ProductsStore 
       WHERE (StoreID = @storeID) AND (cert_code = @cert_code) 
      END 
      ELSE 
      BEGIN 
       SELECT @count = COUNT(*) 
       FROM Enterprise..ProductsStore 
       WHERE (StoreID = @storeID) 
        AND (cert_code = @cert_code) AND (vendor = @vendor) 
      END 
     END 
    END 
END -- This End is wrong should not be here 

如果這只是一個錯字,請正確地發佈代碼。還有其他人指出,將@count參數初始化爲零。
看來程序的其餘部分是從未執行的,因爲@count未初始化,它返回空

+0

這是現貨,我的主要陳述如果'@zone'開始與之前的結尾相匹配。我的程序的其餘部分沒有運行,因此'@count'永遠不會被分配。謝謝 – user3839756

+0

@ user3838756當你在發佈後立即說出同樣的內容時,你沒有看過我的回答嗎? – shawnt00

1

您的查詢都不應該能夠爲您的@count變量分配一個空值,因爲它們全都使用count()聚合。這意味着沒有任何查詢運行。

我認爲在if @zone之前的end是錯誤的。但是我很確定,程序定義結尾的end必須是批次的結尾。也許我錯了,你的proc實際上比你預期的更早結束?

你當然可以壓縮很多分支到你的查詢。這是我的嘗試。我削減了大部分縮進和開始/結束對。

declare @storeID as int; 
if @store is not null 
begin 
    -- Get the storeID from Stores 
    SELECT @storeID = StoreID 
    FROM Enterprise..Stores 
    WHERE Store = @store; 

    if @storeID is not null 
    SELECT @count = COUNT(*) 
    FROM Enterprise..ProductsStore 
    WHERE StoreID = @storeID and 
      (
        @cert_code is null and upc_code = @upc_code 
       or cert_code = @cert_code and (vendor = @vendor or @vendor is null) 
      ); 
end 

if @zone is not null 
SELECT @count = COUNT(*) 
FROM Enterprise..ProductsZone 
WHERE  @cert_code is null and upc_code = @upc_code 
     or cert_code = @cert_code and (vendor = @vendor or @vendor is null); 

if @region is not null 
SELECT @count = COUNT(*) 
FROM Enterprise..ProductsRegion 
WHERE  @cert_code is null and upc_code = @upc_code 
     or cert_code = @cert_code and (vendor = @vendor or @vendor is null); 

if @class is not null 
SELECT @count = COUNT(*) 
FROM Enterprise..ProductsClass 
WHERE  @cert_code is null and upc_code = @upc_code 
     or cert_code = @cert_code and (vendor = @vendor or @vendor is null); 
+0

'@count AS INT = 0 OUTPUT'不是初始化參數,它是參數的默認值,如果參數值在調用期間沒有明確提供的程序。所以,在程序開始時可以使用'@ count'和'NULL'值。 –

+0

是的,我一直忘記這是一個程序。你說得對。 – shawnt00

1

爲什麼你把varchar? 使用波紋管代碼。

@corporate AS VARCHAR(max) = NULL, 
@zone AS VARCHAR(max) = NULL, 
@region AS VARCHAR(max) = NULL, 
@class AS VARCHAR(max) = NULL 
+1

斑點! VARCHAR = VARCHAR(1)=截斷的輸入值 –

+0

值得注意。但是當參數被截斷時,計數仍然不是零而不是零? – shawnt00

+0

感謝您指出這一點,因爲它會影響我的輸出,但是這並沒有解決我原來的問題。 – user3839756