create table T1
(
CityName nvarchar(50)
);
insert into T1 (CityName) values ('上海')
insert into T1 (CityName) values ('北京')
insert into T1 (CityName) values ('南京')
select * from T1
--想得到如下值,即一个字段显示(‘上海’,‘北京’,南京’)这样的信息
下面语句有问题,得不到结果。请大家帮看看,谢谢了
declare @column_name varchar(2000)
select @column_name = ''
select @column_name = @column_name + convert(varchar,CityName ) +',' from T1
5 个解决方案
#1
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+name FROM (SELECT DISTINCT CityName FROM T1)AS T
SELECT @column_name
#2
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+CityName FROM (SELECT DISTINCT CityName FROM T1)AS T
SELECT @column_name
#3
如果城市不重复的话
create table T1
(
CityName nvarchar(50)
);
insert into T1 (CityName) values ('上海')
insert into T1 (CityName) values ('北京')
insert into T1 (CityName) values ('南京')
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+CityName FROM T1
SELECT @column_name
DROP TABLE t1
GO
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上海,北京,南京
(1 行受影响)*/
#4
是这样吗:
--drop table t1
create table T1
(
CityName nvarchar(50)
);
insert into T1 (CityName) values ('上海')
insert into T1 (CityName) values ('北京')
insert into T1 (CityName) values ('南京')
select * from T1
declare @column_name varchar(2000)
select @column_name = ''
select @column_name = @column_name +',' +convert(varchar,CityName ) from T1
select stuff(@column_name,1,1,'')
/*
上海,北京,南京
*/
#5
DECLARE @T1 TABLE (
CityName nvarchar(50)
);
insert into @T1 (CityName) values ('上海')
insert into @T1 (CityName) values ('北京')
insert into @T1 (CityName) values ('南京')
SELECT STUFF(
(SELECT ','+cityname FROM @t1 FOR XML PATH('')),
1,
1,
'')
#1
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+name FROM (SELECT DISTINCT CityName FROM T1)AS T
SELECT @column_name
#2
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+CityName FROM (SELECT DISTINCT CityName FROM T1)AS T
SELECT @column_name
#3
如果城市不重复的话
create table T1
(
CityName nvarchar(50)
);
insert into T1 (CityName) values ('上海')
insert into T1 (CityName) values ('北京')
insert into T1 (CityName) values ('南京')
DECLARE @column_name VARCHAR(8000)
SELECT @column_name =ISNULL(@column_name +',','')+CityName FROM T1
SELECT @column_name
DROP TABLE t1
GO
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上海,北京,南京
(1 行受影响)*/
#4
是这样吗:
--drop table t1
create table T1
(
CityName nvarchar(50)
);
insert into T1 (CityName) values ('上海')
insert into T1 (CityName) values ('北京')
insert into T1 (CityName) values ('南京')
select * from T1
declare @column_name varchar(2000)
select @column_name = ''
select @column_name = @column_name +',' +convert(varchar,CityName ) from T1
select stuff(@column_name,1,1,'')
/*
上海,北京,南京
*/
#5
DECLARE @T1 TABLE (
CityName nvarchar(50)
);
insert into @T1 (CityName) values ('上海')
insert into @T1 (CityName) values ('北京')
insert into @T1 (CityName) values ('南京')
SELECT STUFF(
(SELECT ','+cityname FROM @t1 FOR XML PATH('')),
1,
1,
'')