1
以下是我的示例查詢,任何人都可以告訴我如何在WITH子句中執行動態查詢。如何在SQL Server中的WITH子句中執行動態查詢
DECLARE @V_TBLID NVARCHAR(MAX) = 1, @V_QUERY NVARCHAR(MAX) = '', @IN_PAGESIZE INT = 5, @IN_PAGEINDEX INT = 1;
SET @V_QUERY = N'SELECT * FROM MYTABLE'+ @V_TBLID;
WITH RESULTS AS
(
EXEC(@V_QUERY)
)
SELECT * FROM RESULTS
WHERE [row_num] BETWEEN (((@IN_PAGEINDEX - 1) * @IN_PAGESIZE) + 1) AND (@IN_PAGEINDEX * @IN_PAGESIZE);
謝謝@AdaTheDev – Srinivas