create function f_getnum(@num int)
returns bit
--return 0表示素数,1表示非素数
as
begin
declare @k int,@i int
declare @swit bit
select @k=sqrt(@num),@i=2,@swit=0
while @i<=@k and @swit=0
begin
if @num%@i=0
set @swit=1
else
set @i=@i+1
end
return @swit
end
go
create proc p_test
as
declare @t table(col int)
declare @i int
set @i=3
while @i<=100
begin
if dbo.f_getnum(@i)=0
insert @t values(@i)
set @i=@i+1
end
select * from @t
go
exec p_test
drop function f_getnum
drop proc p_test
/*
col
-----------
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
(所影响的行数为 24 行)
*/