Stored Procedure returnerer forkert select (cursor)
HejsaJeg har lavet en Stored procedure, som returnerer en del-værdi, i forbindelse med definering af cursor:
DECLARE f_cursor CURSOR
FOR SELECT ColumnLaengde, ColumnHoejde, ColumnBredde, ColumnCOD, ColumnLand, ColumnVaegt FROM @TableFreight
Det er uønsket. Jeg ønsker slutværdien returneret, som er resultatet af en temporær tabel:
SELECT fp.FreightProductText, k.*
FROM #KolliTable k
LEFT JOIN dan_FreightProduct fp on k.F_FreightProductID = fp.FreightProductID
Jeg regnede med jeg blot skulle skrive RETURN på det jeg ville returnere, men jeg for en forkert værdi. Jeg håber på hjælp.
Her er hele Stored Procedure:
ALTER PROCEDURE [dbo].[DisplayFreigtsLimits]
(
@TableFreight FreightTableType READONLY
)
AS
BEGIN
SET NOCOUNT ON;
/*
DECLARE @FreightTable AS TABLE
(
laengde INT -- ColumnLaengde
, hoejde INT -- ColumnHoejde
, bredde INT -- ColumnBredde
, CODprice DECIMAL -- ColumnCOD
, country VARCHAR(10) -- ColumnLand
, vaegt INT -- ColumnVaegt
);
*/
DECLARE @ExcludedList AS TABLE
(
FreightLimitationID INT
)
SELECT TOP 0 * INTO #KolliTable FROM dan_FrieghtLimitation
SET IDENTITY_INSERT #KolliTable ON
DECLARE @laengde AS INT
DECLARE @hoejde AS INT
DECLARE @bredde AS INT
DECLARE @vaegt AS INT
DECLARE @CODprice AS INT
DECLARE @country AS VARCHAR(5)
DECLARE @ShipmentWeight AS INT
DECLARE @ShipmentType AS INT
DECLARE @ShipmentVolume AS INT
SET @country = (SELECT TOP 1 ColumnLand FROM @TableFreight);
SET @ShipmentWeight = ( SELECT SUM(ColumnVaegt) FROM @TableFreight);
SET @ShipmentVolume = 0;
-- 1) beregn på kolli niveau.
-- Bare een af kollierne, som ikke overholder limit-kravene, vil ekskludere et produkt.
DECLARE f_cursor CURSOR
FOR SELECT ColumnLaengde, ColumnHoejde, ColumnBredde, ColumnCOD, ColumnLand, ColumnVaegt FROM @TableFreight
OPEN f_cursor
FETCH NEXT FROM f_cursor
WHILE @@FETCH_STATUS = 0
BEGIN
SET @laengde = (SELECT TOP 1 ColumnLaengde FROM @TableFreight);
SET @hoejde = (SELECT TOP 1 ColumnHoejde FROM @TableFreight)
SET @bredde = (SELECT TOP 1 ColumnBredde FROM @TableFreight)
SET @vaegt = (SELECT TOP 1 ColumnVaegt FROM @TableFreight)
SET @ShipmentVolume = @ShipmentVolume + (@laengde * @hoejde * @bredde);
-- print @ShipmentVolume
INSERT #KolliTable (FreightLimitationID, F_county_Culture, ShipmentPaymentType, CubicMesureConversion_Shipment
, Min_Length_Kolli, Max_Length_Kolli, Min_Height_Kolli, Max_Height_Kolli, Min_Width_Kolli
, Max_Width_Kolli, Min_Weight_Kolli, Max_Weight_Kolli, Min_Weight_Shipment, Max_Weight_Shipment
, Max_Perimeter_Kolli, ExtraFee_Kolli, Max_COD_Shipment, F_FreightProductID )
(
SELECT FreightLimitationID, F_county_Culture, ShipmentPaymentType, CubicMesureConversion_Shipment
, Min_Length_Kolli, Max_Length_Kolli, Min_Height_Kolli, Max_Height_Kolli, Min_Width_Kolli
, Max_Width_Kolli, Min_Weight_Kolli, Max_Weight_Kolli, Min_Weight_Shipment, Max_Weight_Shipment
, Max_Perimeter_Kolli, ExtraFee_Kolli, Max_COD_Shipment, F_FreightProductID
FROM dan_FrieghtLimitation lim WHERE lim.F_county_Culture LIKE @country
-- AND (lim.ShipmentPaymentType = 1)
AND (lim.Min_Length_Kolli IS NULL OR lim.Min_Length_Kolli < @laengde)
AND (lim.Max_Length_Kolli IS NULL OR lim.Max_Length_Kolli > @laengde)
AND (lim.Min_Height_Kolli IS NULL OR lim.Min_Height_Kolli < @hoejde)
AND (lim.Max_Height_Kolli IS NULL OR lim.Max_Height_Kolli > @hoejde)
AND (lim.Min_Width_Kolli IS NULL OR lim.Min_Width_Kolli < @bredde)
AND (lim.Max_Width_Kolli IS NULL OR lim.Max_Width_Kolli > @bredde)
AND (lim.Min_Weight_Kolli IS NULL OR lim.Min_Weight_Kolli < @vaegt)
AND (lim.Max_Weight_Kolli IS NULL OR lim.Max_Weight_Kolli > @vaegt)
AND (lim.Max_Perimeter_Kolli IS NULL OR lim.Max_Perimeter_Kolli > (@laengde + 2 * @bredde + 2 * @hoejde))
AND (lim.Min_Weight_Shipment IS NULL OR lim.Min_Weight_Shipment < @ShipmentWeight)
AND (lim.Max_Weight_Shipment IS NULL OR lim.Max_Weight_Shipment > @ShipmentWeight)
AND (SELECT COUNT(*) FROM #KolliTable k WHERE k.FreightLimitationID = lim.FreightLimitationID) = 0
)
-- select (@laengde + 2 * @bredde + 2 * @hoejde) AS OMKREDS
FETCH NEXT FROM f_cursor
CLOSE f_cursor;
DEALLOCATE f_cursor;
DROP TABLE #KolliTable
-- SELECT * FROM #KolliTable
-- Delete wrong insertion from Kolli
DELETE FROM #KolliTable WHERE Min_Length_Kolli IS NOT NULL AND Min_Length_Kolli > ( select MIN(f.ColumnLaengde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Max_Length_Kolli IS NOT NULL AND Max_Length_Kolli < ( select MAX(f.ColumnLaengde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Min_Height_Kolli IS NOT NULL AND Min_Height_Kolli > ( select MIN(f.ColumnHoejde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Max_Height_Kolli IS NOT NULL AND Max_Height_Kolli < ( select MAX(f.ColumnHoejde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Min_Width_Kolli IS NOT NULL AND Min_Width_Kolli > ( select MIN(f.ColumnBredde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Max_Width_Kolli IS NOT NULL AND Max_Width_Kolli < ( select MAX(f.ColumnBredde) from @TableFreight f);
DELETE FROM #KolliTable WHERE Min_Weight_Kolli IS NOT NULL AND Min_Weight_Kolli > ( select MIN(f.ColumnVaegt) from @TableFreight f);
DELETE FROM #KolliTable WHERE Max_Weight_Kolli IS NOT NULL AND Max_Weight_Kolli < ( select MAX(f.ColumnVaegt) from @TableFreight f);
SELECT fp.FreightProductText, k.*
FROM #KolliTable k
LEFT JOIN dan_FreightProduct fp on k.F_FreightProductID = fp.FreightProductID
DROP TABLE #KolliTable
END
END
GO