user156073
user156073

Reputation: 2001

SQL update query

I want to update two fields in one sql query. How do I do that?

update tablename set field1= val1 where id=1

Now I want to update 2 fields as follows: How can I do that?

update tablename set field1 =val1 and set field2=val2 where id=1

Upvotes: 2

Views: 440

Answers (5)

Michael Todd
Michael Todd

Reputation: 17041

You almost had it:

update tablename 
set field1=val1,
field2=val2 
where id=1 

Upvotes: 3

Yoav
Yoav

Reputation: 170

Or to be safe, I like to write UPDATE statements like this:

UPDATE T
SET
    T.Field1 = Value1
    ,T.Field2 = Value2
-- SELECT *
FROM TableName AS T
WHERE T.ID = 1

This way you can be sure of what you'll be updating.

Upvotes: 4

Darknight
Darknight

Reputation: 2500

UPDATE  TableName
SET     Field1=Value1
       ,Field2=Value2
WHERE   id=id_value

Like the others, but this is how I like to indent and format it, on bigger complex queries, proper formating matters alot!

Upvotes: 3

DaveJohnston
DaveJohnston

Reputation: 10151

UPDATE tablename SET field1 = var1, field2 = var2 WHERE id = 1;

COMMIT;

Upvotes: 1

Matthew Jones
Matthew Jones

Reputation: 26190

Your syntax is almost correct, but you can't use AND.

UPDATE tablename SET field1=var1, field2=var2 WHERE id=1

Upvotes: 18

Related Questions