首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

~请问一个高难度SQL语句有关问题,各个大哥帮帮忙吧

2012-03-12 
在线等~~~~~请教一个高难度SQL语句问题,各个大哥帮帮忙吧create table test(idint identity(1,1),name var

在线等~~~~~请教一个高难度SQL语句问题,各个大哥帮帮忙吧
create table test
(
idint identity(1,1),
name varchar(10),
)
go
insert into test values('A')
insert into test values('B')
insert into test values('C')


我想通过一条SQL语句,查询TEST表,想得到如下字符串:“A,B,C” 。请问如何实现?

[解决办法]

SQL code
select stuff((select ','+name from test for xml path('')),1,1,'')
[解决办法]
declare @V varchar(8000)
set @V = ''
select @V = @V + ',' + name from test where id in (1,2,3);
set @V = substring(@V,len(@V) -1);
select @V
[解决办法]
SQL code
create table test(id int identity(1,1),name varchar(10),)goinsert into test values('A')insert into test values('B')insert into test values('C')godeclare @str varchar(1000)select @str = isnull(@str+',','')+name from testprint @strdrop table test/*****************A,B,C
[解决办法]
SQL code
create table test(id int identity(1,1),name varchar(10),)insert into test values('A')insert into test values('B')insert into test values('C')select stuff((select ','+name from test for xml path('')),1,1,'') namename--------------------A,B,C(1 row(s) affected)
[解决办法]
MS-SQL SERVER单列合并的四种常用方法--【叶子】
[解决办法]
方法2,
SQL code
create table test(id int identity(1,1),name varchar(10),)insert into test values('A')insert into test values('B')insert into test values('C')declare @names varchar(100)=''select @names=@names+','+name from testselect stuff(@names,1,1,'') namename--------------------A,B,C(1 row(s) affected) 

热点排行
Bad Request.