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
144
votes
5 answers

How to prepend a string to a column value in MySQL?

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value. For example, if the existing value is "try" it should become "testtry".
santanu
  • 5,217
  • 7
  • 26
  • 17
144
votes
3 answers

Update or Insert (multiple rows and columns) from subquery in PostgreSQL

I'm trying to do something like this in postgres: UPDATE table1 SET (col1, col2) = (SELECT col2, col3 FROM othertable WHERE othertable.col1 = 123); INSERT INTO table1 (col1, col2) VALUES (SELECT col1, col2 FROM othertable) But point 1 is not…
dforce
  • 2,136
  • 3
  • 20
  • 36
135
votes
6 answers

Update multiple rows in Entity Framework from a list of ids

I am trying to create a query for Entity Framework that will allow me to take a list of ids and update a field associated with them. Example in SQL: UPDATE Friends SET msgSentBy = '1234' WHERE id IN (1, 2, 3, 4) How do I convert the above into…
allencoded
  • 7,015
  • 17
  • 72
  • 126
132
votes
6 answers

T-SQL: Using a CASE in an UPDATE statement to update certain columns depending on a condition

I am wondering if this is possible at all. I want to update column x if a condition is true, otherwise column y would be updated UPDATE table SET (CASE (CONDITION) WHEN TRUE THEN columnx ELSE columny END) = 25 I…
pqsk
  • 2,124
  • 3
  • 23
  • 28
113
votes
3 answers

MySql - Way to update portion of a string?

I'm looking for a way to update just a portion of a string via MySQL query. For example, if I have 10 records all containing string as part of the field value, is there a way to change string to anothervalue for each row via one query? I.e. for the…
n00b0101
  • 6,863
  • 17
  • 42
  • 36
105
votes
10 answers

MySQL - UPDATE query with LIMIT

I want to update rows in my table with starting from 1001 to next 1000. I tried with following query: UPDATE `oltp_db`.`users` SET p_id = 3 LIMIT 1001, 1000 This is giving me syntax error. Is this correct? am I doing any mistake here. Can we limit…
Rahul Shelke
  • 2,052
  • 4
  • 28
  • 50
103
votes
5 answers

Update query using Subquery in Sql Server

I have a simple table Structure like this: Table tempData ╔══════════╦═══════╗ ║ NAME ║ MARKS ║ ╠══════════╬═══════╣ ║ Narendra ║ 80 ║ ║ Ravi ║ 85 ║ ║ Sanjay ║ 90 ║ ╚══════════╩═══════╝ And I also have another table names as…
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
95
votes
5 answers

Get count of records affected by INSERT or UPDATE in PostgreSQL

My database driver for PostgreSQL 8/9 does not return a count of records affected when executing INSERT or UPDATE. PostgreSQL offers the non-standard syntax "RETURNING" which seems like a good workaround. But what might be the syntax? The example…
Un Homme
  • 1,021
  • 1
  • 8
  • 5
94
votes
2 answers

How do I make an UPDATE while joining tables on SQLite?

I tried : UPDATE closure JOIN item ON ( item_id = id ) SET checked = 0 WHERE ancestor_id = 1 And: UPDATE closure, item SET checked = 0 WHERE ancestor_id = 1 AND item_id = id Both works with MySQL, but those give me a syntax error in…
Bite code
  • 578,959
  • 113
  • 301
  • 329
94
votes
7 answers

mysql update multiple columns with same now()

I need to update 2 datetime columns, and I need them to be exactly the same, using mysql version 4.1.20. I'm using this query: mysql> update table set last_update=now(), last_monitor=now() where id=1; It is safe or there is a chance that the…
Radu Maris
  • 5,648
  • 4
  • 39
  • 54
90
votes
6 answers

MySQL: update a field only if condition is met

Is it possible to do an UPDATE query in MySQL which updates field value only if certain condition is met? Something like this: UPDATE test SET CASE WHEN true THEN field = 1 END WHERE id = 123 In other words: UPDATE…
Caballero
  • 11,546
  • 22
  • 103
  • 163
89
votes
1 answer

Updating integer column with null values in postgres

I would like to update my column with other column in other table. Before doing so, I would like to nullify my column(integer) first. However, below code did not work. (column_a: bigint; column_b: text) UPDATE table1 SET column_a IS NULL WHERE…
no_name
  • 1,083
  • 1
  • 8
  • 12
87
votes
6 answers

Bulk/batch update/upsert in PostgreSQL

I'm writing a Django-ORM enchancement that attempts to cache models and postpone model saving until the end of the transaction. It's all almost done, however I came across an unexpected difficulty in SQL syntax. I'm not much of a DBA, but from what…
julx
  • 8,694
  • 6
  • 47
  • 86
85
votes
9 answers

How do I do large non-blocking updates in PostgreSQL?

I want to do a large update on a table in PostgreSQL, but I don't need the transactional integrity to be maintained across the entire operation, because I know that the column I'm changing is not going to be written to or read during the update. I…
S D
77
votes
4 answers

Return pre-UPDATE column values using SQL only

I posted a related question, but this is another part of my puzzle. I would like to get the OLD value of a column from a row that was UPDATEd - WITHOUT using triggers (nor stored procedures, nor any other extra, non -SQL/-query entities). I have a…
pythonlarry
  • 2,316
  • 2
  • 20
  • 17