0

I am looking to insert some values into a column based on the selection of a specific row. I have a table with columns a, b, c, and d. I want to insert the values 1, 2, and 3 into columns b, c, and d when column a = X. I cannot find how to do this.

Toad for oracle is my platform and I am looking for SQL code.

2
  • 1
    You want to 'insert' ? Your code doesn't work doing an "update... Where a = x" ? Commented Apr 13, 2015 at 16:08
  • You can also give condition to insert queries Commented Apr 13, 2015 at 16:10

3 Answers 3

1

You can either update them one at a time:

update mytable set b = 1 where a = X;
update mytable set c = 2 where a = X;
update mytable set d = 3 where a = X;

Or update them all in one go:

update mytable set b = 1,c = 2,d = 3 where a = X;

Alternatively, assuming 'a' is a primary key column or unique index and there is only 1 row where a = X, if you only have 4 columns and you want to update 3 of them you could delete your row and re-insert the whole lot:

delete from mytable where a = X;
insert into mytable values(X, 1, 2, 3);
Sign up to request clarification or add additional context in comments.

3 Comments

My brain stopped working. Forgot about updates!! Thanks!
Second way is a wrong way to do that! Consider you have many rows 'WHERE a = x', you delete them all, and then you add only one row. You should update them all.
@WhiteHat that's a good point, I made an assumption that 'a' column was the primary key, I have edited the answer to clarify
1
update mytable set b = 1,c = 2,d = 3 where a = X;

Comments

1

You can use INSERT INTO...SELECT and give condition to your insert queries such as:

INSERT
INTO table_name (b, c, d)
VALUES (bValue, cValue, dValue)
/* Select Condition */
WHERE a=1

Comments

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.