4

有两个表

table stud
id   name  marks1 marks 2
1     X     3      2
2     y     4      2
3     z     5      2

第二表评论

comment

使用给定格式的 stud 行更新注释列 预期结果

表注释

 comment
 1,X,3,2#2,y,4,2#3,z,5,2
4

1 回答 1

4

SQL小提琴

MS SQL Server 2008 架构设置

create table stud
(
  id int,
  name varchar(10),
  marks1 int,
  marks2 int
)

create table comment
(
  comment varchar(max)
)

insert into stud values
(1,     'X',     3,      2),
(2,     'y',     4,      2),
(3,     'z',     5,      2)

查询 1

insert into comment(comment)
select
  (
  select '#'+cast(id as varchar(10))+','+
             name+','+
             cast(marks1 as varchar(10))+','+
             cast(marks2 as varchar(10))
  from stud
  for xml path(''), type
  ).value('substring((./text())[1], 2)', 'varchar(max)')

结果

查询 2

select *
from comment

结果

|                 COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
于 2012-11-10T16:25:27.317 回答