日期:2014-05-19  浏览次数:20722 次

SQL语句怎么求1-10的质数啊
SQL语句怎么求1-10的质数啊
谢谢

------解决方案--------------------
create function t(@a int)
returns int
as
begin
declare @r int
declare @i int
if @a > 2
begin
if @a % 2 =0
set @r = 0
else
begin
set @i = 3
while @i < sqrt(@a) and @a % @i <> 0
begin
set @i = @i + 2
end
if @i > sqrt(@a) set @r = 1 else set @r = 0
end
end
else
set @r = 1
return @r
end
go

select dbo.t(3)
go
select dbo.t(4)
go

set nocount on
declare @a int
declare @t table(i int)
set @a = 1

while @a < 10
begin
if dbo.t(@a) = 1
insert into @t values(@a)
set @a = @a + 2
end

select * from @t