听起来您需要动态 SQL 命令,但您不能从函数中使用动态 SQL。为此,您必须创建存储过程。
CREATE PROCEDURE [dbo].[GetStockDeatils]
AS
BEGIN
DECLARE @tableName varchar(25),
@PKKey numeric(18,0),
@dsql nvarchar(max) = N''
IF OBJECT_ID('tempdb.dbo.#Results') IS NOT NULL DROP TABLE dbo.#Results
CREATE TABLE dbo.#Results(PurchaseData nvarchar(50) NOT NULL)
DECLARE StockCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT tableName ,PKKey
FROM INVM
OPEN StockCursor
FETCH NEXT FROM StockCursor INTO @tableName, @PKKey
WHILE @@FETCH_STATUS = 0
BEGIN
SET @dsql = 'Insert dbo.#Results Select PurchaseData from ' + @tableName + ' WHERE PKKey = ' + CAST(@PKKey AS varchar(10))
EXEC sp_executesql @dsql
FETCH NEXT FROM StockCursor INTO @tableName, @PKKey
END
CLOSE StockCursor
DEALLOCATE StockCursor
SELECT *
FROM dbo.#Results
END
EXEC [dbo].[GetStockDeatils]
SQLFiddle上的演示
创建视图的过程
CREATE PROCEDURE [dbo].[GetStockDeatils]
AS
BEGIN
DECLARE @dsql nvarchar(max) = N''
IF OBJECT_ID('dbo.v_Results') IS NOT NULL DROP VIEW dbo.v_Results
SELECT @dsql +=
'UNION ALL SELECT PurchaseData FROM ' + tableName +
' WHERE PKKey = ' + CAST(PKKey AS varchar(10))
FROM dbo.INVM
SET @dsql = N'CREATE VIEW dbo.v_Results AS ' + STUFF(@dsql, 1, 10, '')
EXEC sp_executesql @dsql
END
EXEC [dbo].[GetStockDeatils]
SELECT *
FROM dbo.v_Results