易截截图软件、单文件、免安装、纯绿色、仅160KB

万能的分页sql(转)

CREATE proc page
@RecordCount int output,
@QueryStr nvarchar(100)='table1',--表名、视图名、查询语句
@PageSize int=20, --每页的大小(行数)
@PageCurrent int=2, --要显示的页 从0开始
@FdShow nvarchar (1000)='*', --要显示的字段列表
@IdentityStr nvarchar (100)='id', --主键
@WhereStr nvarchar (200)='1=1',
@FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
as
declare
@sql nvarchar(2000)
set @sql = ''
if @WhereStr = ''
set @WhereStr = '1=1'
if @PageCurrent = 0 begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
end
else begin
if upper(@FdOrder) = 'DESC' begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
end
else begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
end
end
--print @sql
execute(@sql)
if(@RecordCount is null or @RecordCount<=0)begin
declare @tsql nvarchar(200)
set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
select @Recordcount
end
GO


相关文档:

高级SQL 2

1 用UNION替换OR (适用于索引列)
通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描. 注意, 以上规则只针对多个索引列有效.
如果有column没有被索引, 查询效率可能会因为你没有选择OR而降低. 在下面的例子中, LOC_ID 和REGION上都建有索引.
高效: SELECT LOC_ID , LOC_DESC , ......

Java连接SQL Server数据库

用Java连接SQL Server2000数据库有多种方法,下面介绍其中最常用的两种(通过JDBC驱动连接数据库)。
1. 通过Microsoft的JDBC驱动连接。此JDBC驱动共有三个文件,分别是mssqlserver.jar、msutil.jar和msbase.jar,可以到微软的网站去下载(http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A- ......

【小小问题集锦13之 关于分组加顺序号的SQL写法】


/*
原表:  
thid      other  
a              1  
a              1  
b              0  
b              0  
......

ASP.NET SQL 注入免费解决方案

 UrlScan的3.1是一个安全的工具,限制了IIS的HTTP请求将处理类型。 通过阻止特定的HTTP请求,在URLScan 3.1安全工具有助于防止对服务器应用程序可能有害的请求。  UrlScan的3.1是URLScan 2.5的更新版本。支持IIS 5.1中,IIS 6.0和IIS 7.0在Windows Vista和Windows Server 2008。下载地址http://download.csdn.net ......

关于SQL语句Count的一点细节

count语句支持*、列名、常量、变量,并且可以用distinct关键字修饰, 并且count(列名)不会累计null的记录。下面随便用一些例子示范一下count的规则:比如对如下表做统计,所有列这里都用sql_variant类型来表示。
if (object_id ('t_test' )> 0 )
    drop table t_test
go
create table t_test (a ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号