Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 综合其它

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

时 间:2008-08-21 08:06:33
作 者:umvsoft整理   ID:16  城市:江阴
摘 要:MS SQL Server和Access分别取得随机记录

正 文:

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

  with tk_query do

  begin

    Close;

    sql.clear;

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

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

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

    Open;

  end;

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

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

 

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

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

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

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

var

  sl: TStrings;

  i,ii,kk: integer;

begin

  try

    Result := '';

    sl := TStringList.Create;

    with TADOQuery.Create(nil) do

    begin

      try

        Connection := Adoconnection1;

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

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

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

        Open;

        while not Eof do

        begin

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

          Next;

        end;

        Close;

      finally

        Free;

      end;

    end; //end with ....

    if sl.Count=0 then

       Exit;

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

    begin

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

      Randomize;

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

      if Result='' then

        Result := sl.Strings[ii]

      else

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

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

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

        Break;

    end;

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

  finally

    sl.Free;

 

end;

end;

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

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

begin

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

  if Result <> '' then

&nbs

Access软件网官方交流QQ群 (群号:483923997)       Access源码网店


常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助