• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

为数据库的表自动生成行号--为分页打好基础

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 8次 | 进入软件测试论坛讨论

领测软件测试网


 

在以数据库为基础的应用程序开发中,分页是一个比较常用的操作,
可惜的是SQL Server2000中没有Oracle中相应的ROWNUM属性可用,
小弟用”触发器“生成一个ROWNUM列]
勉强可以一用,当然用如下的SQL语句也可以生成第i页,每页n行,tid是主键列,
select top n  * from tab
 where strWhere  and  tid>(select max(tid)
                                          from (select top (i-1)*n  tid from tab  where  strWhere  order by tid ) as T)
                                         )
order by tid
也可以,但是我想用另一种方法也未尝不可
因此就有自动生成ROWNUM列的想法
eg:
     建表:
CREATE TABLE [dbo].[orderEmp] (
 [rownum] [int] NOT NULL ,---同时该列要求有唯一性约束
 [ordID] [int] IDENTITY (1, 1) NOT NULL ,---主键列
 [empID] [int] NOT NULL ,
 [empTxt] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [empDate] [datetime] NOT NULL ---此列上建“聚集索引“
) ON [PRIMARY]
----------对插入语句处理的触发器
CREATE  TRIGGER orderEmpAddTrg
ON  orderEmp
instead of   INSERT
AS
begin
    declare  @rw int
    select  @rw=0
    select @rw=max(rownum) from orderEmp
    if(@rw is null)
         select @rw=0
    select  @rw=@rw+1
  INSERT INTO orderEmp(rownum,empID,empTxt,empDate)
  SELECT @rw, i.empID,i.empTxt,i.empDate
  FROM inserted  i 
end

---删除的“触发器@#
CREATE TRIGGER orderEmpDelTrg
ON dbo.orderEmp
FOR  DELETE
AS
begin
    set nocount on
    declare  @rw int
    declare   @tab table(rw int)
    insert into @tab
    select   rownum from deleted
    order by rownum desc  -----不可以掉,至于为什么,大家自己试试就知道了
    declare  cp cursor
     for
     select   rw from @tab
     open cp
     fetch next from cp into  @rw
     while  @@fetch_status=0
      begin
         update    orderEmp
          set        rownum=rownum-1
         where     rownum>@rw
         fetch next from cp into @rw
      end
    close  cp
    deallocate cp
    set nocount off
end
---这个触发器是为屏掉用户直接从SQL企业管理器 打开表后对表中的ROWNUM列进行修改
---可能不完全
----但是通过UPdate语句操作表的时,只要不修改rownum列是不会出现问题的
CREATE TRIGGER orderEmpUpdTrg
ON orderEmp
FOR  UPDATE
AS
  begin
  IF UPDATE (rownum)
     RAISERROR (@#ROWNUM列不可以自行修改!@#, 16, 1)
  ROLLBACK TRANSACTION

end  
添加新记录的存储过程如下:
create    PROCEDURE [addOrderEmp]
 ( @empID  [int],
  @empTxt  [varchar](50),
  @empDate  [datetime])

AS INSERT INTO [orderEmp]
  (  [rownum],  [empID],  [empTxt],  [empDate])
 
VALUES
 (        1,  @empID,  @empTxt,  @empDate)----“1“是一定要的但是不会影响ROWNUM列,只是为了
占用内存而已
下面是我的测试用例:

insert into orderemp(rownum,empid,emptxt,empdate)
values( 1, 173,@#ddfdd@#,getdate())
insert into orderemp(rownum,empid,emptxt,empdate)
values( 1, 123,@#ddfdd@#,getdate())
insert into orderemp(rownum,empid,emptxt,empdate)
values( 1, 163,@#ddfdd@#,getdate())
insert into orderemp(rownum,empid,emptxt,empdate)
values( 1, 153,@#ddfdd@#,getdate())
insert into orderemp(rownum,empid,emptxt,empdate)
values( 1, 143,@#ddfdd@#,getdate())
select    * from orderemp  order by rownum
delete from  orderemp where   empid>150 and empid<170
select    * from orderemp  order by rownum
至于更新的语句吗
只要不更新ROWNUM列,就不用处理了

注:一定要把数据库的:服务器设置--->服务器行为--->第二个选项不要选中

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网