首页 新闻 赞助 找找看

SQL2005 中set 设置默认参数 不执行

0
悬赏园豆:5 [已解决问题] 解决于 2011-01-16 10:34

USE [data_smf]
GO
/****** 对象:  StoredProcedure [dbo].[page]    脚本日期: 01/10/2011 15:01:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[page]
@key int=0, --返回总页数或数据
@value nvarchar(50)='-',--用于排序字段
@size int=20, --每页显示数据的条数
@order nvarchar(50)='asc',
@table nvarchar(50),
@column nvarchar(100)
 AS

 SET NOCOUNT ON;
--declare @KEYS int
declare @sqlstr1 nvarchar(2000)
declare @sqlstr2 nvarchar(2000)
declare @sqlstr3 nvarchar(2000)
--set @KEYS=@key
set @sqlstr1='select RowNumber=ceiling(count(id) *1.0/'+cast(@size as nvarchar(20))+')from ' +@table +' '--总页数
set @sqlstr2= 'select '+@column+' from (select row_number() over(order by '+@value+' '+@order+  ' ) as RowNum, *  from  '+@table+' ) t where t.RowNum between 20*('+cast(@key as nvarchar(20))+'-1) and 20*'+cast(@key as nvarchar(20))+'order by '+' '+@value+' '+@order+' '

--id可以被替换


BEGIN
--返回总行数
if(@key=0)
begin
exec(@sqlstr1)
end
--返回调用页的数据
 if(@key>0 and @value<>'-')
begin
  exec(@sqlstr2)
end
--返回总页数,并且返回第一页数据
if(@key=-1)
begin
exec(@sqlstr1)
-- 设置key 值无效
set @key=1
 exec(@sqlstr2)

end
END

 

隋凯的主页 隋凯 | 初学一级 | 园豆:65
提问于:2011-01-10 19:01
< >
分享
最佳答案
0

因为@sqlstr2的值计算顺序,在重新设置@Key的值后,需重新设置@sqlstr2的值。

set @key=1
set @sqlstr2= 'select '+@column+' from (select row_number() over(order by '+@value+' '+@order+ ' ) as RowNum, * from '+@table+' ) t where t.RowNum between 20*('+cast(@key as nvarchar(20))+'-1) and 20*'+cast(@key as nvarchar(20))+'order by '+' '+@value+' '+@order+' '
exec(@sqlstr2)

 

收获园豆:5
邀月 | 高人七级 |园豆:25475 | 2011-01-11 16:16
问题马上解决 困扰了我好几天
隋凯 | 园豆:65 (初学一级) | 2011-01-16 10:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册