Sql server 2008 如何在sql server中更新以逗号分隔的多行列
这是两张桌子Sql server 2008 如何在sql server中更新以逗号分隔的多行列,sql-server-2008,Sql Server 2008,这是两张桌子 table stud id name marks1 marks 2 1 X 3 2 2 y 4 2 3 z 5 2 第二表评论 comment comment 1,X,3,2#2,y,4,2#3,z,5,2 使用给定格式的螺柱行更新注释列 预期结果 表注释 comment comment 1,X,3,2#2,y,4,2#3,z,5,2 MS SQL Server 2008架构设置
table stud
id name marks1 marks 2
1 X 3 2
2 y 4 2
3 z 5 2
第二表评论
comment
comment
1,X,3,2#2,y,4,2#3,z,5,2
使用给定格式的螺柱行更新注释列
预期结果
表注释
comment
comment
1,X,3,2#2,y,4,2#3,z,5,2
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)
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)')
select *
from comment
| COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
查询1:
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)
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)')
select *
from comment
| COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
:
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)
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)')
select *
from comment
| COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
查询2:
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)
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)')
select *
from comment
| COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
:
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)
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)')
select *
from comment
| COMMENT |
---------------------------
| 1,X,3,2#2,y,4,2#3,z,5,2 |
你自己做过什么努力吗?如果是这样,请公布你所拥有的,以及为什么/如何出错。