Lawraoke
Lawraoke

Reputation: 556

SQL Insert values INTO specific column where ID equals to

I would like to perform an INSERT data into a table with specific table.

The example as follows:

Table A col1(PK), col2, col3 these three col already have value on it, lets say:

col1 col2 col3
 q    w     e
 z    x     c

currently I am adding a new column 4 to it

ALTER TABLE A
ADD col4 int;

Now I want to add value to Table A col4, how do I perform such action? for example I am adding 1 to row q and 2 to row z


UPDATE

As I try below for updating one row at a time, the query will be as follows:

UPDATE Table A set col4 = '1' where col1 = 'q'

the above query shall update the table and result looks like follows:

col1 col2 col3 col4
 q    w     e    1
 z    x     c

Or you guys can refer the answer from GordonLinoff, link here.

Upvotes: 0

Views: 2665

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269483

You would use update:

update a
    set col4 = (case when col1 = 'q' then 1 else 2 end)
    where col1 in ('q', 'z');

Upvotes: 1

Related Questions