1

I am using SQL server 2008. I have a table in which the first three columns of each row remain FIXED while the other columns change. R = Row, C = Col.

   C1   C2   C3   C4   C5....
R1:a    b    x    h    l 
R2:p    d    b    r    v 
R3:y    h    d    b    m
.....

The data in columns C1, C2 and C3 never changes. But data in C4, C5 needs to be changed sometimes.

What is the SQL query to update only cols C4, C5 etc. for a PARTICULAR row ?

2 Answers 2

6

Since you have mentioned that c1, c2, and c3 are unchangeable then make it basis for seacrhing a specific record,

UPDATE  tableName
SET     c4 = 'newVal',
        c5 = 'newvla2'
WHERE   c1 = 'val1' AND
        c2 = 'val2' AND
        c3 = 'val3'
Sign up to request clarification or add additional context in comments.

2 Comments

Don't we have something like this - INSERT INTO (DESIRED COLUMN NAMES) VALUES (SOME VALUES) WHERE C1 = 'val1', C2 = 'val2' ?
INSERT statements cannot have a WHERE clause unless you are executing INSERT INTO...SELECT which can have where clause. The statment you need is an UPDATE which modifies an existing row.
0

This is how the general format looks -

Update <<Table Name>> 
set <<ColumnName1>> = <<ColumnValue1>> , 
<<ColumnName2>> = <<ColumnValue2>> Where 
<<Primary Key Column>> = <<Primary Key Value>>

Here Table Name = Your Table Name

Column Name = Your Specific Column Name
Column Value = Your Specific Column Value
Primary Key Column = Your table primary key
Primary Key Value = Value of your table primary key

Hope this helps you.

3 Comments

I don't think that the term primary key is correct here. You can have whatever you want in the where clause, even a tautology.
Yes, we should write unique key combination for a row as the question indicates a particular row. I have written primary key as most of the tables in Databases consists Primary key.
I understand that you want to explain this specific use of UPDATE.

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.