Answer the question
In order to leave comments, you need to log in
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
ALTER PROCEDURE dbo.pTestProcedure
@ReportDate DATE
AS
BEGIN
SELECT * FROM dbo.fGetMonthRatio(DATEADD(MONTH, -6, @ReportDate))
END
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question