帮忙修改以下两个存储过程问题一:
编写一个存储过程,根据货品编号更新货品名称,并且更新的名称不可以跟其它货品相同,并返回更新的结果。
create proc updateProductByName @pid int,@pname varchar(20)
 As
Update 货品信息 set 名称=@pname where 编号=@pid and 名称=@pname
Go
declare @pname varchar(20),@pid int
Exec updateProductByName @pname='电脑机箱' , @pid=1问题二:
写出查询订单信息表,并以订货日期降序显示第n页的记录(假设每页显示2条记录)
SQL:
Create proc getOrder @page int,@pagesize int
As
Declare @ pageCount varchar(5),@pagecapacity varchar(5)
Set @pageCount= @page, @pagecapacity =@pagesize
Select top @pageCount *from 订单信息 where 编号 not in(select top @pagecapacity *(@pageCount -1) from订单信息 order by 订货日期desc ) order by 订货日期desc
Go
Exec getOrder @pagesize=2,@page=2

解决方案 »

  1.   


    /code
    --我说,这帖子您给20分?难怪没人回答,我也不愿意回答,写页查询呢.才给这点分,我拒绝回答.您的问题太简单!
      

  2.   

    create proc updateProductByName @pid int,@pname varchar(20) 
     As 
    Update 货品信息 set 名称=@pname where 编号=@pid and 名称=@pname 
    Go 
    declare @pname varchar(20),@pid int 
    Exec updateProductByName @pname='电脑机箱' , @pid=1 ---
    create proc updateProductByName @pid int,@pname varchar(20)  
     As  
    if not exists(select 1 from 货品信息 where 名称=@pname )
        Update 货品信息 set 名称=@pname where 编号=@pid 
    else 
        raiserror('exists',16,1)Go  
    declare @pname varchar(20),@pid int  
    Exec updateProductByName @pname='电脑机箱' , @pid=1 
      

  3.   

    Create proc getOrder @page int,@pagesize int 
    As 
    Declare @ pageCount varchar(5),@pagecapacity varchar(5) 
    Set @pageCount= @page, @pagecapacity =@pagesize 
    Select top @pageCount *from 订单信息 where 编号 not in(select top @pagecapacity *(@pageCount -1) from订单信息 order by 订货日期desc ) order by 订货日期desc 
    Go 
    ------------------------------create   procedure getOrder 
      @sqlstr nvarchar(4000), --查询字符串  
      @currentpage int, --第N页  
      @pagesize int --每页行数  
      as  
      set nocount on  
      declare @P1 int, --P1是游标的id  
      @rowcount int  
      exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output  
      select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页  
      set @currentpage=(@currentpage-1)*@pagesize+1  
      exec sp_cursorfetch @P1,16,@currentpage,@pagesize    
      exec sp_cursorclose @P1  
      set nocount off  
      -------------- 
    exec getOrder 'Select  * from 订单信息  order by 订货日期 desc ',@pagesize=2,@page=2 
      

  4.   

    /*修改后*/
    create proc updateProductByName (@pid int,@pname varchar(20) )
     As 
        begin 
          begin tran  UpdatePoint--开始一个事务
            
         if  exists(select * from 货品信息 where 名称=@pname)--如果存在记录,则回滚,并返回false
             begin
                rollback tran UpdatePoint
                return false; 
             end
          else --更新记录
             begin
               update 货品信息 set 名称=@pname where id=@pid--更新记录
                 if(@@rowcount>0)--受影响的行数大于0
                   begin 
                     commit tran UpdatePoint
                     return true;
                   end
                 else
                    begin
                    rollback tran updatePoint
                      return false
                    end
             end
         end 
     2.
        create   procedure getOrder 
      @sqlstr nvarchar(4000), --查询字符串  
      @currentpage int, --第N页  
      @pagesize int --每页行数  
      as  
      set nocount on  
      declare @P1 int, --P1是游标的id  
      @rowcount int  
      exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output  
      select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页  
      set @currentpage=(@currentpage-1)*@pagesize+1  
      exec sp_cursorfetch @P1,16,@currentpage,@pagesize    
      exec sp_cursorclose @P1  
      set nocount off  
       
      

  5.   

    SQL codecreate proc updateProductByName @pid int,@pname varchar(20) 
     As 
    Update 货品信息 set 名称=@pname where 编号=@pid and 名称=@pname 
    Go 
    declare @pname varchar(20),@pid int 
    Exec updateProductByName @pname='电脑机箱' , @pid=1 ---
    create proc updateProductByName @pid int,@pname varchar(20)  
     As  
    if not exists(select 1 from 货品信息 where 名称=@pname )
        Update 货品信息 set 名称=@pname where 编号=@pid 
    else 
        raiserror('exists',16,1)Go  
    declare @pname varchar(20),@pid int  
    Exec updateProductByName @pname='电脑机箱' , @pid=1