从SQL Server存储过程中获取比我希望的结果集更多的结果集

时间:2015-07-12 19:40:04

标签: sql-server

从VB 2008调用此存储过程。尝试使用DataGridView的结果填充select * from #temptable。但是,存储过程返回item的结果集和最后一个SQL语句的结果集。我不想为@Item返回任何内容。只希望在主要声明中使用它。

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 个答案:

答案 0 :(得分: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不应该作为存储过程的结果集返回。