---
title: "sql 表变量+while遍历表"
author: "ch.net"
date: "2008-10-16T18:50:12Z"
tags: ["C", "C#", "C++", "SQL"]
slug: "sql-while"
draft: false
lastmod: "2008-10-16T18:50:12Z"
id: 254017
url: "https://haha-net.iteye.com/blog/254017"
---

declare @temp table  
(  
   \[id] int IDENTITY(1,1),  
   \[Name] varchar(10)  
)  
declare @tempId int,@tempName varchar(10)

insert into @temp values('a')  
insert into @temp values('b')  
insert into @temp values('c')  
insert into @temp values('d')  
insert into @temp values('e')

WHILE EXISTS(select \[id] from @temp)  
begin  
 select top 1 @tempId = \[id],@tempName=\[Name] from @temp  
 delete from @temp where \[id] = @tempId  
 print 'Name:----'+@tempName  
end