Anda tidak menggunakan DECLARE
saat mengembalikan variabel tabel. Tentukan tabel hasil di RETURNS
klausa.
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
Perbarui
Bagaimana dengan mengembalikan hasil akhir dalam prosedur tersimpan?
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
)
Perbarui
Coba ini. Buat variabel tabel di dalam UDF untuk menyimpan hasil pemilihan pertama, lalu masukkan hasil kueri akhir ke dalam nilai kembalian.
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