Du bruger ikke DECLARE
når du returnerer en tabelvariabel. Definer resultattabellen i RETURNS
klausul.
CREATE Function GetFinancials ()
RETURNS @financials TABLE
(
[a bunch of variable declarations in here]
)
AS
BEGIN
insert into @Financials
[big old SELECT query here - this all works fine, and populates @Financials]
RETURN
END
Opdater
Hvad med at returnere det endelige resultat i en lagret procedure?
create procedure uspGetFinanicals
as
declare @financial table
(
[table definition here]
)
insert into @financial
select dbo.GetFinancials()
select *
from @Financials f1
where f1.TransactionDate = (
select MAX(TransactionDate)
from @Financials
where SalesDocumentItemID = f1.SalesDocumentItemID
)
Opdater
Prøv dette. Opret en tabelvariabel i UDF'en for at gemme resultaterne af det første valg, og indsæt derefter resultatet af den endelige forespørgsel i returværdien.
CREATE Function GetFinancials ()
RETURNS @financials TABLE
(
[a bunch of variable declarations in here]
)
AS
BEGIN
declare @table table([a bunch of variable declarations in here])
insert into @table
[big old SELECT query here - this all works fine, and populates @Financials]
insert into @Financials
select *
from @table f1
where f1.TransactionDate = (
select MAX(TransactionDate)
from @table
where SalesDocumentItemID = f1.SalesDocumentItemID
)
RETURN
END