0

I have a temporary table

@MyTmpTable(col1, col2, col3, col4)

and my stored procedure SP1 returns 3 columns values

I can use:

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

to fill @MyTmpTable but col1 value will always be null, is there any way I can do something like this?

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

Can anyone help?

1
  • 1
    This is not a temporary table. It's a table variable. There are differences. Commented Mar 16, 2017 at 14:38

2 Answers 2

1

if your col1 is always null,you can create it with default value for your requirement to work .see below for demo

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
Sign up to request clarification or add additional context in comments.

1 Comment

actually, default null is quite meaningless, I would go with an actual value...
0

You could just update col1 after the insert:

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

1 Comment

That's exactly what I am looking for, thanks! I am not familiar with TSQL, I used PSQL, at this point, I think PSQL is more efficient, I can use SP just like another table, even with joins

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.