创建一个school的表
schoolID   schoolNAME   NO.
   1          计算机     3
   2          外国语     2
   3           化工      1
   4          医学院     4
(主键)要求:调整NO.的序号  1置顶  
                     2置底
                     3交换
                     4上移
                     5下移
                     6移至
写一存储过程

解决方案 »

  1.   

    先写一置顶的:create proc SetTop
    @schoolID as int
    as 
    UPDATE SCHOOL SET A.[NO.]=B.[SN] FROM SCHOOL A JOIN 
    (SELECT *,ROW_NUMBER() SN FROM (
    SELECT * FROM SCHOOL WHERE schoolID =@schoolID 
    UNION ALL 
    SELECT * FROM SCHOOL WHERE SCHOOLID<> @SCHOOLID ORDER BY [NO.])C)B 
    ON A.SCHOOLID=B.SCHOOLID
      

  2.   

    '在 school 表中添加一个数据递增列 ID ,通过修改记录对应的 ID 来实现你所需的六种操作'
    如:
    ID schoolID schoolNAME NO.
    ----------------------------
    1  1 计算机 3
    2  2 外国语 2
    3  3 化工 1
    4  4 医学院 4'实现 schoolID 为 3 的记录置顶'
    方法:修改 schoolID 为 3 的记录的原 ID 为 1 , schoolID 为 1 的记录的原 ID 大于 1(最好更新所有 ID 值,使其仍保持递增状态),然后按 ID 值排序,即可实现 schoolID 为 3 的记录置顶其他的操作也是这样的!在这里不做示例了...
      

  3.   


    按 10 楼的方法试试,能实现不?只不过是把 10 楼的对 schoolID 排序改为对 NO. 排序
      

  4.   

    --------------------SQL Server数据格式化工具-------------------
    ---------------------------------------------------------------
    -- DESIGNER :happycell188(喜喜)
    --       QQ :584738179
    -- Development Tool :Microsoft Visual C++ 6.0    C Language 
    -- FUNCTION :CONVERT DATA TO T-SQL
    ---------------------------------------------------------------
    -- Microsoft SQL Server  2005
    -- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
    ---------------------------------------------------------------
    ---------------------------------------------------------------use test
    go
    if object_id('test.dbo.tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    ID int,
    schoolID int,
    schoolNAME varchar(20),
    NO int
    )
    go
    --插入测试数据
    insert into tb select 1,1,'计算机',3
    union all select 2,2,'外国语',2
    union all select 3,3,'化工',1
    union all select 4,4,'医学院',4
    go
    --代码实现if object_id('test.dbo.proc_test') is not null drop proc proc_test
    go
    create proc proc_test
    @no int,
    @str1 varchar(10),
    @str2 varchar(10)=null
    as
    begin
    declare @id int
    if @str2 is null
    begin
    if @str1='置顶'
    begin
    update tb set ID=ID+1 where ID<(select ID from tb where NO=@no)
    update tb set ID=1 where NO=@no
    select * from tb order by ID
    end
    --------------------------------------------
    if @str1='置底'
    begin
    update tb set ID=ID-1 where ID>(select ID from tb where NO=@no)
    update tb set ID=(select count(*) from tb) where NO=@no
    select * from tb order by ID
    end
    --------------------------------------------
    if @str1='上移'
    begin
    update tb set ID=ID+1 where ID=(select ID-1 from tb where NO=@no)
    update tb set ID=ID-1 where NO=@no
    select * from tb order by ID
    end
    --------------------------------------------
    if @str1='下移'
    begin
    update tb set ID=ID-1 where ID=(select ID+1 from tb where NO=@no)
    update tb set ID=ID+1 where NO=@no
    select * from tb order by ID
    end
    end
    else
    begin
    if @str2='交换'
    begin
    select @id=ID from tb where NO=@no
    update tb set ID=(select ID from tb where NO=@str1) where NO=@no
    update tb set ID=@id where NO=@str1
    select * from tb order by ID
    end
    --------------------------------------------
    if @str2='移至'
    begin
    select @id=ID from tb where NO=@no
    if @id<=@str1
    begin
    update tb set ID=ID-1 where ID between @id and @str1
    update tb set ID=@str1 where NO=@no
    select * from tb order by ID
    end
    --------------------------------------------
    if @id>@str1
    begin
    update tb set ID=ID+1 where ID between @str1 and @id
    update tb set ID=@str1 where NO=@no
    select * from tb order by ID
    end
    end
    end
    end
    go
    --原数据表select * from tb
    go
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 1 计算机 3
    2 2 外国语 2
    3 3 化工 1
    4 4 医学院 4(4 行受影响)
    */--存储过程测试exec proc_test 1,'置顶'    --将 NO 为 1 的纪录置顶
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 1 计算机 3
    2 2 外国语 2
    3 4 医学院 4
    4 3 化工 1(4 行受影响)
    */exec proc_test 1,'置底'    --将 NO 为 1 的纪录置底
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 3 化工 1
    2 1 计算机 3
    3 2 外国语 2
    4 4 医学院 4(4 行受影响)
    */exec proc_test 1,3,'交换'    --将 NO 为 1 和 3 的纪录位置交换
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 3 化工 1
    2 2 外国语 2
    3 1 计算机 3
    4 4 医学院 4(4 行受影响)
    */exec proc_test 1,'上移'    --将 NO 为 1 的纪录上移
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 1 计算机 3
    2 3 化工 1
    3 2 外国语 2
    4 4 医学院 4(4 行受影响)
    */exec proc_test 1,'下移'    --将 NO 为 1 的纪录下移
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 1 计算机 3
    2 2 外国语 2
    3 4 医学院 4
    4 3 化工 1(4 行受影响)
    */exec proc_test 3,4,'移至'    --将 NO 为 3 的纪录移至第 4 位
    /*测试结果ID schoolID schoolNAME NO
    -----------------------------------
    1 2 外国语 2
    2 3 化工 1
    3 4 医学院 4
    4 1 计算机 3(4 行受影响)
    */
    '我只给出主要的功能,具体还应该有容错处理!不影响功能实现的,你自己看着办吧...
    学习编程,一定要多练,切忌眼高手低...'
      

  5.   

    建议改变一下参数的位置:create proc proc_test
      @no int,
      @str1 varchar(10),
      @str2 varchar(10)=null
    as==>create proc proc_test
      @op varchar(10),
      @op_no int,
      @go_no int=NULL
    as先指定操作,不可缺省的参数;
    再指定当前编号,不可缺省的参数;
    按需要提供第三参数