帮我详细解说一下这个SQL存储过程(200)

  • 主题发起人 主题发起人 blinking1313
  • 开始时间 开始时间
B

blinking1313

Unregistered / Unconfirmed
GUEST, unregistred user!
CREATE procedure Get_Product(@startIndex int,@endIndex int,@docount bit)asset nocount onif(@docount=1)select count(*) AS Counts from Productelsebegindeclare @indextable table(id int identity(1,1),nid int)set rowcount @endIndexinsert into @indextable(nid) select ID from Product order by ID descselect * from Product O,@indextable t where O.ID=t.nidand t.id between @startIndex and @endIndex order by t.idendset nocount offGO
 
CREATE procedure Get_Product(@startIndex int, // 定义的第一个参数@endIndex int, //定义的第二个参数@docount bit) //定义的第三个参数asset nocount onif(@docount=1) //如果第三个参数为1select count(*) AS Counts from Product //执行该语句获得Product表的记录数作为返回值else //如果第三个参数不为1begindeclare @indextable table(id int identity(1,1),nid int) //声明变量indextable类型为tableset rowcount @endIndex //行数设为第二个参数insert into @indextable(nid) select ID from Product order by ID desc 向变量表indextable中插入记录select * from Product O,@indextable t where O.ID=t.nid and t.id between @startIndex and @endIndex order by t.id//执行以上sql语句返回记录endset nocount offGO
 
初学存储过程,大家指正
 
SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息。当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。当 SET NOCOUNT 为 OFF 时,返回计数。
 
我自己来试着解释一下:CREATE procedure Get_Product(@startIndex int, // 定义的第一个参数,表示开始的序号@endIndex int, //定义的第二个参数,表示结束的序号@docount bit) //定义的第三个参数,一个标识asset nocount on //不返回计数if(@docount=1) //如果第三个参数为1,不分页select count(*) AS Counts from Product //执行该语句获得Product表的记录数作为返回值else //如果第三个参数不为1,分页begindeclare @indextable table(id int identity(1,1),nid int) //新建表:indextableset rowcount @endIndex //行数设为结束的序号insert into @indextable(nid) select ID from Product order by ID desc //向表indextable中导入记录select * from Product O,@indextable t where O.ID=t.nid and t.id between @startIndex and @endIndex order by t.id//执行以上sql语句返回所有序号在开始与结束序号之间的记录,从而实现分页endset nocount off//又开始返回计数GO//不知对不对,大家来看一下。[:D]
 
分了吧!
 
多人接受答案了。
 
后退
顶部