sql server:将存储过程转换为临时表WITH变量值

时间:2017-03-16 14:30:40

标签: sql-server tsql stored-procedures

我有一张临时表

@MyTmpTable(col1, col2, col3, col4)

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

我可以用:

insert into @MyTmpTable (col2, col3, col4) exec SP1

填充@MyTmpTablecol1值始终为null,有什么方法可以做到这样吗?

declare @var1 varchar(10) = 'myvalue';
insert into @MyTable(col2, col3, col4) @var1, exec SP1

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您可以在插入后更新col1:

update @MyTmpTable set col1 = 'myvalue' where col1 is null;
相关问题