declare @s varchar(8000) select @s=isnull(@s+',','')+col from tb select @s
if object_id('[tb]') is not null drop table [tb] go create table [tb] (col nvarchar(10)) insert into [tb] select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x' declare @s varchar(8000) select @s=isnull(@s+',','')+col from tb select @s /* a,b,c,b,c,d,e,f,sa,4t,x(1 個資料列受到影響) */
--> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] create table [tb]([code] varchar(9)) insert [tb] select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x'declare @sql varchar(8000) select @sql = '' select @sql = @sql+','+ [code] from [tb]select stuff(@sql,1,1,'') ---------------- a,b,c,b,c,d,e,f,sa,4t,x
--------------------------------------------- --> Author : js_szy --> Target : ---->★★★ --> Date : 2009-12-15 16:56:29 --> Version: SQL Server 2005 ---------------------------------------------
--> 测试数据: @tb declare @tb table (id varchar(22) ) insert into @tb select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x'
declare @s varchar(50)select @s=isnull(@s+',','')+id from @tbselect @s -------------------------------------------------- a,b,c,b,c,d,e,f,sa,4t,x
--> 测试数据:@tb declare @tb table([name] varchar(9)) insert @tb select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x'declare @ varchar(8000) select @=isnull(@+',','')+[name] from @tb select @ /* (3 行受影响)---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- a,b,c,b,c,d,e,f,sa,4t,x(1 行受影响) */
---测试数据 if object_id [tb] is not null drop table [tb] create table [tb](col varchar(10) insert into [tb] select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x'declare @s varchar(8000) select @s=isnull(@s+',','')+col from tb select @s /* a,b,c,b,c,d,e,f,sa,4t,x*/
---测试数据 if object_id [tb] is not null drop table [tb] SQL codecreate table [tb](col varchar(10) insert into [tb] select 'a,b,c' union all select 'b,c,d,e,f' union all select 'sa,4t,x'declare @s varchar(8000) select @s=isnull(@s+',','')+col from tb select @s /* a,b,c,b,c,d,e,f,sa,4t,x*/
select @s=isnull(@s+',','')+col from tb
select @s
go
create table [tb] (col nvarchar(10))
insert into [tb]
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'
declare @s varchar(8000)
select @s=isnull(@s+',','')+col from tb
select @s
/*
a,b,c,b,c,d,e,f,sa,4t,x(1 個資料列受到影響)
*/
declare @sql varchar(8000)
select @sql =isnull(@sql +',','')+字段名 from 表名
select @sql
if object_id('[tb]') is not null drop table [tb]
create table [tb]([code] varchar(9))
insert [tb]
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'declare @sql varchar(8000)
select @sql = ''
select @sql = @sql+','+ [code] from [tb]select stuff(@sql,1,1,'')
----------------
a,b,c,b,c,d,e,f,sa,4t,x
--> Author : js_szy
--> Target : ---->★★★
--> Date : 2009-12-15 16:56:29
--> Version: SQL Server 2005
---------------------------------------------
--> 测试数据: @tb
declare @tb table (id varchar(22) )
insert into @tb
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'
declare @s varchar(50)select @s=isnull(@s+',','')+id from @tbselect @s
--------------------------------------------------
a,b,c,b,c,d,e,f,sa,4t,x
declare @tb table([name] varchar(9))
insert @tb
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'declare @ varchar(8000)
select @=isnull(@+',','')+[name] from @tb
select @
/*
(3 行受影响)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
a,b,c,b,c,d,e,f,sa,4t,x(1 行受影响)
*/
if object_id [tb] is not null drop table [tb]
create table [tb](col varchar(10)
insert into [tb]
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'declare @s varchar(8000)
select @s=isnull(@s+',','')+col from tb
select @s
/*
a,b,c,b,c,d,e,f,sa,4t,x*/
if object_id [tb] is not null drop table [tb]
SQL codecreate table [tb](col varchar(10)
insert into [tb]
select 'a,b,c' union all
select 'b,c,d,e,f' union all
select 'sa,4t,x'declare @s varchar(8000)
select @s=isnull(@s+',','')+col from tb
select @s
/*
a,b,c,b,c,d,e,f,sa,4t,x*/