Questions tagged [sql-update]

An SQL UPDATE statement is used to change existing rows in a table.

An SQL UPDATE statement is used to change existing rows in a table.

The basic syntax is:

UPDATE table 
SET 
    Column1 = 'x',
    Column2 = 'y'
WHERE id=1

Tagging Recommendation

This tag should be used for general SQL programming language questions, in addition to tags for specific products. For example, questions about Microsoft SQL Server should use the tag, while questions regarding MySQL should use the tag. Tagging by product (including version, e.g , ) is the easiest way to know what functionality is available for the task at hand.

References

12009 questions
23
votes
4 answers

EntityFramework, Insert if not exist, otherwise update

I'm having a Entity-Set Countries, reflecting a database table '<'char(2),char(3),nvarchar(50> in my database. Im having a parser that returns a Country[] array of parsed countries, and is having issues with getting it updated in the right way. What…
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
23
votes
1 answer

Using jsonb_set() for updating specific jsonb array value

Currently I am working with PostgreSQL 9.5 and try to update a value inside an array of a jsonb field. But I am unable to get the index of the selected value My table just looks like this: CREATE TABLE samples ( id serial, sample jsonb …
Daniel Seichter
  • 809
  • 1
  • 8
  • 14
23
votes
11 answers

mySQL UPDATE query returns "0 rows affected"

I have this query: UPDATE phonecalls SET Called = "Yes" WHERE PhoneNumber = "999 29-4655" My table is phonecalls, I have a column named PhoneNumber. All I want to update is a column named Called to "yes". Any idea what I am doing wrong? …
user222427
22
votes
6 answers

MySQL UPDATE and SELECT in one pass

I have a MySQL table of tasks to perform, each row having parameters for a single task. There are many worker apps (possibly on different machines), performing tasks in a loop. The apps access the database using MySQL's native C APIs. In order to…
Paul Oyster
  • 1,228
  • 6
  • 16
  • 19
22
votes
1 answer

UPDATE-no-op in SQL MERGE statement

I have a table with some persistent data in it. Now when I query it, I also have a pretty complex CTE which computes the values required for the result and I need to insert missing rows into the persistent table. In the end I want to select the…
Lucero
  • 59,176
  • 9
  • 122
  • 152
22
votes
2 answers

Postgres INSERT ON CONFLICT DO UPDATE vs INSERT or UPDATE

I have stock_price_alert table with 3 columns. stock_price_id is PRIMARY KEY & also FOREIGN KEY to other table. Table definition as below: create table stock_price_alert ( stock_price_id integer references stock_price (id) on delete cascade not…
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
22
votes
2 answers

MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields

From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE statement to specify behavior when values inserted (with INSERT or SET or VALUES) are already in destination table w.r.t. PRIMARY KEY or some UNIQUE field. If value for PRIMARY…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
22
votes
3 answers

How to change VARCHAR type to DATETIME using ALTER in MySQL?

How can I change VARCHAR() type to DATETIME using ALTER in MySQL?
aizaz
  • 3,056
  • 9
  • 25
  • 57
22
votes
3 answers

Mysql multiplication query

I have a table called products containing a field called price and I simply want to double the price on every product. Could you give me a hand with an SQL statement I can run within PHP myAdmin please.
Mark
  • 833
  • 3
  • 10
  • 21
21
votes
10 answers

How can I rollback an UPDATE query in SQL server 2005?

How can I rollback an UPDATE query in SQL server 2005? I need to do this in SQL, not through code.
Guddu
  • 627
  • 3
  • 13
  • 28
21
votes
1 answer

Changing a value in SQLite3

I'll start off by showing the code: create table products ('name' text primary key, 'price' INTEGER) insert into table products ('name', 'price') values ('coke', 8) insert into table products ('name', 'price') values ('sprite', 9) What would be the…
james
  • 919
  • 4
  • 11
  • 17
21
votes
2 answers

Return updated row attributes on UPDATE

My query is as follow: UPDATE t1 SET t1.foreign_key = (SELECT id FROM t2 WHERE t2.col = %s ) WHERE t1.col = %s How do I return some attributes of the updated row in the table in the same query?
hangc
  • 4,730
  • 10
  • 33
  • 66
21
votes
8 answers

T-SQL conditional UPDATE (v2)

I have a table: Message (MessageID int, Subject nvarchar(100), Body nvarchar(max)) After a message is being updated on UI, I call a stored proc to update that table. In some cases user might update just subject, in other cases just body. I want…
Andrey
  • 20,487
  • 26
  • 108
  • 176
20
votes
6 answers

SQL UPDATE statement to switch two values in two rows

I'm using SQL Server to swap two values in two rows. Let me show: [ord] [name] 1 John 4 Jack 7 Pete 9 Steve 11 Mary Say, I need to swap [ord] numbers for "Pete" and "Steve" to make this table to be like so: [ord] [name] 1 …
ahmd0
  • 16,633
  • 33
  • 137
  • 233
20
votes
2 answers

How to update from select with a Join

How can I update a table that is also present in a subquery? Do I have to do it in 2 stages? (create a temporary table - put the selected data in it and then update the final table) I am trying to update the invoiceLine table with the label of the…
Manse
  • 37,765
  • 10
  • 83
  • 108