Search code examples
sqlinsertwhere-clause

insert using where clause in SQL table dynamically


I am having a table COMPONENTS of 3 columns (Sno,Component,Quantity) among which I am writing Sno and Component columns initially and I want to fill the column 'quantity' using some expression(ex: ((d1+d2)*d3) ) 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 where

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


Solution

  • 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';