<原创>SQLServer一个高效的存储过程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了<原创>SQLServer一个高效的存储过程相关的知识,希望对你有一定的参考价值。
USE [Ecological]
GO
/****** Object: StoredProcedure [dbo].[SP_Ecological] Script Date: 2016/3/24 15:02:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
***************************************************************
** 分页存储过程 **
***************************************************************
参数说明:
1.CurrentPage :当前页码
2.PageSize :分页尺寸
3.SQL:用于自定义查询语句
4.tempTableName:临时表名称
***************************************************************/
ALTER PROCEDURE [dbo].[SP_Ecological]
(
@CurrentPage int = 1,
@PageSize int = 10,
@SQL nvarchar(max),
@tempTableName nvarchar(max)
)
AS
DECLARE @strEndRow varchar(500)
DECLARE @strStartRow varchar(500)
DECLARE @strSQL1 VARCHAR(MAX)
DECLARE @strSQL2 VARCHAR(MAX)
DECLARE @strSQL3 VARCHAR(MAX)
SET @strEndRow = CAST(@PageSize*@CurrentPage AS varchar(500))
SET @strStartRow = CAST((@PageSize*(@CurrentPage-1) + 1) AS varchar(500))
SET @strSQL1=‘if object_id(‘‘tempdb..‘[email protected]+‘‘‘) is not null Begin drop table ‘+ @tempTableName+‘ End‘
SET @strSQL2=‘ SELECT identity(int,1,1) as ID,* into ‘ [email protected]+ ‘ FROM (‘[email protected]+‘) as t‘
SET @strSQL3=‘ select * from ‘[email protected]+‘ where id>=‘[email protected]+‘ and id<=‘[email protected]+‘‘
EXEC(@strSQL1+ @[email protected])
GO
/****** Object: StoredProcedure [dbo].[SP_Ecological] Script Date: 2016/3/24 15:02:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
***************************************************************
** 分页存储过程 **
***************************************************************
参数说明:
1.CurrentPage :当前页码
2.PageSize :分页尺寸
3.SQL:用于自定义查询语句
4.tempTableName:临时表名称
***************************************************************/
ALTER PROCEDURE [dbo].[SP_Ecological]
(
@CurrentPage int = 1,
@PageSize int = 10,
@SQL nvarchar(max),
@tempTableName nvarchar(max)
)
AS
DECLARE @strEndRow varchar(500)
DECLARE @strStartRow varchar(500)
DECLARE @strSQL1 VARCHAR(MAX)
DECLARE @strSQL2 VARCHAR(MAX)
DECLARE @strSQL3 VARCHAR(MAX)
SET @strEndRow = CAST(@PageSize*@CurrentPage AS varchar(500))
SET @strStartRow = CAST((@PageSize*(@CurrentPage-1) + 1) AS varchar(500))
SET @strSQL1=‘if object_id(‘‘tempdb..‘[email protected]+‘‘‘) is not null Begin drop table ‘+ @tempTableName+‘ End‘
SET @strSQL2=‘ SELECT identity(int,1,1) as ID,* into ‘ [email protected]+ ‘ FROM (‘[email protected]+‘) as t‘
SET @strSQL3=‘ select * from ‘[email protected]+‘ where id>=‘[email protected]+‘ and id<=‘[email protected]+‘‘
EXEC(@strSQL1+ @[email protected])
转发需声明!!
以上是关于<原创>SQLServer一个高效的存储过程的主要内容,如果未能解决你的问题,请参考以下文章