2015-07-12 76 views
0

從VB 2008調用此存儲過程。試圖用select * from #temptable的結果填充DataGridView。但是,存儲過程返回項目的結果集和最後一條SQL語句的結果集。我不想爲@Item返回任何東西。只希望在主要聲明中使用它。獲取比我希望從SQL Server存儲過程更多的結果集

ALTER PROCEDURE [dbo].[spGetItem2] 
    @ScanData NVARCHAR(50), 
    @RecordCount INT OUTPUT 
AS 
SET NOCOUNT ON 

Begin 
DECLARE @Item char(20) 

SELECT i.Item 
FROM dbo.Inv_Item i 
WHERE i.Item = @ScanData 

Set @RecordCount = @@RowCount 

if @@RowCOunt > 0 
    Set @Item = @Scandata 
Else 
    SELECT @Item = Inv_Item.Item 
    FROM Inv_UPC 
    INNER JOIN Inv_Item ON Inv_UPC.Item = Inv_Item.Item 
    WHERE upc = @Scandata 

    --Set @Item = @Item 
    Set @RecordCount = @@RowCount 
END 

Begin 
    SELECT  
     Inv_Item_Location_Detail.Area, Inv_Item_Location_Detail.Location, 
     Inv_Item_Location_Detail.Item, Inv_Item.[Description], 
     Inv_Item_Location_Detail.[Level], Inv_item_levels.[levelDescription], 
     Inv_Item_Location_Detail.GID, 
     Count(Inv_Item_Location_Detail.Item) as QTY 
    INTO 
     #TempTable 
    FROM   
     Inv_Item_Location_Detail 
    LEFT OUTER JOIN 
     Inv_Item ON Inv_Item_Location_Detail.Item = Inv_Item.Item 
    LEFT OUTER JOIN 
     Inv_Item_Levels ON Inv_Item_Location_Detail.[Level] = Inv_Item_Levels.[Level] 
         AND Inv_Item_Location_Detail.Item = Inv_Item_Levels.ItemNumber 
    WHERE  
     (Inv_Item_Location_Detail.Item = @Item) 
    GROUP BY 
     Inv_Item_Location_Detail.Area, Inv_Item_Location_Detail.Location, 
     Inv_Item_Location_Detail.Item, Inv_Item_Location_Detail.[Level], 
     Inv_Item_Location_Detail.GID, Inv_Item.[Description], 
     Inv_item_levels.[levelDescription] 
End 

Select * from #Temptable 

- 首先返回@Item的值。 - 然後它返回一個結果集,這是我正在尋找的。

+1

使用'if exists(select ...)'而不是選擇並檢查'@@ rowcount'。爲什麼要插入臨時表然後從中進行選擇而不是直接選擇? –

回答

1

我想你應該改變你的第一個SELECT到:

DECLARE @Item char(20) 

SELECT @Item = i.Item -- store this value into a variable 
FROM dbo.Inv_Item i 
WHERE i.Item = @ScanData 

這樣一來,SELECT不應該返回從存儲過程的結果集。

相關問題