row_number sql server 2005 更新数据

来源:互联网 发布:员工考勤工资软件 编辑:程序博客网 时间:2024/04/30 04:07

背景: 项目中遇到这样一个问题 ,原来的一些应用数据库设计时,有的表没有主键。我们需要手动添加主键 而且不能是自动增长的。

 

一共需要做这些事情 :

 

1. 添加字段。不为NULL

2.更新数据

3.设为主键。

 

为了不影响原来的程序。我写了一些触发器 来实现自动增长,而且对原来的应用没有任何影响。

 

为了更新数据,我们单独写了一个存储过程来实现数据的更新。 数据更新的困难在于要生成唯一的值,而且更新一条记录。

考虑多个方案,后来想到 row_number  sql server 2005  相当于oracle 的rowid

 

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

alter PROCEDURE [dbo].[zfhf_letter_pro]

as

 

declare @rowid int

declare  zfhf_letter_cursor cursor for SELECT  ROW_NUMBER() OVER (ORDER BY sublet_id ASC) AS ROWID  FROM zfhf_letter

open zfhf_letter_cursor

fetch next from zfhf_letter_cursor into @rowid

WHILE @@FETCH_STATUS = 0

begin

 

with CTE
as
(
  select rw=row_number() over(order by sublet_id),*
  from zfhf_letter
)
update CTE
  set TONGBUID=@rowid
 where rw=@rowid

fetch next from zfhf_letter_cursor into @rowid


end
close zfhf_letter_cursor

deallocate zfhf_letter_cursor

 

 

 

这是存储过程的全部

 

感兴趣的同学 可以看看

 

 

 

下面是自动增长替代的那个触发器

 

 

create trigger zfhf_reply_tri

--应用到的表

on [dbo].zfhf_reply

--触发事件

for insert

as

begin  

-- 表中存在主键为的记录

        if exists(select * from zfhf_reply  where recid=0)

        begin

          delete from zfhf_reply  where recid=0

        end

 

--变量定义

  declare @myID as int

  declare @myLet_unit_id as int

  declare @myReplytitle as varchar(200)  

  declare @myReplycontent as varchar(5000) 

  declare @myreplydate as datetime

  declare @myReplyer as varchar(200) 

  declare @myHigher_up as varchar(50) 

  declare @myReplyTel as varchar(100) 

--变量赋值

 

  select @myID = recid  from inserted

    if(@myID=0)

       begin

      

 select @myID = max(recid) from zfhf_reply 

       end

    else

--如何默认有值

       begin

          

    set @myID=0

       end

 

   select @myLet_unit_id = let_unit_id from inserted

   select @myReplytitle = replytitle from inserted

   select @myReplycontent = replycontent from inserted

   select @myreplydate = replydate from inserted

   select @myReplyer = replyer from inserted

   select @myHigher_up = higher_up from inserted

   select @myReplyTel = replyTel from inserted

    --数据库没有记录的变量

      declare @noResult as int

      set @noResult=0

 

       --表中没有任何记录

        if not exists(select * from zfhf_reply)

        begin

           --变量定义

             declare @tempID as int

             declare @tempLet_unit_id as int

             declare @tempReplytitle as varchar(200)   

             declare @tempReplycontent as varchar(5000) 

             declare @tempreplydate as datetime

             declare @tempReplyer as varchar(200) 

             declare @tempHigher_up as varchar(50) 

             declare @tempReplyTel as varchar(100) 

             declare @tempName as varchar(20)   

           --变量赋值

              select @tempID = max(recid) from zfhf_reply 

              select @tempLet_unit_id = let_unit_id from inserted

              select @tempReplytitle = replytitle from inserted

              select @tempReplycontent = replycontent from inserted

              select @tempreplydate = replydate from inserted

              select @tempReplyer = replyer from inserted

              select @tempHigher_up = higher_up from inserted

              select @tempReplyTel = replyTel from inserted

 

                  insert into zfhf_reply(recid,let_unit_id,replytitle,replycontent,replydate,replyer,higher_up,replyTel)

               values(1,@tempLet_unit_id,@tempReplytitle,@tempReplycontent,@tempreplydate,@tempReplyer,@tempHigher_up,@tempReplyTel)

          set @noResult=1

 

return

        end

      

 

 

--数据入库

     if not exists(select * from zfhf_reply where recid=@myID+1)

        begin

         insert into zfhf_reply(recid,let_unit_id,replytitle,replycontent,replydate,replyer,higher_up,replyTel)

             values(@myID+1,@myLet_unit_id,@myReplytitle,@myReplycontent,@myreplydate,@myReplyer,@myHigher_up,@myReplyTel)

end

 

 

--删除插入的主键为默认值的记录在数据库不存在记录的情况下而插入的记录

       if(@noResult=1)

       begin

         delete from zfhf_reply where recid=1

       end

--删除主键为的数据insert 插入的主键为默认值的数据   

       delete from zfhf_reply where recid=0

    return

    end

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苍蝇不小心碰到嘴唇了怎么办 苍蝇老往身上飞怎么办 单位乒乓球比赛有领导参加怎么办 意外看到别人打野战怎么办 骨盆低想顺产要怎么办 右胯比左胯突出怎么办 一岁宝宝骨盆不对称怎么办 入盆了又出来了怎么办 大腿前突小腿后怎么办 英语不好高二了怎么办 断奶后又复吸怎么办 招联金融综合评定不足怎么办 git本地分支比远程高怎么办 娃儿上嘴唇里面破了怎么办 错过了各大招聘怎么办 优秀团员申请表没有获奖情况怎么办 大学生毕业学生登记表涂改了怎么办 c语言挂科了怎么办 吸入腐蚀性气体鼻子流血了怎么办 腰劳损痛的厉害怎么办 两个宝宝斜颈左边力量差怎么办 姿势不正确引起的习惯性斜颈怎么办 一岁宝宝有点斜颈怎么办 六个月宝宝有点斜颈怎么办 5月宝宝有点斜颈怎么办 半岁宝宝头偏怎么办 肌性斜颈成年后怎么办 5个月孩子脖子歪怎么办 宝宝一岁多头歪怎么办 四个月宝宝歪脖子怎么办 1岁宝宝脖子歪怎么办 宝宝脖子是歪的怎么办 小孩脖子睡歪了怎么办 脸部三角区肿了怎么办 面部三角区挤了怎么办 胳膊扭着了肿了怎么办 多囊卵巢综合症治不好怎么办 胳膊受了风发麻怎么办 胳膊抻筋了很疼怎么办 腰抻了怎么办最有效 孩子胳膊抻着了怎么办