简体   繁体   中英

SQL Server : Insert two stored procedure results into one table

I have two stored procedure sp1 and sp2

sp1 returns results

value1 
------
   1    
   2
   3
   4
   5

sp2 returns results

value2
------
   4    
   5
   6
   7
   8

I have a table called test that has two columns value1 and value2 , how to insert sp1 result in value1 column and sp2 result in value2 column in test table?

I am using this

  insert into test 
     exec [sp1], exec [sp2]

It is causing an error but it is working for single value please click following link

The only way I can think of is the following:

declare @t1 as table (id int identity(1,1), val int);
declare @t2 as table (id int identity(1,1), val int);

insert into @t1 (val)
    exec sp1;

insert into @t2 (val)
    exec sp2;

insert into test
    select t1.val, t2.val
    from @t1 t1 full outer join
         @t2 t2
         on t1.id = t2.id

If these are functions you can do the following, pseudocode:

first_val = select sp1();  
second_val = select sp2();


insert into test values (first_val,second_val);

This may work as well:

insert into test values (select sp1(),select sp2());

You cannot use procedures as there is no way to return a value from a procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM