sql 循環表中記錄


=========================================================================循環排序查詢數據=============================================================數據庫

這個寫的是個函數,以便存儲過程當中調用!
USE [數據庫名稱]
GO
/****** Object: UserDefinedFunction [dbo].[GetServer] Script Date: 05/22/2014 17:31:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetServer]
()
RETURNS int函數

AS
BEGIN
declare @count int;--查詢表中數據
declare @number int;--循環條件
declare @id int;---表ID

declare @capbility int;
declare @existnumber int;

select @count=count(*) from serverlist;
if(@count is not null and @count<>0)----若是表中有數據才能夠進行循環
begin
set @number=0;
while @number <@count
begin
select @number = @number +1;----每循環一次循環條件+1
---將表按照循序進行排序,而後每次查詢排序字段
select @id =hh.id,@capbility=hh.capbility, @existnumber=hh.existnumber
from (select ROW_NUMBER() over (order by id) 'rowindex',* from serverlist)hh where hh.rowindex = @number;
if(@existnumber<@capbility)
begin
return @id;
break;
end
end
end
return 0;---若是條件不符合將其返回0
END
=========================================================================循環排序查詢數據結束================================================================server

相關文章
相關標籤/搜索