茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1669405 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00157.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼]Update rows in SQL that retain the old value

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Update rows in SQL that retain the old value
Update rows in SQL that retain the old value [closed]

My table in SQL
CREATE TABLE Person(
  Person_id INT PRIMARYKEY
  Person_name NVARCHAR(250)
)

Data in table:
INSERT INTO Person VALUES(1,'Peter')

I want update row in SQL that retain the old value.
UPDATE Person SET Person_name = 'Mary' WHERE Person_id = 1

This command does not work. I want after update column Person_name is value 'Peter Mary'



You need to concatenate the literal value Mary with the value of the column Person_name using your RDBMS concatenation operator. But, you didn't specify what RDNMS you are using.

Here is the standard way:
UPDATE Person
SET Person_name = Person_name || ' ' || 'Mary'
WHERE Person_id = 1;

For SQL Server use the + operator like so:
UPDATE Person
SET Person_name = Person_name + ' ' + 'Mary'
WHERE Person_id = 1;




Is it this you want to archieve?

Before the update:
ID | Person_name
---+---------------
1  | Peter

After the update:
ID | Person_name
---+---------------
1  | Peter Mary

If you're for example using MySQL, you could use this approach:
UPDATE Person
SET Person_name = CONCAT(Person_name, ' ', 'Mary')
WHERE Person_id = 1

Just CONCAT a blank and the new value to the current value.
Please don't forget to add the RDBMS you're using. Other RDBMS might have different syntaxes and possibilities.

原文出處:Update rows in SQL that retain the old value - Stack Overflow
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|