0

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..!

2 Answers 2

7

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';
Sign up to request clarification or add additional context in comments.

2 Comments

Did it help with your problem?
@RavitejA, if an answer solve your problem, please upvote it.
-1

Use an IF statement before the insert and don't use the where clause:

If <your condition>
  Begin <your statement>
End 

1 Comment

Hi, welcome to SO. To increase the chance of your response being accepted, and useful to future readers, I would suggest posting the entire text of the correct query, with code markdown (stackoverflow.com/editing-help#code).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.