--> 测试数据: [stuinfo]
if object_id('[stuinfo]') is not null drop table [stuinfo]
create table [stuinfo] (sId int,sName varchar(4))
insert into [stuinfo]
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五' union all
select 4,'赵六' union all
select 5,'张军' union all
select 6,'李佳'declare @s varchar(8000) set @s=''
select @s=@s+sName+',' from [stuinfo]
if len(@s)>0 set @s=left(@s,len(@s)-1)
print @s/*
张三,李四,王五,赵六,张军,李佳
*/

解决方案 »

  1.   

    declare @sql nvarchar(200)
    select @sql=isnull(@sql+',','')+sName from stuInfo order by sId
    select @sql
      

  2.   

    第一条记录的名字:
    '张三',
    ------------------------第2条------------------------------
    没加之前的名字'张三'
    临时的名字叫李四
    sql语句为:select top 1 sname from stuinfo where sname not in('张三')
    加过后的名字'张三','李四',
    ------------------------第3条------------------------------
    没加之前的名字'张三','李四'
    临时的名字叫王五
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四')
    加过后的名字'张三','李四','王五',
    ------------------------第4条------------------------------
    没加之前的名字'张三','李四','王五'
    临时的名字叫赵六
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五')
    加过后的名字'张三','李四','王五','赵六',
    ------------------------第5条------------------------------
    没加之前的名字'张三','李四','王五','赵六'
    临时的名字叫张军
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五','赵六')
    加过后的名字'张三','李四','王五','赵六','张军',
    ------------------------第6条------------------------------
    没加之前的名字'张三','李四','王五','赵六','张军'
    临时的名字叫李佳
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五','赵六','张军')
    加过后的名字'张三','李四','王五','赵六','张军','李佳',
      

  3.   

    -- Test Data: stuinfo
    If object_id('stuinfo') is not null 
        Drop table stuinfo
    Go
    Create table stuinfo(sId int,sName varchar(8))
    Go
    Insert into stuinfo
    select 1,'张三' union all
    select 2,'李四' union all
    select 3,'王五' union all
    select 4,'赵六' union all
    select 5,'张军' union all
    select 6,'李佳' 
    Go
    --Start
    declare @strNames varchar(100)  -- 所有的名字 
    declare @counts int --表记录条数 
    declare @no int --在表中的第几条 
    declare @strsql nvarchar(100) --SQL语句 
    declare @tempName varchar(100) 
    set @no = 1 
    set @counts = (select count(*) from stuinfo) 
    while(@no <=@counts) 
    begin 
    if(@no=1) 
    begin 
    print '第一条记录的名字:' 
    set @strNames=''''+(select top 1 sname from stuinfo )+''',' 
    print @strNames 
    end 
    else 
    begin 
    print '------------------------第'+cast(@no as varchar(1))+'条------------------------------' 
    print '没加之前的名字'+substring(@strNames,1,len(@strNames)-1) 
    set @tempName=(select top 1 sname from stuinfo where charindex(sname,@strNames)=0) 
    print '临时的名字叫'+@tempName 
    set @strsql = N'select top 1 sname from stuinfo where sname not in(' +substring(@strNames,1,len(@strNames)-1) + N')' 
    set @strNames=@strNames+''''+@tempName+''',' 
    print 'sql语句为:'+@strsql 
    print '加过后的名字'+@strNames 
    end set @no = @no +1 
    end --Result:
    /*
    第一条记录的名字:
    '张三',
    ------------------------第2条------------------------------
    没加之前的名字'张三'
    临时的名字叫李四
    sql语句为:select top 1 sname from stuinfo where sname not in('张三')
    加过后的名字'张三','李四',
    ------------------------第3条------------------------------
    没加之前的名字'张三','李四'
    临时的名字叫王五
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四')
    加过后的名字'张三','李四','王五',
    ------------------------第4条------------------------------
    没加之前的名字'张三','李四','王五'
    临时的名字叫赵六
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五')
    加过后的名字'张三','李四','王五','赵六',
    ------------------------第5条------------------------------
    没加之前的名字'张三','李四','王五','赵六'
    临时的名字叫张军
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五','赵六')
    加过后的名字'张三','李四','王五','赵六','张军',
    ------------------------第6条------------------------------
    没加之前的名字'张三','李四','王五','赵六','张军'
    临时的名字叫李佳
    sql语句为:select top 1 sname from stuinfo where sname not in('张三','李四','王五','赵六','张军')
    加过后的名字'张三','李四','王五','赵六','张军','李佳',
    */
    --End 
      

  4.   

    -- Test Data: stuinfo
    If object_id('stuinfo') is not null 
        Drop table stuinfo
    Go
    Create table stuinfo(sId int,sName varchar(8))
    Go
    Insert into stuinfo
    select 1,'张三' union all
    select 2,'李四' union all
    select 3,'王五' union all
    select 4,'赵六' union all
    select 5,'张军' union all
    select 6,'李佳' 
    Go
    --Start
    declare @strNames varchar(100)  -- 所有的名字 
    declare @counts int --表记录条数 
    declare @no int --在表中的第几条 
    declare @strsql nvarchar(100) --SQL语句 
    declare @tempName varchar(100) 
    set @no = 1 
    set @counts = (select count(*) from stuinfo) 
    while(@no <=@counts) 
    begin 
    if(@no=1) 
    begin 
    print '第一条记录的名字:' 
    set @strNames=','+(select top 1 sname from stuinfo )+',' 
    print @strNames 
    end 
    else 
    begin 
    print '------------------------第'+cast(@no as varchar(1))+'条------------------------------' 
    print '没加之前的名字'+substring(@strNames,1,len(@strNames)-1) 
    set @tempName=(select top 1 sname from stuinfo where charindex(','+sname+',',@strNames+',')=0) 
    print '临时的名字叫'+@tempName 
    set @strsql = N'select top 1 sname from stuinfo where sname charindex('',''+sname+'','',''' +@strNames + N''')=0' 
    set @strNames=@strNames+@tempName+',' 
    print 'sql语句为:'+@strsql 
    print '加过后的名字'+@strNames 
    end set @no = @no +1 
    end --Result:
    /*第一条记录的名字:
    ,张三,
    ------------------------第2条------------------------------
    没加之前的名字,张三
    临时的名字叫李四
    sql语句为:select top 1 sname from stuinfo where sname charindex(','+sname+',',',张三,')=0
    加过后的名字,张三,李四,
    ------------------------第3条------------------------------
    没加之前的名字,张三,李四
    临时的名字叫王五
    sql语句为:select top 1 sname from stuinfo where sname charindex(','+sname+',',',张三,李四,')=0
    加过后的名字,张三,李四,王五,
    ------------------------第4条------------------------------
    没加之前的名字,张三,李四,王五
    临时的名字叫赵六
    sql语句为:select top 1 sname from stuinfo where sname charindex(','+sname+',',',张三,李四,王五,')=0
    加过后的名字,张三,李四,王五,赵六,
    ------------------------第5条------------------------------
    没加之前的名字,张三,李四,王五,赵六
    临时的名字叫张军
    sql语句为:select top 1 sname from stuinfo where sname charindex(','+sname+',',',张三,李四,王五,赵六,')=0
    加过后的名字,张三,李四,王五,赵六,张军,
    ------------------------第6条------------------------------
    没加之前的名字,张三,李四,王五,赵六,张军
    临时的名字叫李佳
    sql语句为:select top 1 sname from stuinfo where sname charindex(','+sname+',',',张三,李四,王五,赵六,张军,')=0
    加过后的名字,张三,李四,王五,赵六,张军,李佳,*/
    --End 
      

  5.   

    declare @t table(sId int,sName varchar(64))
    insert into @t
    select '1','张三'
    union all
    select '2','李四'
    union all
    select '3','王五'
    union all
    select '4','赵六'
    union all
    select '5','张军'
    union all
    select '6','李佳'
    set nocount ondeclare @str varchar(8000)
    set @str=''
    select @str=@str+isnull(sname,'')+',' from @t
    print @str
      

  6.   

    为什么后面不能用not in啊? 
    用not in跟substring 输入的时候也是对的啊。
    但是放循环里就不对了啊。给个理由吧..3Q