34,838
社区成员




可以用
SELECT RTRIM(@str) AS Value
取代
select right(@str,len(@str)-1) as value
CREATE TABLE t1
(
id INT,
name VARCHAR(20),
pid INT
)
INSERT INTO t1
SELECT 1, '通信', 0 UNION all
SELECT 2, '通信_1', 1UNION all
SELECT 3, '通信_2', 1 UNION all
SELECT 4, '通信_3', 1 UNION all
SELECT 5, '汽车', 0 UNION all
SELECT 6, '汽车_1', 5 UNION all
SELECT 7, '移动', 0 UNION all
SELECT 8, '移动_1', 7
SELECT * FROM t1 WHERE id IN (2,1,4,7,8) ORDER BY id
-------------------------
id name pid
1 通信 0
2 通信_1 1
4 通信_3 1
7 移动 0
8 移动_1 7
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[name] varchar(6),[Pid] int)
insert [test]
select 1,'通信',0 union all
select 2,'通信_1',1 union all
select 3,'通信_2',1 union all
select 4,'通信_3',1 union all
select 5,'汽车',0 union all
select 6,'汽车_1',5 union all
select 7,'移动',0 union all
select 8,'移动_1',7
declare @str varchar(100)
set @str=''
select @str=@str+','+[name] from test
where id in(8,4,2,7,1)
select right(@str,len(@str)-1) as value
/*
value
---------------------------------------
通信,通信_1,通信_3,移动,移动_1
*/
结果一样的
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[name] varchar(6),[Pid] int)
insert [test]
select 1,'通信',0 union all
select 2,'通信_1',1 union all
select 3,'通信_2',1 union all
select 4,'通信_3',1 union all
select 5,'汽车',0 union all
select 6,'汽车_1',5 union all
select 7,'移动',0 union all
select 8,'移动_1',7
declare @str varchar(100)
set @str=''
select @str=@str+','+[name] from test
where id in(1,2,4,7,8)
select right(@str,len(@str)-1) as value
/*
value
---------------------------------------
通信,通信_1,通信_3,移动,移动_1
*/