Question

I currently have a query that runs n number of inserts into a temp table based on results from a previous query. The results put into the temp table come from a sproc.

I would like to also insert into the table, along with this information, a variable that is being used (but not used in the sproc). Example is this:

--somewhere up top
Declare @userID int --This is used to get info before the important part
Declare @someOtherID int --This is passed into the sproc

--Some stuff happens here that is unrelated to question

--Important part
while (@@rowcount > 0)
begin
  insert @tmp2 exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID
  delete from @tmp where [someID]=@someOtherID
  select top 1 @id=[id], @someOtherID=[someID] from @tmp
end

SELECT * FROM @tmp2

What I'd like to do is also insert into each row in @tmp2 the userID used in the previous section. Something like

insert @tmp2 @userID + exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID

Obviously, this won't work. However, it's the general idea of what I am trying to do.

Does anyone have any suggestions on this one?

(note that I'd prefer not to modify the sproc / make a similar one to also take that variable in just to pass it back out to the results if I can help it. Not impossible, just would prefer not to)

Was it helpful?

Solution

You can do it by an extra step (as you mentioned without any modification in sp)

insert @tmp3 exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID
insert @tmp2 Select @userId, field1, field2 from @temp3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top