【问题标题】:Inserting stored procedure output into a new physical table with 2 additional columns将存储过程输出插入到具有 2 个附加列的新物理表中
【发布时间】:2015-11-04 13:46:52
【问题描述】:

感谢您的所有帮助。

我正在使用 SQL Server 2008 R2,我的要求是我需要将存储过程输出插入到数据库中的一个新表(不是临时表或虚拟表)中,其中包含 2 个额外的列而无需添加值。

【问题讨论】:

标签: sql stored-procedures sql-server-2008-r2


【解决方案1】:

这是一个例子;

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
......

【讨论】:

    【解决方案2】:

    创建一个将默认列作为输出的存储过程。 例如:

    创建过程 proc1 作为 开始 选择名称,database_id,1 作为 Col1,2 作为 Col2 FROM sys.databases 结尾; 走

    上述 SP 的输出将提供所需的 2 个附加列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多