百万级数据查询

来源:互联网 发布:笔记本电脑打印机端口 编辑:程序博客网 时间:2024/04/27 13:41
/*  
      函数名称:   GetRecordFromPage  
      函数功能:   获取指定页的数据  
      参数说明:       @tblName                 包含数据的表名  
                            @fldName                 关键字段名  
                              @PageSize             每页记录数  
                            @PageIndex         要获取的页码  
                              @IsCount                   是否要取得记录数  
                            @OrderType         排序类型,   0   -   升序,   1   -   降序  
                            @strWhere             查询条件   (注意:   不要加   where)  
  */  
  CREATE     PROCEDURE   pGO_GetRecordFromPage  
          @tblName             varchar(255),               --   表名  
          @fldName             varchar(255),               --   字段名  
          @PageSize           int   =   10,                       --   页尺寸  
          @PageIndex         int   =   1,                         --   页码  
          @IsCount             bit   =   0,                         --   返回记录总数,   非   0   值则返回  
          @OrderType         bit   =   0,                         --   设置排序类型,   非   0   值则降序  
          @strWhere           varchar(1000)   =   ''     --   查询条件   (注意:   不要加   where)  
  AS  
   
  declare   @strSQL       varchar(6000)               --   主语句  
  declare   @strTmp       varchar(500)                 --   临时变量  
  declare   @strOrder   varchar(400)                 --   排序类型  
   
  --   如果是查询记录总数,直接使用Count(0)函数  
  if   @IsCount   !=   0  
    begin  
      if   @strWhere   !=   ''  
        set   @strSQL   =   'select   count(*)   as   Total   from   ['   +   @tblName   +   ']   where   '   +   @strWhere  
      else  
        set   @strSQL   =   'select   count(*)   as   Total   from   ['   +   @tblName   +   ']   '    
    end  
  --如果是想查询记录,则  
  else  
    begin  
      if   @PageIndex   =   1  
        begin  
                  set   @strTmp   =   ''  
                  if   @strWhere   !=   ''  
                            set   @strTmp   =   '   where   '   +   @strWhere  
   
                  set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['  
                            +   @tblName   +   ']'   +   @strTmp   +   '   '   +   @strOrder  
        end  
      else  
        begin  
          --如果是降序查询……  
          if   @OrderType   !=   0  
            begin  
                      set   @strTmp   =   '<(select   min'  
                      set   @strOrder   =   '   order   by   ['   +   @fldName   +']   desc'  
            end  
          --如果是升序查询……  
          else  
            begin  
                      set   @strTmp   =   '>(select   max'  
                      set   @strOrder   =   '   order   by   ['   +   @fldName   +']   asc'  
            end  
   
          if   @strWhere   !=   ''  
                    set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['  
                              +   @tblName   +   ']   where   ['   +   @fldName   +   ']'   +   @strTmp   +   '(['  
                              +   @fldName   +   '])   from   (select   top   '   +   str((@PageIndex-1)*@PageSize)   +   '   ['  
                              +   @fldName   +   ']   from   ['   +   @tblName   +   ']   where   '   +   @strWhere   +   '   '  
                              +   @strOrder   +   ')   as   tblTmp)   and   '   +   @strWhere   +   '   '   +   @strOrder  
          else  
            set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['  
                      +   @tblName   +   ']   where   ['   +   @fldName   +   ']'   +   @strTmp   +   '(['  
                      +   @fldName   +   '])   from   (select   top   '   +   str((@PageIndex-1)*@PageSize)   +   '   ['  
                      +   @fldName   +   ']   from   ['   +   @tblName   +   ']'   +   @strOrder   +   ')   as   tblTmp)'  
                      +   @strOrder      
   
       
        end  
  end  
   
  exec   (@strSQL)  
  GO  
  ----------------------------------------------  
  我也来一下,这个可以实现多表查询。    
   
  --   =============================================    
  --   数据分页的存储过程    
  --   记录号在   TempIDKey_Num   字段中    
  --   调用的例子:   表示   从结果中第3行开始的5条记录。    
  --   T-SQL:EXECUTE   proTest   N'select   top   100   percent   *   from   orders',   3,5    
  --   ASP.NET(C#):    
  --   <%@   Page   Language="C#"   %>    
  --   <%@   Import   Namespace="System.Data"   %>    
  --   <%@   Import   Namespace="System.Data.SqlClient"   %>    
  --   <Script   Runat="Server">    
  --   void   Page_Load(   Object   s,   EventArgs   e   )    
  --   {    
  --   SqlConnection   myConnection;    
  --   SqlCommand   myCommand;    
  --   myConnection   =   new   SqlConnection(   "Server=(local);uid=sa;pwd=11;Database=Northwind"   );    
  --   myCommand   =   new   SqlCommand(   "proTest",   myConnection   );    
  --   myCommand.CommandType   =   CommandType.StoredProcedure;    
  --   myCommand.Parameters.Add("@strSql","Select   top   30   *   from   orders");    
  --   myCommand.Parameters.Add("@startRow",10);    
  --   myCommand.Parameters.Add("@maxRows",15);    
  --   myConnection.Open();    
  --   myDataGrid.DataSource   =   myCommand.ExecuteReader();    
  --   myDataGrid.DataBind();    
  --   myConnection.Close();    
  --   }    
  --   </Script>    
  --   <html><head><title>DataGrid</title></head><body>    
  --   <form   Runat="Server">    
  --   <asp:DataGrid   id="myDataGrid"   Runat="Server"/>    
  --   </form>    
  --   </body></html>    
  --   =============================================    
   
   
  IF   EXISTS   (SELECT   name    
  FROM   sysobjects    
  WHERE   name   =   N'proTest'    
  AND   type   =   'P')    
  DROP   PROCEDURE   proTest    
  GO    
  CREATE   PROCEDURE   proTest    
  @strSql   as   nvarchar(2000)   =   null,   --要查询语句如   Select   top   30   *   from   orders    
  @startRow   as   int   =   null,   --从其开始的从零开始的记录号    
  @maxRows   as   int   =   null   --要检索的最大记录数    
  AS    
  DECLARE   @stopRow   as   int    
  set   @stopRow   =   @startRow   +   @maxRows    
   
  set   @strSql   =   N'   Select   top   '   +   CAST(@StopRow   as   nvarchar(9))   +   '*,   IDENTITY(int,1,1)   AS   TempIDKey_Num   '    
  +   '   INTO   #New_Table   '    
  +   '   FROM(   '   +   @strSql   +   ')   A   '    
  +   '   Select   *   From   #New_Table   Where   TempIDKey_Num>='   +   CAST(@StartRow   as   nvarchar(9))    
  +   '   DROP   TABLE   #New_Table   '    
  execute   sp_executesql   @strSql    
   
  GO    
  ----------------------------------------------  
  这个效率不错。  
  但是我目前碰到了一些问题,不知道大家有什么好的解决办法。  
   
  1、当排序字段为一个以上时,如有如下查询语句:  
            select   id,   a,b,c   from   tb   where   a='aa'   order   by   a,b  
            即@fldName   为'a,b'时,出错:  
      列名   'a,b'   无效。  
   
  2、当某个表没有ID字段,只有复合主键(如a和b)时,也通不过  
原创粉丝点击