D_C
D_C

Reputation: 390

Insert Values into table of specific row

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.

Upvotes: 0

Views: 5373

Answers (3)

Matt
Matt

Reputation: 3353

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);

Upvotes: 1

goGud
goGud

Reputation: 4333

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

Upvotes: 1

Sailesh Kotha
Sailesh Kotha

Reputation: 2099

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

Upvotes: 1

Related Questions