zoukankan      html  css  js  c++  java
  • 今天被机试了

    今天被机试了,题目是分页存储过程,记录一下:

    创建数据库:

    USE [master]
    GO
    
    /****** Object:  Database [UserDB]    Script Date: 04/25/2012 15:22:56 ******/
    CREATE DATABASE [UserDB] ON  PRIMARY 
    ( NAME = N'UserDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\UserDB.mdf' , SIZE = 4352KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
     LOG ON 
    ( NAME = N'UserDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\UserDB_log.LDF' , SIZE = 832KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
    GO

    创建数据表的语句:

    USE [UserDB]
    GO
    
    /****** Object:  Table [dbo].[UserBaseinfo]    Script Date: 04/25/2012 15:21:02 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    SET ANSI_PADDING ON
    GO
    
    CREATE TABLE [dbo].[UserBaseinfo](
        [UserId] [int] IDENTITY(1,1) NOT NULL,
        [Name] [varchar](128) NULL,
        [Description] [varchar](1024) NULL,
        [Title] [varchar](64) NULL,
        [Address] [varchar](128) NULL
    ) ON [PRIMARY]
    
    GO
    
    SET ANSI_PADDING OFF
    GO

    以下有几个存储过程:

    select * from UserDB.dbo.UserBaseinfo
    
    
    alter procedure proc_paged_with_notin  ----利用select top and select not in 
    ( 
        @pageIndex int,  --页索引 
        @pageSize int    --每页记录数 
    ) 
    as 
    begin 
        set nocount on; 
        declare @timediff datetime --耗时 
        declare @sql nvarchar(500) 
        select @timediff=Getdate() 
        set

    @sql='select top '+str(@pageSize)+' * from UserDB.dbo.UserBaseinfo where(UserId not in(select top '+str(@pageSize*@pageIndex)+' UserId from UserDB.dbo.UserBaseinfo order by UserId ASC)) order by UserId'
    ----上面这个@sql的意思是:假定pagesize=10,pageindex=2,那么先找前面20条数据,然后再提取不在这20条里面的数据,也就是从21条开始找10条取出来,pageindex记住是pageindex-1的,从0数起,其他的都比较容易理解
    execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql select datediff(ms,@timediff,GetDate()) as 耗时 set nocount off; end
    耗时 :0-80ms
    --利用select top and select max(列) 
    alter procedure proc_paged_with_selectMax  
    ( 
        @pageIndex int,  --页索引 
        @pageSize int    --页记录数 
    ) 
    as 
    begin 
    set nocount on; 
        declare @timediff datetime 
        declare @sql nvarchar(500) 
        select @timediff=Getdate() 
        set @sql='select top '+str(@pageSize)+' * From UserDB.dbo.UserBaseinfo
         where
         (  UserId>(select max(UserId) 
         From (select top '+str(@pageSize*@pageIndex)+' UserId 
         From UserDB.dbo.UserBaseinfo order by UserId) 
         as TempTable
               )
         ) order by UserId' 

    execute(@sql) select datediff(ms,@timediff,GetDate()) as 耗时 set nocount off; end
    A=select top 20 UserId 
         From UserDB.dbo.UserBaseinfo order by UserId 意思是先找到前面20条

    UserId>(select max(UserId) 
         From A 就是说在这20条里面打一个最大号的ID,也就是20
    然后再去找比20大的,找10条

    耗时:0-40ms
    作者:johnny 出处:http://www.cnblogs.com/sunjunlin 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    quartz 之misfire
    quartz 之job
    quartz 日志观察
    quartz集群原理2
    netty 之 GlobalEventExecutor
    redis 修改配置文件不起作用?
    Qto_MemberBaseQuantities
    Qto_PlateBaseQuantities
    Pset_BuildingElementProxyCommon
    matlab绘制函数(观察凹凸性)
  • 原文地址:https://www.cnblogs.com/sunjunlin/p/2469915.html
Copyright © 2011-2022 走看看