Microsoft SqlServer 2005 通用分页 存储过程
本来我是不赞成使用通用存储过程的,主要是因为根据表结构来定制分页查询不用动态的拼SQL,这样才是真正的高效,而且只要写过一个,那么再有新需求的时候,小范围改动几处就ok了。
但总是有人向我讨要或者讨论通用存储过程,没办法,被逼无奈,良心丧与困境。
木有找到T-SQL代码编辑器
-- =============================================
-- Author: 袁立强
-- Create date: 2006-5-11
-- Description: SQL 2005 版本 通用分页存储过程
-- For: FirstDM
-- BLOG: http://blog.csdn.net/ylqmf
-- =============================================
CREATE PROCEDURE [dbo].[Pagination]
@Table nvarchar(4000) = N'select * from sys.objects',
@ID nvarchar(200) = N'[object_id]',
@OrderBy nvarchar(2000) = N'[object_id]',
@PageSize int = 20,
@PageIndex int = 1,
@RowCount int OUTPUT
as
if @PageIndex<1
begin
set @PageIndex=1;
end
if @PageSize<1
begin
set @PageSize = 20;
end
declare @begin int
declare @end int
set @begin=(@PageIndex-1)*@PageSize+1;
set @end = @PageIndex*@PageSize;
declare @from nvarchar(4000)
declare @sql nvarchar(4000)
declare @index int
select @index = PATINDEX(N'%from%',@table);
if @index = 0
begin
set @from = N'from '+@Table;
set @sql= N'select * from '+@Table;
end
else
begin
set @from = SUBSTRING(@table,@index,LEN(@table)-@index+1);
set @sql = @Table;
end
declare @parameters nvarchar(50)
set @parameters = '@RowCount int OUTPUT';
declare @SQLString nvarchar(max)
select @SQLString =N'
declare @Tpage table
(
id bigint,
inx bigint PRIMARY KEY
);
with cte_page
as
(
select '+@id+N' as id,ROW_NUMBER() over (order by '+@orderby+N') as inx '+@from+N'
)
insert @Tpage select * from cte_page;
with cte_temp
as
(
'+@sql+N'
)
select a.* from cte_temp as a
inner join @Tpage as b on b.id = a.'+@ID+N'
where b.inx between '+CONVERT(nvarchar(100), @begin)+N' and '+CONVERT(nvarchar(100), @end)+N';
with cte_count
as
(
select top 1 inx from @Tpage order by inx desc
)
select @RowCount = inx from cte_count;
'
exec sp_executesql @SQLString , @
相关文档:
方式一:
select top 200 * from view_OrganResource
where 1=1 and OrganID = 57 and
(OrderID not in(select top 1000 OrderID from tb_OrganResource
where 1=1 and OrganID = 57 order by uploadtime desc))
order by uploadtime desc --5858 1980
方式二:
select top 200 * from
vi ......
如何开启SQLSERVER数据库缓存依赖优化网站性能
数据库, 缓存, SQLSERVER, 性能
很多时候,我们服务器的性能瓶颈会是在查询数据库的时候,所以对数据库的缓存非常重要,那么有没有一种方法,可以实现SQL SERVER数据库的缓存,当数据表没有更新时,就从缓存中读取,当有更新的时候,才从数据表中读取呢,答案是肯定的,这 ......
触发器的定义与分类:
只要触发器所在表上的对应时间发生,就会自动执行该触发器的代码.实际上,我们并不能直接调用触发器,唯一的方法是在分配哦触发器的表上执行某种行为方可激活触发器. [触发器是一种特殊的存储过程,他在执行特定的语言事件时生效
& ......