深刻sql server 2005 全能分頁存儲進程的詳解。本站提示廣大學習愛好者:(深刻sql server 2005 全能分頁存儲進程的詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是深刻sql server 2005 全能分頁存儲進程的詳解正文
–樹立主表暫時表
CREATE TABLE #temp
(
rownumber bigint,
orderseqno VARCHAR(36),
goodsname VARCHAR(50),
companyname VARCHAR(100)
)
–樹立子表臨 時表
CREATE TABLE #detail
(
orderseqno VARCHAR(36),
detailid UNIQUEIDENTIFIER,
unitprice DECIMAL(12,2),
Qty int
)
–拔出主表數據到主表暫時表
insert into #temp
SELECT oo.rownumber, oo.OrderSeqNO, oo.GoodsName, oo.CompanyName FROM
(SELECT ROW_NUMBER () OVER (ORDER BY oi.createdate DESC) AS rownumber,
oi.OrderSeqNO, oi.GoodsName ,ci.CompanyName
FROM OrderInfo oi INNER JOIN CompanyInfo ci ON oi.CompanyID=ci.CompanyID
WHERE oi.CreateDate<GETDATE()
) AS oo
WHERE rownumber BETWEEN 10 AND 20
–界說游標
DECLARE @temp_cursor CURSOR
–給游標賦值
SET @temp_cursor=CURSOR FOR SELECT #temp.orderseqno,#temp.goodsname FROM #temp
–界說游標輪回進程中所需保留的暫時數據
DECLARE @orderseqno VARCHAR(36),@goodsname varchar(50)
–翻開游標
OPEN @temp_cursor
FETCH NEXT FROM @temp_cursor INTO @orderseqno,@goodsname
–輪回游標,查詢子表數據,然後拔出子表暫時表
WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO #detail
SELECT od.OrderSeqNO,od.OrderDetailID, od.UnitPrice,od.Qty
FROM OrderDetail od
WHERE od.OrderSeqNO=@orderseqno
FETCH NEXT FROM @temp_cursor INTO @orderseqno,@goodsname
END
–封閉游標
CLOSE @temp_cursor
DEALLOCATE @temp_cursor
SELECT * FROM #temp
SELECT * FROM #detail
–刪除暫時表
DROP TABLE #temp
DROP TABLE #detail
下面的T-SQL只在SQL Server 2005上調試勝利。