SQL update query

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

update tablename set field1= val1 where id=1

Now I want to update 2 fields like this: How can I do this?

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

+2


source to share


5 answers


Your syntax is almost correct, but you cannot use AND.



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

      

+18


source


Or, to be on the safe side, 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 that you will update.

+4


source


You almost had this:

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

      

+3


source


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

      

As others have said, but this is how I like to step back and format it, for more complex queries, formulating questions correctly!

+3


source


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

COMMIT;

+1


source







All Articles