access

推荐列表 站点导航

当前位置:首页 > 数据库 > access >

MS SQL Server和Access分别取得随机记录

来源:网络整理  作者:网络  发布时间:2020-12-08 11:04
这是博主用在一个项目上的源码片断,无论是速度、均衡性,还是随机度都非常好,当然这更不可能抽到重复记录了...

Free;

end;

sql.clear;

begin

sl: TStrings;

while not Eof do

sql.Add('where pid is null and tk.题型='+quotedstr(tx)+' and tk.知识点='+quotedstr(zsd));

Result := '';

SQL.Text := ' select ID from 题库表 where pid is null and 题型='+quotedstr(tx)+

else

for i := 0 to StrToIntDef(t_count,0)-1 do

end;

if Result='' then

//以下为调用上述函数生成随机抽题的代码片断

function TTest_Srv_RDataForm.Get_Random_Sql(const kcdm,tx,zsd,nyd,t_count:string):string;

Open;

二、数据库为Access2000时生成随机记录的最好办法:

with tk_query do

Open;

end;

if sl.Count=0 then

//获取题库表中的随机ID,组成一个字符串,类似这样 (3,8,2,25,49,1,7,10,6,83....)

.......

Close;

//kcdm:课程代码,tx:题型,zsd:知识点,nyd:难易度,t_count:某一题型某一知识点某一难度下的要抽取的题量

try

var

begin

sql.add('and tk.难易度='+quotedstr(nyd)+' and tk.课程号='+quotedstr(kcdm)+' order by newid()');

if Result <> '' then

Randomize;

由于Access中没有newid()这一随机函数,故要想在Access中直接由SQL语句生成我们所希望的随机记录不太可能,因此我们只好在开发语言中生成合适SQL语句,让Access执行了(博主的开发工具为Delphi)。

finally

//=============================================== 课程号,题型,知识点,难易度,题量

end;

ii := Random(kk); //取得随机数

sql.Text := Get_Random_Sql(Kcdm,tx_str,zsd_str,nyd_str,txzsd_count_str);

Next;

try

sl.Free;

sl := TStringList.Create;

这是博主用在一个项目上的源码片断,无论是速度、均衡性,还是随机度都非常好,当然这更不可能抽到重复记录了。

end;

Close;

kk := sl.Count;//随机因子

Result := '('+Result+')'; //给结果串前后加上(......),最终形成(24,36,5,89,72,3,6,1....)的串样

end;

Close;

begin

Connection := Adoconnection1;

Break;

sql.Add('select top '+inttostr(st_count)+' tk.ID,标准答案 from 题库表 tk');

一、在MS SQL Server 2000中生成随机记录的最好办法:

if sl.Count=0 then //如果无题可抽了退出循环

function TTest_Srv_RDataForm.Get_Random_ID(const kcdm,tx,zsd,nyd,t_count:string):string;

Result := sl.Strings[ii]

finally

' and 课程号='+quotedstr(Kcdm);

else

Result := Result+','+sl.Strings[ii];

Result := ' select top '+t_count+' tk.ID,标准答案 from 题库表 tk where id in '+Result

i,ii,kk: integer;

' and 知识点='+quotedstr(zsd)+' and 难易度='+quotedstr(nyd)+

Result := Get_Random_ID(kcdm,tx,zsd,nyd,t_count);

sql.Clear;

begin

sl.Add(Fields[0].AsString);

sl.Delete(ii); //为了避免有可能出现的重复,此ID被抽取过后把它删了

with TADOQuery.Create(nil) do

begin

注:关键就是 order by newid() 这条语句!随机因子就是从这里产生的。

begin

begin

Exit;

end; //end with ....

Result := ' select top '+t_count+' tk.ID,标准答案 from 题库表 tk where 1=1 ';

end;

with tk_query do

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/sql/access/1398.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

MS SQL Server和Access分别取得随机记录

2020-12-08 编辑:网络

Free;

end;

sql.clear;

begin

sl: TStrings;

while not Eof do

sql.Add('where pid is null and tk.题型='+quotedstr(tx)+' and tk.知识点='+quotedstr(zsd));

Result := '';

SQL.Text := ' select ID from 题库表 where pid is null and 题型='+quotedstr(tx)+

else

for i := 0 to StrToIntDef(t_count,0)-1 do

end;

if Result='' then

//以下为调用上述函数生成随机抽题的代码片断

function TTest_Srv_RDataForm.Get_Random_Sql(const kcdm,tx,zsd,nyd,t_count:string):string;

Open;

二、数据库为Access2000时生成随机记录的最好办法:

with tk_query do

Open;

end;

if sl.Count=0 then

//获取题库表中的随机ID,组成一个字符串,类似这样 (3,8,2,25,49,1,7,10,6,83....)

.......

Close;

//kcdm:课程代码,tx:题型,zsd:知识点,nyd:难易度,t_count:某一题型某一知识点某一难度下的要抽取的题量

try

var

begin

sql.add('and tk.难易度='+quotedstr(nyd)+' and tk.课程号='+quotedstr(kcdm)+' order by newid()');

if Result <> '' then

Randomize;

由于Access中没有newid()这一随机函数,故要想在Access中直接由SQL语句生成我们所希望的随机记录不太可能,因此我们只好在开发语言中生成合适SQL语句,让Access执行了(博主的开发工具为Delphi)。

finally

//=============================================== 课程号,题型,知识点,难易度,题量

end;

ii := Random(kk); //取得随机数

sql.Text := Get_Random_Sql(Kcdm,tx_str,zsd_str,nyd_str,txzsd_count_str);

Next;

try

sl.Free;

sl := TStringList.Create;

这是博主用在一个项目上的源码片断,无论是速度、均衡性,还是随机度都非常好,当然这更不可能抽到重复记录了。

end;

Close;

kk := sl.Count;//随机因子

Result := '('+Result+')'; //给结果串前后加上(......),最终形成(24,36,5,89,72,3,6,1....)的串样

end;

Close;

begin

Connection := Adoconnection1;

Break;

sql.Add('select top '+inttostr(st_count)+' tk.ID,标准答案 from 题库表 tk');

一、在MS SQL Server 2000中生成随机记录的最好办法:

if sl.Count=0 then //如果无题可抽了退出循环

function TTest_Srv_RDataForm.Get_Random_ID(const kcdm,tx,zsd,nyd,t_count:string):string;

Result := sl.Strings[ii]

finally

' and 课程号='+quotedstr(Kcdm);

else

Result := Result+','+sl.Strings[ii];

Result := ' select top '+t_count+' tk.ID,标准答案 from 题库表 tk where id in '+Result

i,ii,kk: integer;

' and 知识点='+quotedstr(zsd)+' and 难易度='+quotedstr(nyd)+

Result := Get_Random_ID(kcdm,tx,zsd,nyd,t_count);

sql.Clear;

begin

sl.Add(Fields[0].AsString);

sl.Delete(ii); //为了避免有可能出现的重复,此ID被抽取过后把它删了

with TADOQuery.Create(nil) do

begin

注:关键就是 order by newid() 这条语句!随机因子就是从这里产生的。

begin

begin

Exit;

end; //end with ....

Result := ' select top '+t_count+' tk.ID,标准答案 from 题库表 tk where 1=1 ';

end;

with tk_query do

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/sql/access/1398.shtml

相关文章

风云图片

推荐阅读

返回access频道首页