- SQL code
declare @ProductName nvarchar(50)
declare pcurr cursor for select ProductName from Products
open pcurr
fetch next from pcurr into @ProductName
while (@@fetch_status = 0)
begin
print (@ProductName)
fetch next from pcurr into @ProductName
end
close pcurr
deallocate pcurr
declare @ProductName nvarchar(50)
declare pcurr cursor for select ProductName from Products
open pcurr
fetch next from pcurr into @ProductName
while (@@fetch_status = 0)
begin
print (@ProductName)
fetch next from pcurr into @ProductName
end
close pcurr
deallocate pcurr
此方法適用所有情況,對標結構沒有特殊要求。
方法二:使用循環(huán)
- SQL code
declare @ProductName nvarchar(50)
declare @ProductID int
select @ProductID=min(ProductID) from Products
while @ProductID is not null
begin
select @ProductName=ProductName from Products where
ProductID=@ProductID
print(@ProductName);
select @ProductID=min(ProductID) from Products where
ProductID>@ProductID
end
declare @ProductName nvarchar(50)
declare @ProductID int
select @ProductID=min(ProductID) from Products
while @ProductID is not null
begin
select @ProductName=ProductName from Products where
ProductID=@ProductID
print(@ProductName);
select @ProductID=min(ProductID) from Products where
ProductID>@ProductID
end
此方法適用于表帶有自動增加標識的字段