SQL Server:从存储过程中捕获临时表,该表调用其他存储过程

时间:2019-05-14 08:47:06

标签: sql-server stored-procedures

我有一个存储过程,该存储过程采用一个参数,并将其传递给其中的存储过程。从这些存储过程输出的角度来看,结果集是一个临时表。存储过程运行良好,但我无法捕获其结果集:

    const document = () => new Promise((resolve: any, reject: any) => {
      this.database.find({ id: payload.user.id }).sort({ postedAt: 1 }).limit(1).exec((error: any, document: any) => {
        (error) ? reject(error) : resolve(document);
      });
    }) 

    const x = await document().then((results: any) => {
      console.log('Results: ', results);
      return results;
    }).catch((error: any) => {
      console.log('Error: ', error);
      return false;
    });

    console.log('Doc: ', x);

我试图以以下方式捕获结果集,但得到一个空表:

use mydb
go

create procedure sp_myproc (@refdate date)
as
begin
    declare @temptable table 
                       (
                            field1 varchar(50), 
                            field2 varchar(50), 
                            field3 varchar(50), 
                            field4 varchar(30), 
                            field5 numeric(20, 0)
                       )

    exec sp_firstsp @refdate 
    exec sp_secondsp @refdate
    exec sp_thirdsp @refdate

    exec sp_fourthsp @refdate
    exec sp_fifthsp

    insert into @temptable  
        select field1, field2, field3, field4, field5 
        from view_into_results_of_abovesps 

    -- return @temptable (error: Must declare the scalar variable "@temptable".)
end
go 

谢谢

1 个答案:

答案 0 :(得分:2)

use mydb
go

create procedure sp_myproc (@refdate date)
as
begin
declare @temptable table 
                   (
                        field1 varchar(50), 
                        field2 varchar(50), 
                        field3 varchar(50), 
                        field4 varchar(30), 
                        field5 numeric(20, 0)
                   )

exec sp_firstsp @refdate 
exec sp_secondsp @refdate
exec sp_thirdsp @refdate

exec sp_fourthsp @refdate
exec sp_fifthsp

insert into @temptable  
    select field1, field2, field3, field4, field5 
    from view_into_results_of_abovesps 

--USE THIS
select * from @temptable

end