当前位置: 代码迷 >> Sql Server >> SQL语句如何求1-10的质数
  详细解决方案

SQL语句如何求1-10的质数

热度:274   发布时间:2016-04-27 21:55:56.0
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
  相关解决方案