简体   繁体   中英

Getting invalid column name when creating a procedure using multiple if statements

If someone could point me in the right direction. I have a homework problem where I need to create a stored procedure that takes in a product name and will check how much a customer is spending. Depending on how much is spent a discount increase will be added to the existing discount.

I've been referencing the videos and SQL scripts provided by the teacher. I've also checked w3schools, sqlshack, and a few other websites to finish the project and I've reached a point where I'm stuck. Any of the column names in the if statement gives me an error message of "Invalid Column Name". If I add the table name to the column, I get a different error message of "The multi-part identifier could not be bound". I have moved the inner joins to inside the if statements and didn't change anything.

    
Use Northwind;

go

CREATE PROCEDURE CategoryDiscount
 @categoryname varchar(50)

 AS

Select [Order Details].UnitPrice, Quantity from [Order Details] 
INNER JOIN Products on [Order Details].ProductID= Products.ProductID
INNER JOIN Categories on Categories.CategoryID = [Order Details].ProductID

If (UnitPrice * Quantity) < 50
    begin
        update [Order Details] 
        set Discount = Discount + .05
        Where CategoryName = '@categoryname';
    end
ELSE IF (UnitPrice * Quantity) between 50 and 100
    begin
        update [Order Details] 
        set Discount = Discount + .10
        Where CategoryName = '@categoryname';
    end
ELSE IF (UnitPrice * Quantity) > 100
    begin
        update [Order Details] 
        set Discount = Discount + .15
        Where CategoryName = '@categoryname';
    end
Else
    begin
        print 'Incorrect'
    end;

You should store values UnitPrice and Quantity in the variables, those could be @UnitPrice and @Quantity and it will be in the select statement which comes in the start of your stored procedure. Then use those variables in your if statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM