0
Create Procedure spMatchQuantityAndPercentage
    @MatchInventoryQuantity nvarchar(10),
    @Percentage decimal (3,2)
AS
BEGIN
    BEGIN TRY
        INSERT INTO Sales.SpecialOffer(SpecialOfferID, Description, DiscountPct, Type, Category, 
            StartDate, EndDate, MinQty, MaxQty, ModifiedDate)
        VALUES('18', 'Discount on products with quantity greater than the
            matched inventories','0.50','Greater than match quantity', 'Customer', '2019-11-01 00:00:00', 
           '2019-11-02     00:00:00', '0', NULL, '2019-11-02 00:00:00');
        INSERT INTO Sales.SpecialOfferProduct (SpecialOfferID, ProductID) 
        SELECT DISTINCT 18, PP.ProductID 
        FROM Production.ProductInventory as PP
        Group By PP.ProductID
        Having SUM(Quantity) > @MatchInventoryQuantity
    END TRY
    BEGIN CATCH
        PRINT 'The error was handled' 
    END CATCH
END

I am trying to create a discount and assign all products with a total inventory quantity (product is not unique in inventory) greater then the match quantity to that special offer, making sure to assign the product only once.

How do I assign these parameters of percentage(discount) and specialofferID not as constants as used in the insert state and select statements above?

4
  • 1
    MySQL or SQL Server?
    – Dale K
    Dec 12, 2019 at 4:04
  • 1
    Please share sample data with expected results Dec 12, 2019 at 4:17
  • Please add your SpecialOffer table's structure. If SpecialOfferID is an IDENTITY, then your insert will fail (you'll have to omit this column) and you can use SCOPE_IDENTITY() to recover the new ID, if not then you will have to get somehow a non-existing value (assuming it's a PK) for the new record, store it in a variable and then you can use it again on the 2nd insert.
    – EzLo
    Dec 12, 2019 at 7:33
  • Why would a parameter named MatchInventoryQuantity use a string datatype and not a numeric one? You use it as such! And using DISTINCT with GROUP BY is pointless - the grouping will make all rows unique. Your error handling will also eat any error and prevent any consumer of this logic from knowing an error occurred - that is not useful error "handling" at all.
    – SMor
    Dec 12, 2019 at 13:23

1 Answer 1

0

SpecialOfferID should probably be passed into the stored procedure as a parameter. But then in your code, your SpecialOfferID is first hard-coded as a string '18' and then as an integer 18. I suppose let's assume it is an integer. We'll add the parameter to the stored procedure and reference it from within our insert statements.

In addition, your @Percentage parameter is never used, and I assume it should be referenced from within the first insert statement as well.

Create Procedure spMatchQuantityAndPercentage
    @MatchInventoryQuantity nvarchar(10),
    @Percentage decimal (3,2),
    @SpecialOfferID nvarchar(max)
AS
BEGIN
    BEGIN TRY
        INSERT INTO Sales.SpecialOffer(SpecialOfferID, Description, DiscountPct, Type, Category, 
            StartDate, EndDate, MinQty, MaxQty, ModifiedDate)
        VALUES(@SpecialOfferID, 'Discount on products with quantity greater than the
            matched inventories',@Percentage,'Greater than match quantity', 'Customer', '2019-11-01 00:00:00', 
        '2019-11-02     00:00:00', '0', NULL, '2019-11-02 00:00:00');
        INSERT INTO Sales.SpecialOfferProduct (SpecialOfferID, ProductID) 
        SELECT DISTINCT @SpecialOfferID, PP.ProductID 
        FROM Production.ProductInventory as PP
        Group By PP.ProductID
        Having SUM(Quantity) > @MatchInventoryQuantity
    END TRY
    BEGIN CATCH
        PRINT 'The error was handled' 
    END CATCH
END
4
  • Although, honestly, we could have quite a long discussion about your style and some of the assumptions you are making in your code. But, I hope this answers your question. Dec 12, 2019 at 15:31
  • @Robbpriestly. I can see where I got it mixed up. Thank you for opening my eyes to it. Is there any way I can use autoincrement on my specialOfferID so that I don't need to supply it each time I am assigning products for specialOffer? Dec 12, 2019 at 23:51
  • Well sure you could auto increment SpecialOfferID but you'd have to so that in the SpecialOffer table. That's a different question, so post the code and ask it if you want help with it. If this answered your question I'd appreciate if you marked it answered or upvoted it. Dec 13, 2019 at 0:13
  • This comment answered my question. It's helpful to be on this platform. Dec 14, 2019 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.