Du använder inte DECLARE
när du returnerar en tabellvariabel. Definiera 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
Uppdatera
Vad sägs om att returnera det slutliga resultatet i en lagrad procedur?
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
)
Uppdatera
Prova detta. Skapa en tabellvariabel i UDF för att lagra resultaten av det första urvalet, infoga sedan resultatet av den sista frågan i returvärdet.
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