日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴(lài)的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → MSSQL分頁(yè)存儲(chǔ)過(guò)程

      MSSQL分頁(yè)存儲(chǔ)過(guò)程

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/11/15 16:20:16字體大。A-A+

      作者:佚名點(diǎn)擊:83次評(píng)論:0次標(biāo)簽: MSSQL 分頁(yè)存儲(chǔ)

      • 類(lèi)型:編程輔助大。134KB語(yǔ)言:中文 評(píng)分:3.3
      • 標(biāo)簽:
      立即下載
      set ANSI_NULLS ON
      set QUOTED_IDENTIFIER ON
      GO

      if exists(select * from sysobjects where name='DataPaging')
      drop proc DataPaging
      go

      create PROCEDURE [dbo].[DataPaging]
      @tblName varchar(255), -- 表名
      @strGetFields varchar(1000) = '*', -- 需要返回的列
      @fldName varchar(255)='', -- 關(guān)鍵字段名
      @PageSize int = 10, -- 頁(yè)尺寸,如果為0則表示返回所有行,不分頁(yè)
      @PageIndex int = 1, -- 頁(yè)碼
      @doCount INT OUTPUT, -- 返回記錄總數(shù), 非 0 值則返回
      @OrderType bit = 0, -- 設(shè)置排序類(lèi)型, 非 0 值則降序
      @strWhere varchar(2000) = '' , -- 查詢(xún)條件 (注意: 不要加 where)
      @SortField varchar(500) = '' --排序字段
      AS
      declare @strSQL varchar(8000) -- 主語(yǔ)句
      declare @strTmp varchar(110) -- 臨時(shí)變量
      declare @strOrder varchar(400) -- 排序類(lèi)型
      declare @str nvarchar(4000)
      if @doCount != 0
      begin
      if @strWhere !=''
      set @str = N'select @doCount=count(*) from [' + Convert(nvarchar(255),@tblName) + N'] with (nolock) where 1=1 '+Convert(nvarchar(2000),@strWhere)
      else
      set @str = N'select @doCount=count(*) from [' + Convert(nvarchar(255),@tblName) + N'] with (nolock) '

      execute sp_executesql @str, N'@doCount INT output', @doCount output

      print @doCount
      end

      --以上代碼的意思是如果@doCount傳遞過(guò)來(lái)的不是0,就執(zhí)行總數(shù)統(tǒng)計(jì)。以下的所有代碼都是@doCount為0的情況

      if len(@SortField) > 0
      begin
      if @OrderType != 0
      begin
      set @strOrder = ' order by ' +@SortField+' Desc'
      --如果@OrderType不是0,就執(zhí)行降序,這句很重要!
      end
      else
      begin
      set @strOrder = ' order by ' +@SortField
      end
      end
      else
      begin
      set @strOrder=''
      end
      if @PageSize=0
      begin
      if @strWhere != ''
      set @strSQL = 'select '+@strGetFields+ ' from [' + @tblName + '] with (nolock) where 1=1 ' + @strWhere + ' ' + @strOrder
      else
      set @strSQL = 'select '+@strGetFields+ ' from ['+ @tblName + '] with (nolock) '+ @strOrder
      end

      else
      begin
      if @PageIndex = 1
      begin
      if @strWhere != '' ---頁(yè)數(shù)為1 ,條件不為空
      set @strSQL = 'select * from (select top('+convert(varchar(20),@PageSize)+')
      '+@strGetFields+' from '+@tblName+' where 1=1 '+@strWhere+' order by '+@fldName+') TB '+@strOrder
      else
      set @strSQL = 'select * from (select top('+convert(varchar(20),@PageSize)+') '+@strGetFields+' from '+@tblName+' order by '+@fldName+') TB '+@strOrder
      end
      --如果是第一頁(yè)就執(zhí)行以上代碼,這樣會(huì)加快執(zhí)行速度
      else
      begin
      --以下代碼賦予了@strSQL以真正執(zhí)行的SQL代碼

      if @strWhere=''
      set @strSQL = 'select * from (select top('+convert(varchar(20),@PageSize)+') '+@strGetFields+' from '+@tblName+' where 1=1 and '+
      @fldName+' > (select max('+@fldName+') from (select top ('+convert(varchar(20),@PageSize*(@PageIndex-1))+') '+@fldName+' from '+@tblName
      +' order by '+@fldName+' ) T ) order by '+@fldName+') TB '+@strOrder
      else
      set @strSQL = 'select * from (select top('+convert(varchar(20),@PageSize)+') '+@strGetFields+' from '+@tblName+' where 1=1 '+
      @strWhere+' and '+@fldName+' > (select max('+@fldName+') from (select top ('+convert(varchar(20),@PageSize*(@PageIndex-1))+') '+@fldName+
      ' from '+@tblName+' where 1=1 '+@strWhere+' order by '+@fldName+' ) T )'+' order by '+@fldName+') TB '
      + ' where 1=1 ' + @strWhere + ' ' + @strOrder
      end
      end

      print @strSQL
      exec (@strSQL)

      --exec DataPaging 'Sys_User','*','UserID',5,2,1,1, '','UserID'
      -- --表名,所有列,主鍵,條數(shù),頁(yè)碼,是否查總條數(shù),升降序,條件、按某字段排序

      --select * from (select top 30 * from View_Accounts where 1=1 and AccountsID > (select max(AccountsID) from (select top 30 AccountsID
      -- from View_Accounts where 1=1 order by AccountsID ) T ) order by AccountsID) TB
      -- where 1=1 order by AccountsID

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱(chēng):
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)