更新 sql 语法 - 通过串联将字段重置为自身 - SQLServer 2005
我使用此语法时遇到错误:
update table set field1 = (field1+' - '+field2) where field1 = 'somevalue'
它对为我这样做不太满意。 我知道连接的“+”在我的 select 语句中有效,所以这是正确的语法。 这里还有其他东西在起作用......我也尝试删除括号。
示例:
如果 field1 = 'Cheese' 且 field2 = 'ConQueso'
那么我的更新应将 field1 = 'Cheese' 的所有记录设置为 field1 = 'Cheese - ConQueso'
EDIT:
Both fields are text fields
I'm getting and error using this syntax:
update table set field1 = (field1+' - '+field2) where field1 = 'somevalue'
It's not too happy with doing this for me. I know that the '+' for concatenation works in my select statements, so that is the right syntax. There is something else at play here... and I tried removing the parenthesis too.
Example:
if field1 = 'Cheese' and field2 = 'ConQueso'
then my update should set all records where field1 = 'Cheese' to field1 = 'Cheese - ConQueso'
EDIT:
Both fields are text fields
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不提供错误,很难判断,但组合数据大小可能超过 field1。
例如,如果
field1
为 varchar(50),field2
为 varchar(50),则总计最多可达 103 个字符,包括' - ',超过了field1的50个字符。
Hard to tell without you providing the error, but perhaps the combined data size exceeds field1.
E.g., if
field1
is varchar(50) andfield2
is varchar(50), the combined total could be up to 103 characters including your' - '
, which exceeds the 50 characters of field1.(编辑:更新之前澄清数据类型是
text
;但作为varchar(max)
工作正常)在这里工作正常(SQL2005):
输出:
(edit: pre-dates the update clarifying data-type is
text
; but works fine asvarchar(max)
)Works fine here (SQL2005):
outputs: