mssql分页存储过程
/********** 通用分页存储过程 返回的数据中总是在每条记录前加上行号RowNumber列 ***************/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_common_pager]
@strSelect NVARCHAR(4000) = '*' , --显示的列(多列用逗号分开),例如:id,name; 如果未指定@StrSelect参数,相当于设置它为'*'
@strFrom NVARCHAR(4000) , --表名称,或者是表连接字符串,多表连接例如:student as s inner join dwinfo as dw on s.dwbh=dw.bh ;如果未指定@StrFrom参数,不做查询,返回-1
@strWhere NVARCHAR(4000) = '' , --查询条件,''代表没有条件,单条件或者多条件,多条件例如:name='啊' and id=10; 如果未指定@StrWhere参数,则忽略Where条件
@strOrder NVARCHAR(4000) , --排序列(多个排序列用逗号分开),例如:id desc,name asc; 如果未指定@StrOrder参数,试着自动获得标识列作为@StrOrder,失败则不做查询,返回-1
@pageSize INT = 0 , --每页显示条数; 如果未指定@PageSize参数,或者它<=0,则返回全部记录
@pageIndex INT = 1 , --当前页; 如果未指定@PageIndex参数,会设置它为1,显示第一页数据
@isStats BIT = 0 , --非0则统计,为0则不统计(统计会影响效率)
@rowCount BIGINT = -1 OUTPUT --返回当前页记录数
AS
BEGIN
SET NOCOUNT ON ;
IF @StrFrom IS NULL
BEGIN
RETURN -1
END
--若没有指定@StrOrder,试着获取标识列
IF @StrOrder IS NULL
BEGIN
DECLARE @identitycol NVARCHAR(50)
SET @identitycol = ( SELECT TOP ( 1 )
COLUMN_NAME
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = @StrFrom
AND COLUMNPROPERTY(
OBJECT_ID(@StrFrom),
COLUMN_NAME,
'IsIdentity') = 1
)
IF @identitycol IS NULL --没有Order By和标识列
BEGIN
RETURN -1
END
SET @StrOrder = @identitycol
END
DECLARE @SqlQuery NVARCHAR(4000)
IF ( @PageSize <= 0 )
BEGIN
IF ( @StrWhere = '' )
BEGIN
SET @SqlQuery=N'SELECT ROW_NUMBER() OVER(ORDER BY ' + @StrOrder
+ ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom;
END
ELSE
BEGIN
SET @SqlQuery=N'SELECT ROW_NUMBER() OVER(ORDER BY ' + @StrOrder
+ ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom + ' WHERE ' + @StrWhere;
END
GOTO LABEL_EXEC
END
IF ( @PageIndex = 1 )
BEGIN
IF ( @StrWhere = '' )
BEGIN
SET @SqlQuery = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize)
+ ' ROW_NUMBER() OVER(ORDER BY ' + @StrOrder
+ ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom ;
END
ELSE
BEGIN
SET @SqlQuery = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize)
+ ' ROW_NUMBER() OVER(ORDER BY ' + @StrOrder
+ ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom + ' WHERE ' + @StrWhere ;
END
END
ELSE
BEGIN
IF ( @StrWhere = '' )
BEGIN
SET @SqlQuery = N'WITH CTE AS (SELECT ROW_NUMBER() OVER(ORDER BY '
+ @StrOrder + ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom + ') SELECT * FROM CTE WHERE ROWNUMBER BETWEEN '
+ CONVERT(NVARCHAR, ( ( @PageIndex - 1 ) * @PageSize ) + 1)
+ ' AND ' + CONVERT(NVARCHAR, @PageIndex * @PageSize)
END
ELSE
BEGIN
SET @SqlQuery = N'WITH CTE AS (SELECT ROW_NUMBER() OVER(ORDER BY '
+ @StrOrder + ' ) AS RowNumber,' + @StrSelect + ' FROM '
+ @StrFrom + ' WHERE ' + @StrWhere
+ ') SELECT * FROM CTE WHERE ROWNUMBER BETWEEN '
+ CONVERT(NVARCHAR, ( ( @PageIndex - 1 ) * @PageSize ) + 1)
+ ' AND ' + CONVERT(NVARCHAR, @PageIndex * @PageSize)
END
END
LABEL_EXEC:
DECLARE @StrCount NVARCHAR(1000)
IF (@IsStats != 0)
BEGIN
IF (@StrWhere != '')
BEGIN
SET @StrCount = 'SET @NUM=(SELECT COUNT(0) FROM ' + @StrFrom + ' WHERE ' + @StrWhere + ')'
END
ELSE
BEGIN
SET @StrCount = 'SET @NUM=(SELECT COUNT(0) FROM ' + @StrFrom + ')'
END
EXECUTE SP_EXECUTESQL @strCount ,N'@NUM INT OUTPUT',@RowCount OUTPUT
END
EXEC (@SqlQuery)
END
/****** Object: StoredProcedure [dbo].[sp_get_MenuByAcct] Script Date: 06/15/2012 11:17:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_get_MenuByAcct](
@acctID INT = 0 --用户ID
)
AS
BEGIN
SET NOCOUNT ON ;
SELECT a.func_id id,b.func_nm [name],b.page_id childNode,c.parent_id parentNode,c.page_url url FROM
(SELECT func_id FROM adm_acct_func WHERE acct_id = @acctID) AS a
INNER JOIN (SELECT func_id,func_nm,page_id FROM adm_func WHERE is_left_menu = 1) AS b
ON a.func_id = b.func_id
INNER JOIN adm_page AS c
ON b.page_id = c.page_id
UNION
SELECT 0 id,p.page_nm [name],t.parent_id childNode,p.parent_id parentNode,p.page_url url FROM(
SELECT c.parent_id FROM (SELECT func_id FROM adm_acct_func WHERE acct_id = @acctID) AS a
INNER JOIN (SELECT func_id,func_nm,page_id FROM adm_func WHERE is_left_menu = 1) AS b
ON a.func_id = b.func_id
INNER JOIN adm_page AS c
ON b.page_id = c.page_id
) AS t
INNER JOIN adm_page AS p
ON t.parent_id = p.page_id AND p.parent_id = 0
ORDER BY id ASC, childNode DESC;
END
/****** Object: StoredProcedure [dbo].[sp_get_LoginByAcct] Script Date: 06/15/2012 13:16:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_get_LoginByAcct](
@logName CHAR(32), -- 用户名
@logWord CHAR(64), -- 密码(经过加密的)
@logIP CHAR(15) -- IP
)
AS
BEGIN
SET NOCOUNT ON ;
IF ((SELECT COUNT(0) FROM adm_acct WHERE log_nm = @logName AND log_psd = @logWord AND valid = 1) > 0)
BEGIN
UPDATE adm_acct SET log_dt = CURRENT_TIMESTAMP WHERE log_nm = @logName;
INSERT INTO adm_log (rec_dt, acct_id, opt_cont)
SELECT log_dt, acct_id, '通过IP[' + @logIP + ']登陆系统' FROM adm_acct WHERE log_nm = @logName AND log_psd = @logWord AND valid = 1;
END
SELECT a.acct_id, a.acct_nm, a.log_nm, a.mobile, a.email, a.im_nm, a.sap_sale_id, a.sap_tech_id, a.staff_id, a.sap_usr_type,
ISNULL(b.staff_nm,'') staff_nm, ISNULL(b.staff_no,0) staff_no, ISNULL(b.dep_id,0) dep_id,
ISNULL(b.post_id,0) post_id, ISNULL(b.id_card,'') id_card, ISNULL(c.dep_nm,'') dep_nm, ISNULL(d.post_nm,'') post_nm
FROM adm_acct AS a
LEFT JOIN base_staff AS b ON a.staff_id = b.staff_id
LEFT JOIN base_dep AS c ON b.dep_id = c.dep_id
LEFT JOIN base_post AS d ON b.post_id = d.post_id
WHERE a.log_nm = @logName AND a.log_psd = @logWord AND a.valid = 1;
END