日期:2014-05-17 浏览次数:20636 次
--> 测试数据:#Table1求分页显示最好是高效一点的
IF OBJECT_ID('TEMPDB.DBO.#Table1') IS NOT NULL DROP TABLE #Table1
GO
CREATE TABLE #Table1([MID] VARCHAR(4),[PID] VARCHAR(8),[AA] INT)
INSERT #Table1
SELECT 'A001','A001-001',1 UNION ALL
SELECT 'A001','A001-002',0 UNION ALL
SELECT 'A001','A001-003',0 UNION ALL
SELECT 'A001','A001-004',0
--> 测试数据:#Table2
IF OBJECT_ID('TEMPDB.DBO.#Table2') IS NOT NULL DROP TABLE #Table2
GO
CREATE TABLE #Table2([ID] INT,[QID] VARCHAR(4),[PID] VARCHAR(8),[DD] INT,[EE] INT)
INSERT #Table2
SELECT 1,'XX01','A001-001',1,1 UNION ALL
SELECT 2,'XX02','A001-002',0,0 UNION ALL
SELECT 3,'XX02','A001-003',0,0 UNION ALL
SELECT 4,'XX04','A001-003',0,0 UNION ALL
SELECT 5,'XX05','A001-003',1,1
--> 测试数据:#Table3
IF OBJECT_ID('TEMPDB.DBO.#Table3') IS NOT NULL DROP TABLE #Table3
GO
CREATE TABLE #Table3([ID] INT,[QID] VARCHAR(4),[Name] VARCHAR(4))
INSERT #Table3
SELECT 1,'XX02','张三' UNION ALL
SELECT 2,'XX04','李四' UNION ALL
SELECT 3,'XX05','王五' UNION ALL
SELECT 4,'XX06','赵六'
--------------开始查询--------------------------
SELECT d.[Name] , a.[PID] , d.[DD] , d.[EE] , a.[AA]
FROM #Table1 a
LEFT JOIN (
SELECT b.[PID] , c.[Name],b.[DD] , b.[EE]
FROM #Table2 b
JOIN #Table3 c
ON b.[QID]= c.[QID]
AND (b.[EE]=0 and b.[DD]=0)
) d
ON a.[PID] = d.[PID]
WHERE a.aa = 0
----------------结果----------------------------
/*
Name PID DD EE AA
张三 A001-002 0 0 0
张三 A001-003 0 0 0
李四 A001-003 0 0 0
NULL A001-004 NULL NULL 0
*/
SELECT Identifier,ROW_NUMBER() OVER(order by Identifier desc) AS RowIndex INTO #tb1_1 FROM View_GoodsBrand WITH(NOLOCK)
WHERE
SET @SumCount=@@RowCount
IF(@SumCount%@PageSize=0)
BEGIN
SET @TotalPage=@SumCount/@PageSize
END
ELSE
BEGIN
SET @TotalPage=Round(@SumCount/@PageSize,0)+1