开发者

Update table column with data from other columns in same row

开发者 https://www.devze.com 2023-03-23 19:09 出处:网络
I have a table that has several columns of textual data. The goal is to concatenate those columns into a single different column in the same table and same row.

I have a table that has several columns of textual data. The goal is to concatenate those columns into a single different column in the same table and same row.

What is the SQL Server query syntax that would all开发者_C百科ow me to do this?


Something like this:

UPDATE myTable SET X = Y + Z


Do you absolutely have to duplicate your data? If one of the column values changes, you will have to update the concatenated value.

A computed column:

alter table dbo.MyTable add ConcatenatedColumn = ColumnA + ColumnB

Or a view:

create view dbo.MyView as
select ColumnA, ColumnB, ColumnA + ColumnB as 'ConcatenatedColumn'
from dbo.MyTable

Now you can update ColumnA or ColumnB, and ConcatenatedColumn will always be in sync. If that's the behaviour you need, of course.


Might be misunderstanding but:

Alter table myTable add combinedColumn Varchar(1000);

Update myTable set combinedColumn = textField1 + textField2;


select 
  textfield1 + textfield2 + ... + textfieldN as conc_text,
  otherfield1,
  otherfield2,
  ...
  otherfieldN
from 
  mytable
0

精彩评论

暂无评论...
验证码 换一张
取 消