2017-03-16 121 views
0

我有一个临时表 的SQL Server:存储过程到临时表变量的值

@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 

任何人都可以帮忙吗?

+1

这不是临时表格。这是一个表变量。有差异。 –

回答

0

你可以只插入后更新COL1:

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

这正是我所要找的,谢谢!我不熟悉TSQL,我使用PSQL,在这一点上,我认为PSQL更有效率,我可以像使用另一个表一样使用SP,即使使用连接 – supermyz

1

如果你的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 
+2

实际上,'default null'是相当无意义的,我会用一个实际的值... –

相关问题