加逗号 declare @str varchar(8000) set @str= '' select @str=@str+','+colName from tbl_emp set @str=stuff(@str,1,1,'') print @str
declare @tbl table(name nchar(2), id int) insert into @tbl select '车务', 1 union all select '机务', 2 union all select '供电', 3 union all select '供电', 4 union all select '车辆', 5 union all select '车务', 6 declare @ret varchar(8000) set @ret = '' select @ret = @ret + ',' + name from @tbl set @ret = stuff(@ret,1,1,'') select @ret--车务,机务,供电,供电,车辆,车务
--> 测试数据: [s] if object_id('[s]') is not null drop table [s] create table [s] (name varchar(4),id int) insert into [s] select '车务',1 union all select '机务',2 union all select '供电',3 union all select '供电',4 union all select '车辆',5 union all select '车务',6declare @sql varchar(1000) select @sql=isnull(@sql+',','')+name from [s] select @sql
set @str= ''
select @str=@str+namefrom table
print @str
加逗号
declare @str varchar(8000)
set @str= ''
select @str=@str+','+colName from tbl_emp
set @str=stuff(@str,1,1,'')
print @str
insert into @tbl
select '车务', 1 union all
select '机务', 2 union all
select '供电', 3 union all
select '供电', 4 union all
select '车辆', 5 union all
select '车务', 6 declare @ret varchar(8000)
set @ret = ''
select @ret = @ret + ',' + name from @tbl
set @ret = stuff(@ret,1,1,'')
select @ret--车务,机务,供电,供电,车辆,车务
if object_id('[s]') is not null drop table [s]
create table [s] (name varchar(4),id int)
insert into [s]
select '车务',1 union all
select '机务',2 union all
select '供电',3 union all
select '供电',4 union all
select '车辆',5 union all
select '车务',6declare @sql varchar(1000)
select @sql=isnull(@sql+',','')+name from [s]
select @sql