Create FUNCTION [dbo].[fn_GetStockDeatils]()
RETURNS @Results TABLE
(
PurchaseData nvarchar(50) NOT NULL
)
AS
BEGIN
Declare @tableName varchar(25)
Declare @PKKey numeric(18,0)
DECLARE StockCursor CURSOR FOR Select tableName ,PKKey from INVM
OPEN StockCursor
FETCH StockCursor INTO @tableName ,@PKKey
Begin
Insert @Results Select PurchaseData from @tableName.PKKey =@PKKey
END
FETCH StockCursor INTO @tableName ,@PKKey
close StockCursor
DEALLOCATE StockCursor
Return
END
I have already written this but it does not working properly. @tableName contains the name of the table. Please help me if possible.
Sounds like you need an dynamic SQL commands, but you cannot use dynamic SQL from a function. For that you have to create stored procedure.
Demo on SQLFiddle
Procedure creating view