A
A
Anton Starostin2014-09-04 13:53:50
SQL
Anton Starostin, 2014-09-04 13:53:50

Why doesn't a table-valued function work inside a stored procedure?

I'm working with MS SQL 2008 R2 server if that matters
. I have a stored procedure that builds a large report.
In order to form some data, I have to run the same query with different dates and return a table (id : value), then I link these values ​​and count the field.
It was logical to isolate reusable code. This is what I did. I put it in a function that returns a table value. After that, I make 12 requests to this function and concatenate the return value by ID.
Everything worked fine until I wrapped it all in a stored procedure. As it turned out now, when calling a function that returns a table value from a stored procedure, the query is simply hung up and pretends to be executed.
For example, this is what the function looks like:

CREATE FUNCTION dbo.fGetMonthRatio
(
    @ReportDate DATE
)

RETURNS @RetTable TABLE
(
    id INT
    ,ratio FLOAT
)

AS
BEGIN
    INSERT @RetTable
        SELECT
            id,
            ratio
        FROM
            ...
            много кода с JOIN и прочими штуками
            ...
    RETURN
END

And this is what the procedure looks like:
ALTER PROCEDURE dbo.pTestProcedure
    @ReportDate DATE
AS
BEGIN
    SELECT * FROM dbo.fGetMonthRatio(DATEADD(MONTH, -6, @ReportDate))
END

When I call procedure, the request is hung up.
What is the reason?
PS: You should not pay attention to grammatical errors, if any. I retyped from the monitor, since there is no direct access to the Internet + I changed all the names

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question