18

I need to know if I can do this in an UPDATE statement:

UPDATE users SET ('field1', 'field2', 'field3') 
VALUES ('value1', 'value2', 'value3');

Or similar syntax. I'm using SQLite.

Note:

Nobody understands me, I just want to know if it is possible to SET separate fields to separate values. That's all.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
calbertts
  • 1,513
  • 2
  • 15
  • 34

6 Answers6

33

There is a (standard SQL) syntax that is similar to what you propose but as far as I know, only Postgres has implemented it:

UPDATE users
SET  (field1, field2, field3)
   = ('value1', 'value2', 'value3') 
WHERE some_condition ;

Tested (for the infidels) in: SQL-Fiddle


This also works in Postgres:

UPDATE users AS u
SET 
   (field1, field2, field3)
 = (f1, f2, f3)
FROM
  ( VALUES ('value1', 'value2', 'value3')
  ) AS  x (f1, f2, f3)
WHERE condition ;

This works in Postgres and SQL-Server:

UPDATE users 
SET 
   field1 = f1, field2 = f2, field3 = f3
FROM
  ( VALUES ('value1', 'value2', 'value3')
  ) AS  x (f1, f2, f3)
WHERE condition ;

and as @JackDouglas commented, this works in Oracle:

UPDATE users
SET  (field1, field2, field3)
   = ( SELECT 'value1', 'value2', 'value3' FROM dual ) 
WHERE condition ;
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
23

What you posted is not the correct syntax for UPDATE. To UPDATE your syntax is:

UPDATE users 
SET field1 = 'value1',
    field2 = 'value2',
    field3 =  'value3';

You will want to add a WHERE clause or this will UPDATE all records.

Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 1
    Did you read the question? I'm actually asking if there's a syntax like that because I know what's the correct one. – calbertts Mar 02 '17 at 10:32
10

If you want to update a record, it should look like this. You should very rarely want to update without a WHERE condition.

UPDATE users
SET field1='value1',
    field2='value2',
    field3='value3'
WHERE field1=1

SQL Fiddle

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
8

Try

UPDATE users SET field1='value1', field2='value2'

etc...

You can find the official docs here.

W3Schools also has a useful page:

SQL UPDATE Statement

The UPDATE statement is used to update records in a table.

SQL UPDATE Syntax

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

EDIT: since you seem to need to create a string, the normal method to do so safely is to use prepared statements.

Assuming Java, we already have this example on StackOverflow:

SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("SELECT * FROM Country WHERE code = ?");
stmt.bindString(1, "US");
stmt.execute();

In your case,

SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("UPDATE users SET field1=?, field2=?...");
stmt.bindString(1, "value1");
stmt.bindString(2, "value2");
stmt.execute();
Community
  • 1
  • 1
Sklivvz
  • 30,601
  • 24
  • 116
  • 172
  • No, I need create a string and this sintax isn't useful to me. :( – calbertts Nov 20 '12 at 21:06
  • 1
    @calbertts if you need to "create a string" then you need to rephrase/rethink your question. as it stands, the three answers here all satisfy your question. – swasheck Nov 20 '12 at 21:10
  • 2
    NO! nobody understands me, I just want to know if is possible separate fields of the values.. thats all.. – calbertts Nov 20 '12 at 21:15
2

No, there is no such syntax in sqlite (postgress seems to have such thing implemented) - but better use:

UPDATE users SET firstname = 'Joe', lastname = 'value2', age = 50 WHERE id=12;
ingobaab
  • 87
  • 4
0

This feature is supported starting from version 3.15.0:

UPDATE

An assignment in the SET clause can be a parenthesized list of column names on the left and a row value of the same size on the right.

Syntax is as follows:

UPDATE users 
SET ("field1", "field2", "field3") = ('value1', 'value2', 'value3')
-- WHERE ...;

db<>fiddle demo

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275