rtvalluri rtvalluri - 1 year ago 64
SQL Question

inserting using where clause in SQL table dynamically

I am having a table

of 3 columns (
) among which I am writing
columns initially and I want to fill the column 'quantity' using some expression(ex:
) involving variables from another table
SHEET(d1 int,d2 int ,d3 int,d4 int ,d5 int,d6 int)

Here I need to write values to quantity column based on value in Sno column from COMPONENTS table(components.sno).

I used to keep expression value in 'x' and trying to insert into COMPONENTS as shown below:

insert into components(Quantity) values(x)
where components.sno='y'; [Y is inetger starting from 0 to 70]

But the above query showing error at

Please suggest me best SQL query to achieve this!
Thanks in Advance..!

Answer Source

You can't do INSERT with WHERE clause unless it's WHERE NOT EXISTS, so just do:

INSERT INTO components(Quantity) VALUES(x)

Maybe you needed to do UPDATE

UPDATE components SET Quantity=x WHERE components.sno='y';