用SQLServer实现数据表中,将任意一条记录信息移动到该表中的任意位置

来源:互联网 发布:js预加载页面动画 编辑:程序博客网 时间:2024/06/06 01:20
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

--定位数据
--思路:
--  保存要被移动记录的原主键值和新主键值(如果有主键的话),然后比较两个主键值的大小(记录信息按升序排列),
--  如果原主键值大,表明该记录被移动到前面的新位置,可将原位置的记录信息保存下来,并将从新位置的记录信息到
--  原记录的前一条记录的信息顺次往下移动,把保存的原记录移动到新位置即可。
--  如果原主键值小,表明该记录被移动到后面的新位置,可将原位置的记录信息保存下来,并将从新位置的记录信息到
--  原记录的下一条记录的信息顺次往上移动,把保存的原记录移动到新位置即可。实际上就是记录块的移动.
--  但是,如果数据表中的数据非常庞大,该存储过程的执行效率将会下降.
 

Usezzydb

--创建举例表(学生信息表)
CreateTableT_StudentsInfo
(i_idintidentity(1,1),--系统自增流水号
 c_Stu_IDnvarchar(10),--学号
 c_ClassNamenvarchar(50),--班级
 d_BirthDaydatetime)--出生日期
go

--向举例表中插入4条学生信息,以验证下面的存储过程(sp_MyAdjustRecordOrder)
InsertintoT_StudentsInfovalues('001','大二三班','1978-01-25')
InsertintoT_StudentsInfovalues('002','大一六班','1979-02-05')
InsertintoT_StudentsInfovalues('003','大四三班','1981-07-15')
InsertintoT_StudentsInfovalues('004','大三一班','1976-01-05')

select*fromT_StudentsInfo

ifobject_id('sp_MyAdjustRecordOrder')<>0
 dropprocsp_MyAdjustRecordOrder
go

CreateProcsp_MyAdjustRecordOrder(@OldStuIdnvarchar(10),@NewStuIdnvarchar(10))
as
--@OldStuid学生学号(用以表示被移动的记录),
--@NewStuid学生学号(用以表示将被移动的记录插入的新位置)
begin
 declare@Old_i_idint,@New_i_idint
 declare@i_BlockCountint--即将被移动的记录块条数
 declare@iint--循环变量

 --获得id值
 Select@Old_i_id=(selectI_idfromT_StudentsInfowherec_Stu_ID=@OldStuId)
 Select@New_i_id=(selectI_idfromT_StudentsInfowherec_Stu_ID=@NewStuId)
 select@i_BlockCount=abs(@Old_i_id-@New_i_id)

 --保存被移动的学生信息
 Selectc_Stu_ID,c_ClassName,d_BirthDay
  intoNew_StudentsInfo--临时创建的表,用完后删除
 fromT_StudentsInfo
 wherec_Stu_ID=@OldStuId
 
 
 if@New_i_id<@Old_i_id --将原记录信息移动到了前面
 begin
  select@i=0
  while@i<=@i_BlockCount-1
  begin
   updateT_StudentsInfo
   setc_Stu_ID=T2.c_Stu_ID,
    c_ClassName=T2.c_ClassName,
    d_BirthDay=T2.d_BirthDay
   FromT_StudentsInfo,T_StudentsInfoT2
   where  (T_StudentsInfo.i_id=@Old_i_id-@i)and
     (T2.i_id=@Old_i_id-@i-1)
   
   select@i=@i+1  
  end
 end

 if@New_i_id>@Old_i_id --将原记录信息移动到了后面
 begin
  select@i=0
  while@i<=@i_BlockCount-1
  begin
   updateT_StudentsInfo
   setc_Stu_ID=T2.c_Stu_ID,
    c_ClassName=T2.c_ClassName,1
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击