27,579
社区成员
发帖
与我相关
我的任务
分享
declare @su int;
select @su=id from t1;
print @su;
--这样
select
STUFF((SELECT ','+cast(id as varchar) from test FOR XML PATH(''))
,1,1,'');
create table test(id int,kname varchar(10))
insert into test
select 1,'小王'
union all select 2,'小李'
union all select 3,'张三'
union all select 4,'李四'
union all select 5,'王五'
go
declare @s varchar(1000)
set @s=''
SET @s=STUFF((
SELECT ','+cast(id as varchar) from test FOR XML PATH('')
),1,1,'')
SELECT @s
--结果
/*
1,2,3,4,5
*/
create table t1(id int)
insert into t1
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8
go
declare @su int;
declare @count int;
declare @i int;
--记录条数
set @count = (select count(*) from t1)
set @i = 1;
while @i <= @count
begin
select @su=id
from
(select *,row_number() over(order by @@servername) as rownum
from t1
)t where rownum = @i
set @i = @i + 1
print @su;
end;
/*
1
2
3
4
5
6
7
8
*/
declare @su int,@i int,@a int
set @i=1
select @a=count(1) from t1
while @i<@a+1
begin
select @su=id from (
select row_number()over(order by id)num,* from t1
)a where num=@i
set @i=@i+1
print @su
end
create table test(id int,kname varchar(10))
insert into test
select 1,'小王'
union all select 2,'小李'
union all select 3,'张三'
union all select 4,'李四'
union all select 5,'王五'
go
declare @s varchar(1000)
set @s=''
select @s=@s+','+cast(id as varchar)
from test
print stuff(@s,1,1,'')
--结果
/*
1,2,3,4,5
*/