0

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?

4
  • 2
    CostTable is VARCHAR, you might want to use dynamic sql. Commented Nov 29, 2013 at 9:41
  • You cannot use dynamic SQL in functions, I think... You will need to change that into a stored proc. Commented Nov 29, 2013 at 9:44
  • beware of what you are doing. Generic functions like that, especially if you use them in another SELECT, will drain performances. Sometimes, in SQL, it is better to copy/paste code Commented Nov 29, 2013 at 9:47
  • Thanks, but I can't turn it into a SP, because I'm using it in a query. Commented Nov 29, 2013 at 10:04

2 Answers 2

1

You're missing the DECLARE @CostTable statement

Try this code:

CREATE FUNCTION GetCostData(@CostTable VARCHAR(30))
RETURNS @H2 TABLE (
    Repairdate    datetime     NOT NULL,
    ReceivedDate  datetime     NOT NULL
)

AS

BEGIN 
DECLARE @CostTable table (RepairDate datetime not null) 
    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;

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

As you cannot use dynamic SQL in functions, it will be better to turn it into a stored procedure:

CREATE PROCEDURE GetCostData(@CostTable VARCHAR(30)) 
AS
BEGIN 
  DECLARE @sql NVARCHAR(1000) =
  N'SELECT R1.Repairdate, Max(H1.ReceivedDate) as ReceivedDate
    FROM ' + @CostTable + N'R1
    JOIN History H1 
    ON R1.VehicleID=H1.VehicleID 
    AND H1.ReceivedDate < R1.RepairDate
    GROUP BY R1.RepairDate'

    EXEC (@sql)
END

To use the result of this stored proc, you should insert the results into a temp table and then join to this temp table. See this SQL Fiddle.

create table table1(id int, Repairdate datetime)

insert into table1 values (1, getdate())

go

create procedure GetCostData
as
begin
  select getdate() as Repairdate, getdate() as ReceivedDate
end

go

create table #temp(Repairdate datetime, ReceivedDate datetime)

insert into #temp
exec getcostdata

select * from table1 t1
inner join #temp t
on cast(t1.Repairdate as date) = cast(t.Repairdate as date)

3 Comments

Thanks, but I can't turn it into a SP, because I'm using it in a query.
In what way are you using it in the query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.