运行SQL 2008 R2。此proc在VS 2008 DB Project中通过了验证,但在管理工作室中失败了。有什么想法吗?
CREATE PROCEDURE [dbo].[WRHS_chkBinTransferItem]
(
@BinFromID int,
@BarCodeNumber varchar(50),
@Count int,
@Message varchar(500) OUTPUT
)
AS
DECLARE
@LotTrackIND int,
@ItemID int,
@QtyAvail int,
@LotNumber varchar(50)
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = ''
FROM WRHS_ItemUOM iu
INNER JOIN WRHS_Item i ON iu.ItemID = i.ItemID
WHERE iu.BarcodeNumber = @BarCodeNumber
UNION
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = wil.LotNumber
FROM WRHS_WarehouseItemLotIdentify wili
INNER JOIN WRHS_WarehouseItemLot wil ON wili.WarehouseItemLotID = wil.WarehouseItemLotID
INNER JOIN WRHS_WarehouseItem wi ON wil.WarehouseItemID = wi.WarehouseItemID
INNER JOIN WRHS_Item i ON wi.ItemID = i.ItemID
WHERE wili.ItemIdentification = @BarCodeNumber
IF @LotTrackIND = 1
BEGIN
IF @ItemID > 0
BEGIN
SELECT DISTINCT @QtyAvail = q.Qty
FROM WRHS_BinItem bi
INNER JOIN WRHS_BinItemQty q ON bi.BinItemID = q.BinItemID
WHERE bi.BinID = @BinFromID AND bi.ItemID = @ItemID AND q.LotNumber = @LotNumber
IF @Count <= @QtyAvail
BEGIN
SELECT DISTINCT q.BinItemQtyID, RTRIM(i.ItemNumber) as ItemNumber, i.LicensePlateInd, q.LotNumber, i.LotTrackingInd, i.ItemID, wi.ForcedPutAwayInd
FROM WRHS_Bin b
INNER JOIN WRHS_BinItem bi ON b.BinID = bi.BinID
INNER JOIN WRHS_BinItemQty q ON bi.BinItemID = q.BinItemID
INNER JOIN WRHS_Item i On bi.ItemID = i.ItemID
INNER JOIN WRHS_WarehouseItem wi ON b.WarehouseID = wi.WarehouseID AND i.ItemID = wi.ItemID
WHERE b.BinID = @BinFromID AND bi.ItemID = @ItemID AND q.LotNumber = @LotNumber
SET @Message = 'Success'
END
ELSE
BEGIN
SET @Message = 'Selected Quantity not Available'
END
END
ELSE
BEGIN
SET @Message = 'Invalid BarCode Scan, this Item is Lot Tracked and the proper BarCode must be scanned.'
END
END
ELSE
BEGIN
IF @ItemID > 0
BEGIN
SELECT DISTINCT @QtyAvail = q.Qty
FROM WRHS_BinItem bi
INNER JOIN WRHS_BinItemQty q ON bi.BinItemID = q.BinItemID
WHERE bi.BinID = @BinFromID AND bi.ItemID = @ItemID AND q.LotNumber = @LotNumber
IF @Count <= @QtyAvail
BEGIN
SELECT DISTINCT q.BinItemQtyID, RTRIM(i.ItemNumber) as ItemNumber, i.LicensePlateInd, q.LotNumber, i.LotTrackingInd, i.ItemID, wi.ForcedPutAwayInd
FROM WRHS_Bin b
INNER JOIN WRHS_BinItem bi ON b.BinID = bi.BinID
INNER JOIN WRHS_BinItemQty q ON bi.BinItemID = q.BinItemID
INNER JOIN WRHS_ItemUOM iu ON bi.ItemID = iu.ItemID
INNER JOIN WRHS_Item i On bi.ItemID = i.ItemID
INNER JOIN WRHS_WarehouseItem wi ON b.WarehouseID = wi.WarehouseID AND i.ItemID = wi.ItemID
WHERE b.BinID = @BinFromID AND bi.ItemID = @ItemID AND q.LotNumber = @LotNumber
SET @Message = 'Success'
END
ELSE
BEGIN
SET @Message = 'Selected Quantity not Available'
END
END
ELSE
BEGIN
SET @Message = 'Invalid BarCode Scan'
END
END
答案 0 :(得分:3)
对于赋值,部分,您可以在子查询的帮助下进行处理,例如:
Select @ItemID = ItemID, @LotTrackIND = LotTrackingInd, @LotNumber = LotNumber
from (
SELECT i.ItemID, i.LotTrackingInd, '' LotNumber
FROM WRHS_ItemUOM iu
INNER JOIN WRHS_Item i ON iu.ItemID = i.ItemID
WHERE iu.BarcodeNumber = @BarCodeNumber
UNION
SELECT i.ItemID, i.LotTrackingInd, wil.LotNumber
FROM WRHS_WarehouseItemLotIdentify wili
INNER JOIN WRHS_WarehouseItemLot wil ON wili.WarehouseItemLotID = wil.WarehouseItemLotID
INNER JOIN WRHS_WarehouseItem wi ON wil.WarehouseItemID = wi.WarehouseItemID
INNER JOIN WRHS_Item i ON wi.ItemID = i.ItemID
WHERE wili.ItemIdentification = @BarCodeNumber
) q1
我想你没有迭代你的查询可能返回的(可能的)多条记录。
最好的问候。
答案 1 :(得分:0)
你不能做变量作业
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = ''
...
UNION
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = wil.LotNumber
...
正如错误所说。
至于为什么它在VS中传递,这是一个VS错误。
答案 2 :(得分:0)
是的,错误就在这部分,
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = ''
FROM WRHS_ItemUOM iu
INNER JOIN WRHS_Item i ON iu.ItemID = i.ItemID
WHERE iu.BarcodeNumber = @BarCodeNumber
UNION
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = wil.LotNumber
FROM WRHS_WarehouseItemLotIdentify wili
INNER JOIN WRHS_WarehouseItemLot wil ON wili.WarehouseItemLotID = wil.WarehouseItemLotID
INNER JOIN WRHS_WarehouseItem wi ON wil.WarehouseItemID = wi.WarehouseItemID
INNER JOIN WRHS_Item i ON wi.ItemID = i.ItemID
WHERE wili.ItemIdentification = @BarCodeNumber
如果语句具有UNION,则无法使用select语句为变量赋值。如果你考虑一下,它没有意义。变量只能包含一个值,但应该选择哪个值?第一个选择语句或第二个语句中的一个?
你必须明确做出这个选择。假设您希望第二个select语句优先(即从第二个select中取值。如果第二个select语句没有返回值,那么取第一个返回的值),你可以这样做
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = ''
FROM WRHS_ItemUOM iu
INNER JOIN WRHS_Item i ON iu.ItemID = i.ItemID
WHERE iu.BarcodeNumber = @BarCodeNumber
SELECT @ItemID = i.ItemID, @LotTrackIND = i.LotTrackingInd, @LotNumber = wil.LotNumber
FROM WRHS_WarehouseItemLotIdentify wili
INNER JOIN WRHS_WarehouseItemLot wil ON wili.WarehouseItemLotID = wil.WarehouseItemLotID
INNER JOIN WRHS_WarehouseItem wi ON wil.WarehouseItemID = wi.WarehouseItemID
INNER JOIN WRHS_Item i ON wi.ItemID = i.ItemID
WHERE wili.ItemIdentification = @BarCodeNumber