简体   繁体   中英

Get the value from a variable of a table and INSERT into the new table with extra variable for the new table not working?

I am testing a SQL Query for a VB.NET project that INSERT the details of food into a table for the Add to Cart feature after user select the quantity of the food.

The query will get the food details such as food name and food price from a table called Food and it will need to add all the food details into the table called Cart together with quantity and subtotal.

I wanted to get the value from Food table and some quantity that input by user and INSERT into the Cart table.

However, there's an issue that the values that I wanted to add are displaying invalid column name when I test the query in SQL Query compiler.

Here's the error

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Food.Id" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Food.foodName" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Food.price" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Food.price" could not be bound.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'qty'.

Here's my SQL Query command. Note:The integer and the math calculation is just for testing, I will change to parameterized query in the program.

SELECT * FROM Cart

INSERT INTO Cart (foodID, foodName, foodPrice, [qty], total) 
VALUES (Food.Id, Food.foodName, Food.price, 2, Food.price * qty)
SELECT Id, foodName, price
FROM Food
WHERE foodName = 'BREAKFAST'

SELECT * FROM Cart

Here's the table that I created

CREATE TABLE [dbo].[Cart] (
    [Id]        INT             IDENTITY (100000, 1) NOT NULL,
    [foodID]    INT             NOT NULL,
    [foodName]  VARCHAR(50)     NOT NULL,
    [foodPrice] DECIMAL (18, 2) NOT NULL,
    [qty]       INT             NOT NULL,
    [total]     DECIMAL (18, 2) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    FOREIGN KEY ([foodID]) REFERENCES [dbo].[Food] ([Id]),
);

CREATE TABLE [dbo].[Food] (
    [Id]       INT             IDENTITY (100, 1) NOT NULL,
    [foodName] VARCHAR (50)    NOT NULL,
    [price]    DECIMAL (18, 2) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

I pretty sure the problem is the variable I put in the VALUE is not correct, I am not sure what is the correct way to put the variable from another table into the VALUE ? Sorry for the question because I am very new to SQL Database.

You seem to want something like:

INSERT INTO Cart (foodID, foodName, foodPrice, [qty], total) 
    SELECT f.Id, f.foodName, f.price, 2, f.price * qty
    FROM Food f
    WHERE f.foodName = 'BREAKFAST'

I don't know where qty comes from. Presumably, you pass that in as a parameter of some sort.

Also, your data model is not normalized and that is a bad thing. You should just include foodID in the cart and look up the rest of the values using JOIN .

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