Sql server sql server:将存储过程存储到带有变量值的临时表中

Sql server sql server:将存储过程存储到带有变量值的临时表中,sql-server,tsql,stored-procedures,Sql Server,Tsql,Stored Procedures,我有一张临时桌 我的存储过程SP1返回3列值 我可以使用: 填充@MyTmpTable,但是col1值总是空的,有什么方法可以这样做吗? 有人能帮忙吗?如果您的col1始终为空,您可以使用默认值创建它,以满足您的工作需求。请参阅下面的演示 create table #test ( id int default null, id1 int, id2 int ) create proc usp_test as begin select rand()*10,rand()*20 end in

我有一张临时桌

我的存储过程SP1返回3列值

我可以使用:

填充@MyTmpTable,但是col1值总是空的,有什么方法可以这样做吗?


有人能帮忙吗?

如果您的col1始终为空,您可以使用默认值创建它,以满足您的工作需求。请参阅下面的演示

create table #test
(
id int default null,
id1 int,
id2 int
)


create proc usp_test
as
begin
select rand()*10,rand()*20
end

insert into #test
(id1,id2)
exec usp_test

您可以在插入后更新col1:

update @MyTmpTable set col1 = 'myvalue' where col1 is null;

事实上,默认的null是毫无意义的,我会使用一个实际值…这不是一个临时表。这是一个表变量。有区别。这正是我想要的,谢谢!我不熟悉TSQL,我使用了PSQL,在这一点上,我认为PSQL更有效,我可以像使用另一个表一样使用SP,即使使用连接
declare @var1 varchar(10) = 'myvalue';
insert into @MyTable(col2, col3, col4) @var1, exec SP1
create table #test
(
id int default null,
id1 int,
id2 int
)


create proc usp_test
as
begin
select rand()*10,rand()*20
end

insert into #test
(id1,id2)
exec usp_test
update @MyTmpTable set col1 = 'myvalue' where col1 is null;