SQLSERVER 分页存储过程(2 在SQL2005下使用)
之前有一个SQLServer的分页存储过程 但是性能不是十分理想
又找了一个
--SQL2005分页存储过程
/**
if exists(select * from sysobjects where name='fenye')
drop proc fenye
**/
CREATE procedure fenye
@tableName nvarchar(200) ,
@pageSize int,
@curPage int ,
@orderBy nvarchar(200) ,
@field nvarchar(200) = '*' ,
@condition nvarchar(200)
AS
SET NOCOUNT ON
DECLARE
@STMT nvarchar(max), -- SQL to execute
@recct int -- total # of records (for GridView paging interface)
IF LTRIM(RTRIM(@condition)) = '' SET @condition = '1 = 1'
IF @pageSize IS NULL BEGIN
SET @STMT = 'SELECT ' + @field + 'from ' + @tableName +'WHERE ' + @condition + 'ORDER BY ' + @orderBy
EXEC (@STMT) -- return requested records
END ELSE BEGIN
SET @STMT = 'SELECT @recct = COUNT(*) from ' + @tableName + ' WHERE ' + @condition
-- EXEC sp_executeSQL @STMT, @params = N'@recct INT OUTPUT', @recct = @recct OUTPUT
--SELECT @recct AS recct -- return the total # of records
DECLARE
@lbound int,
@ubound int
SET @curPage = ABS(@curPage)
SET @pageSize = ABS(@pageSize)
IF @curPage < 1 SET @curPage = 1
IF @pageSize < 1 SET @pageSize = 1
SET @lbound = ((@curPage - 1) * @pageSize)
SE
相关文档:
如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......
对数据库进行多表操作,如果表与表之间存在依赖,那么显式的使用事务,可以保持对数据库操作的原子性。用Python访问SqlServer数据库,我使用pymssql库。今天在使用这个库的时候,发现一个问题。
问题大概是这样的:
我有两张表,一张主表(ClassInfo),一张从表(Student),Student表通过外键ClassID与ClassI ......
一 在Oracle中连接数据库
public class Test1 {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
&nbs ......
EXEC sp_addlinkedsrvlogin @rmtsrvname = 'serverontest', @useself = 'false', @locallogin = 'sa', @rmtuser = 'sa', @rmtpassword = 'passwordofsa'
添加登录方式
以上两个语句中,@server为服务器的别名,@datasrc为要链接的目标数据库的连接串,@rmtsrvname为别名,@locallogin为本地登录的用户名,@rmtuser和@rmtpa ......
SqlServer命名规范
参考各种命名规范,我们统一使用命名规范如下:
命名过程中如有现存的缩写,则使用该缩写,如无,一律不得缩写,例:ISBN
数据库:用一个或三个以下英文单词组成,单词首字母大写,如:DepartmentManage;
表名:使用名词性质的单词全拼表示,各单词首字母大写, 使用复数形式,如:Books
& ......