これは質問ではなく、開発者に役立つかもしれないアイデアを共有したかっただけです
この例は、プロシージャから別のプロシージャに変数テーブルを返す方法をシミュレートします
手順例
Create Proc [dbo].[ReturnVariableTable] (
@_PersonID uniqueidentifier, @_fromdate date , @_Todate date
@obj_cursor cursor varying output
) as
Begin Try
Begin Tran
DECLARE @PersonSalaryOverYear Table(_Salary Float ,PersonID uniqueidentifier ,Month Int )
declare @tempdate = @_fromdate
while ( @tempdate <= @_Todate )
BEGIN
declare @_Salary FLOAT = (select Salary from Person where ID = @_PersonID AND Month(@tempdate) = MonthNumber )
insert into @PersonSalaryOverYear values(@_Salary, @_PersonID,Month(@tempdate) )
select @tempdate = DATEADD(Month,1,@tempdate)
END
Set @obj_cursor = cursor forward_only static for
select @_Salary as Salary, @_PersonID as PersonID ,Month(@tempdate) as Month
Open @obj_cursor
Commit
End Try
呼び方
Declare @cur Cursor
Declare @_Salary Float
Declare @_PersonID uniqueidentifier
Declare @Month int
Exec ReturnMultipleData
@_PersonID = '2121ad51-fa84-4da4-a9e6-c22e259d549e',@_fromdate='10-01-2013' , @_Todate='10-31-2013'
@obj_cursor = @cur OUTPUT
Fetch Next From @cur into @_Salary, @_PersonID,@Month
While @@fetch_status = 0
Begin
print @_Salary
print @_PersonID
print @Month
Fetch Next From cur into @_Salary, @_PersonID,@Month
End
Close @cur
Deallocate @cur