I'm trying to create this function which returns a table and has a table name as a parameter:
CREATE FUNCTION GetCostData(@CostTable VARCHAR(30))
RETURNS @H2 TABLE (
Repairdate datetime NOT NULL,
ReceivedDate datetime NOT NULL
)
AS
BEGIN
INSERT INTO @H2(DataFactura, Data)
SELECT R1.Repairdate, Max(H1.ReceivedDate) as ReceivedDate
FROM @CostTable R1
JOIN History H1
ON R1.VehicleID=H1.VehicleID
AND H1.ReceivedDate < R1.RepairDate
GROUP BY R1.RepairDate;
RETURN;
END;
But I get 'Must declare the scalar variable "@CostTable".' error. Why? How can I fix it?