You need to use SP_EXECUTESQLit to pass the type of table to work inside a dynamic query. You can also parameterize a variable @Entry_Date_Toinstead of concatenating strings
DECLARE @tbl AS ITEMNAME_ID_TABLE,
@Entry_Date_To date = '2017-10-22',
@qry NVARCHAR(max)
SET @qry = 'SELECT tblStockLedger.item_id,
tblStockLedger.inward_qty,
tblStockLedger.inward_qty2,
Fn_StockValue_1.Value
FROM tblStockLedger
LEFT OUTER JOIN dbo.Fn_StockValue(@Entry_Date_To,@tbl) AS Fn_StockValue_1
ON tblStockLedger.item_id = Fn_StockValue_1.item_id
GROUP BY tblStockLedger.item_id,
tblStockLedger.inward_qty,
tblStockLedger.inward_qty2,
Fn_StockValue_1.Value'
EXEC Sp_executesql
@qry,
N'@tbl ItemName_Id_Table READONLY, @Entry_Date_To Date',
@tbl,@Entry_Date_To
Note. . You pass an empty table variable @tblfor the function
source
share