Z
zqssoft
Unregistered / Unconfirmed
GUEST, unregistred user!
如何修改下面的SQL语句为Interbase SQL语法格式的?
用存储过程实现随机生成N条记录
测试表为:Office(OfficeID int,O_name varchar(50),O_type tinyint)
Write By zhubian QQ195986181
*/
create procedure dbo.RandSelect
(
@number int --输入参数(要生成的记录条数)
)
as
declare @tableString varchar(20)
create table #1(OfficeID int,O_name varchar(50),O_type tinyint) --存放随机生成的记录(临时表)
create table #2(rand_num int) --存入已放入#1的记录的OfficeID(实现不生成相同的两条记录)
declare @OfficeID int , @O_name varchar(50) , @O_type tinyint , @randnum int , @rowcount int , @i int
select @rowcount = (select count(*) from Office)
if @number<@rowcount
begin
set @i = @number --要求随机生成的记录条数(输入参数)
end
else
begin
set @i = @rowcount
end
declare c cursor scroll for select * from Office
open c
while(@i>0)
begin
declare @tempoid int--记录生成生的随机数是否重复
set @randnum = rand()*@rowcount+1
set @tempoid = (select count(*) from #2 where rand_num = @randnum)
while( @tempoid <>0 )
begin
set @randnum = rand()*@rowcount+1
set @tempoid = (select count(*) from #2 where rand_num = @randnum)
end
fetch absolute @randnum from c into @OfficeID,@O_name,@O_type
insert into #1 values(@OfficeID,@O_name,@O_type)
insert into #2 values(@randnum)
set @i = @i-1
end
close c
deallocate c
select * from #1
drop table #1
drop table #2
GO
--exec RandSelect @number=150
--drop procedure dbo.RandSelect
用存储过程实现随机生成N条记录
测试表为:Office(OfficeID int,O_name varchar(50),O_type tinyint)
Write By zhubian QQ195986181
*/
create procedure dbo.RandSelect
(
@number int --输入参数(要生成的记录条数)
)
as
declare @tableString varchar(20)
create table #1(OfficeID int,O_name varchar(50),O_type tinyint) --存放随机生成的记录(临时表)
create table #2(rand_num int) --存入已放入#1的记录的OfficeID(实现不生成相同的两条记录)
declare @OfficeID int , @O_name varchar(50) , @O_type tinyint , @randnum int , @rowcount int , @i int
select @rowcount = (select count(*) from Office)
if @number<@rowcount
begin
set @i = @number --要求随机生成的记录条数(输入参数)
end
else
begin
set @i = @rowcount
end
declare c cursor scroll for select * from Office
open c
while(@i>0)
begin
declare @tempoid int--记录生成生的随机数是否重复
set @randnum = rand()*@rowcount+1
set @tempoid = (select count(*) from #2 where rand_num = @randnum)
while( @tempoid <>0 )
begin
set @randnum = rand()*@rowcount+1
set @tempoid = (select count(*) from #2 where rand_num = @randnum)
end
fetch absolute @randnum from c into @OfficeID,@O_name,@O_type
insert into #1 values(@OfficeID,@O_name,@O_type)
insert into #2 values(@randnum)
set @i = @i-1
end
close c
deallocate c
select * from #1
drop table #1
drop table #2
GO
--exec RandSelect @number=150
--drop procedure dbo.RandSelect