0
我有一個複雜的查詢與「與」選擇分頁記錄,我想重用最後臨時表像的記錄計數的另一個用途:SQL如何重用臨時表
;with ProductCatIDs
as
(
select
ID, ParentID, Name
from
[tbl_Categories]
where
ID = @CatID
union all
select
m.ID, m.ParentID, m.Name
from
[tbl_Categories] m
join
ProductCatIDs on ProductCatIDs.ID = m.ParentID
),
ProductsFiltered as (
SELECT DISTINCT tbl_Products.*
FROM tbl_ProductCategories RIGHT OUTER JOIN
tbl_Products ON tbl_ProductCategories.ProductID = tbl_Products.ID
WHERE tbl_Products.Enabled = 'true' and (tbl_ProductCategories.CategoryID IN (
select id
from ProductCatIDs
))),
#ProductsNumbered as (
Select *, ROW_NUMBER() OVER (order by ID DESC) as RowNumber FROM ProductsFiltered
)
SELECT * FROM #ProductsNumbered As tbl_Products
Where
RowNumber > @FirstRec
And RowNumber < @LastRec
SELECT Count(*) FROM #ProductsNumbered
最後一條語句返回一個錯誤無效對象名'#產品編號'
如何重複使用#ProductsNumbered來避免爲簡單計數器計算整個SP?
SELECT Count(*) FROM #ProductsNumbered
謝謝艾倫:) – Mertez