sql 遍历临时表(set rowcount 1 )

--判断零时表是否存在,如果存在删除--  
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempcitys') and type='U')
	drop table #tempcitys 
go
--创建零时表
create table #tempcitys
(
	id int primary key identity(1,1),
	name nvarchar(50)
)
go
--插入数据
insert into #tempcitys(name) values('上海');
insert into #tempcitys(name) values('杭州');
insert into #tempcitys(name) values('苏州');
go
--遍历零时表
declare @city_name nvarchar(50)
while exists(select name from #tempcitys)   
begin   
	set rowcount 1   
	select @city_name=name from #tempcitys   
	set rowcount 0   
	delete from #tempcitys where name = @city_name  
	print  @city_name
end


 



你可能感兴趣的:(sql,set,1,RowCount,遍历临时表)