使用update语句追加字符串

时间:2014-10-24 10:07:43

标签: sql sql-server-2008 tsql

假设我在数据库中有一个名为updateLog的字段,它包含如下数据:

Staff A 24/10/2014 11:00
Hi your order is being picked.

Staff A 24/10/2014 08:00
Your order is being sent to the picking team.

现在假设我想为我的客户添加新的更新?如何使用TSQL附加以下文本:

Staff A 24/10/2014 12:00
Order completed. Thankyou

我需要能够将此更新添加到我的字段顶部。它需要遵循我的更新惯例。

UPDATE CustUpdates
SET updateLog = 'Staff A 24/10/2014 12:00' + CHAR(13) + 'Order completed. Thankyou'
WHERE ORDERID = 'ORD001'

我尝试的问题是它会覆盖我的字段内容!

2 个答案:

答案 0 :(得分:1)

这应该有所帮助:

UPDATE CustUpdates
   SET updateLog = 'Staff A 24/10/2014 12:00' + CHAR(13) + 'Order completed. Thankyou' + CHAR(13) + updateLog
 WHERE ORDERID = 'ORD001'

答案 1 :(得分:1)

create table status(orderid int, updatelog nvarchar(500))

insert into status values(1, 'your previous message')

declare @message nvarchar(500) = 'Staff A 24/10/2014 12:00' + CHAR(13) + 
  'Order completed. Thankyou '

update status 
set updatelog = @message + updatelog
where orderid = 1


select * from status

http://sqlfiddle.com/#!3/562d4/2