那位高手能将这个分页的存储过程改成ORACLE 的,感激不尽啊,搞了一天没搞出来。
ALTER PROCEDURE comm_divideRecords_saved
@fields nvarchar(4000),
@table nvarchar(4000),
@link nvarchar(500),
@condition nvarchar(4000),
@keyfield nvarchar(30),
@sort nvarchar(200),
@pagesize int=10,
@pagination int=1 output,
@recordcount int=1 output,
@pagecount int = 1 output AS
declare
@where nvarchar(2000),
@orderby nvarchar(200),
@sql nvarchar(4000)
set @where = ''
if @link<>''or @condition<>''
begin
set @where=' where'
if @link<>''
begin
set @where=@where+' '+@link
if @condition<>''
set @where=@where+' and '+@condition
end
else
set @where=@where+' '+@condition
end
set @sql='select @recordcount=count(*) from '+@table+@where
exec sp_executesql @sql,N'@recordcount int out',@recordcount out
if @recordcount%@pagesize=0
set @pagecount=@recordcount/@pagesize
else
set @pagecount=@recordcount/@pagesize+1
if @pagination>@pagecount and @pagecount>0
set @pagination=@pagecount
if @fields=''
set @fields='*'
set @sql='select '+@fields+' from '+@table+@where
set @orderby=''
if @sort<>''and @sort<>@keyfield and @sort<>@keyfield+' asc'
begin
if @sort=@keyfield+' desc'
set @orderby=' order by '+@sort
else
set @orderby=' order by '+@sort+','+@keyfield
set @sql='select top '+cast(@pagesize as varchar(10))+' * from (select top '+cast(@pagination*@pagesize as varchar(10))+' * from ('+@sql+') TEMP_TABLE_1'+@orderby+') TEMP_TABLE_2 where '+@keyfield+' not in (select top '+cast((@pagination-1)*@pagesize as varchar(10))+' '+@keyfield+' from (select top '+cast(@pagination*@pagesize as varchar(10))+' * from ('+@sql+') TEMP_TABLE_3'+@orderby+') TEMP_TABLE_4'+@orderby+')'+@orderby
print @sql
end
else
begin
set @sql='select top '+cast(@pagesize as varchar(10))+' * from (select top '+cast(@pagination*@pagesize as varchar(10))+' * from ('+@sql+') TEMP_TABLE_1) TEMP_TABLE_2 where '+@keyfield+' not in (select top '+cast((@pagination-1)*@pagesize as varchar(10))+' '+@keyfield+' from (select top '+cast(@pagination*@pagesize as varchar(10))+' * from ('+@sql+') TEMP_TABLE_3) TEMP_TABLE_4)'
print @sql
end
exec(@sql)
------解决方案--------------------------------------------------------
业务逻辑看起来不太复杂。
如果用Oracle写,可以用动态Sql。因为你的表名是传进去的。
剩余的,还有个难点,就是Oracle存储过程不返回结果集。
你需要用到游标参数(Out型)来返回结果集。
我做过测试,用Out型参数返回结果集效率很慢。
建议你将其搞成一个Sql语句在Oracle中执行比较好
------解决方案--------------------------------------------------------
CREATE OR REPLACE PACKAGE SelectResultInfo IS
-- 定义一个结果集返回类型游标
TYPE CURS_RESULT IS REF CURSOR;