--> 测试数据: [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/*
张三,李四,王五,赵六,张军,李佳
*/
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/*
张三,李四,王五,赵六,张军,李佳
*/
解决方案 »
- 查询2000-5-6这一天的这种查询语句怎么写????
- 关于新生分班,子陌老大已经完美解答,只是情况发生了变化又发难了,再请老大出手解答,谢谢!
- 两个相同结果集求差值问题
- 过程调用失败
- 如何把查询结果马上保存为EXCEL文件?用替代表行不行?===========救命!
- 数据库设计的问题
- 我有三个表a、b、c,我想删除a表时级联删除b,同时删除c表时也级联删除b。但是a和c不能同时设置。
- 一个非常难的查询问题,急!!!!!!!!!!
- reporting services的问题,配置完成后两个URL都打不开
- sql server2008的问题,触发器的,超难大侠帮忙
- 紧急求助!如何还原自动备份的数组到指定日期!
- 关于sql自动编号问题
select @sql=isnull(@sql+',','')+sName from stuInfo order by sId
select @sql
'张三',
------------------------第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('张三','李四','王五','赵六','张军')
加过后的名字'张三','李四','王五','赵六','张军','李佳',
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
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
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
用not in跟substring 输入的时候也是对的啊。
但是放循环里就不对了啊。给个理由吧..3Q