简体   繁体   中英

Inserting stored procedure output into a new physical table with 2 additional columns

Thanks for all your help.

I am using SQL Server 2008 R2, my requirement is I need to insert stored procedure output into a new table (not temp or virtual table) in database with 2 additional columns without values to be added.

This is an example;

create procedure proc_1
as begin
    SELECT name, database_id FROM sys.databases
end;
go

create table new_tbl (
    name varchar(150),
    id int,
    new_col1 int,
    new_col2 int
)
go

declare @tab table(
    name varchar(150),
    id int  
)
begin
    insert into @tab
    exec proc_1

    insert into new_tbl
    select name, id, 1, 2 from @tab;
end;
go

select * from new_tbl

OutPut:
master  1   1   2
tempdb  2   1   2
model   3   1   2
......

Create a stored procedure which will give default columns as output. for example:

CREATE Procedure proc1
AS 
BEGIN
    SELECT name, database_id,1 as Col1,2 as Col2
    FROM sys.databases
END;
go

output of above SP will provide the 2 aditional columns needed.

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