0

I have a stored procedure that gets data from different tables. I want a few fields from the result set of the stored procedure to be inserted on another table.

How can I do this? Cursor, another stored procedure or what?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user988122
  • 19
  • 1
  • 4
  • Can you clarify your question? You say that you want a few fields from the resultset inserted? Does that mean that you still want all the rows from the resultset? –  Dec 14 '11 at 23:29
  • No, the resultset gets 20 rows and I just want 15 of them to be inserted in the table – user988122 Dec 14 '11 at 23:33
  • But all the columns? How do you select which rows you don't want? –  Dec 14 '11 at 23:37
  • Well, that´s part of the problem. I just need a few rows of that resultset to be inserted on the table and how to do it – user988122 Dec 14 '11 at 23:50
  • I understand that - how would you describe - in code - which records to insert? What would distinguish these rows from the others? –  Dec 14 '11 at 23:51
  • By creating a temp table.I found this question http://stackoverflow.com/questions/6197844/inserting-updating-data-on-table-from-a-stored-procedure-result-set it´s very helpful but I don´t know how to do it when having fewer columns in the table. – user988122 Dec 15 '11 at 00:01

1 Answers1

3

You can insert the result set from a stored procedure into another table, as in this example from this article:

DECLARE @People TABLE
(
    ContactID INT,
    FirstName NVARCHAR(50),
    LastName NVARCHAR(50)
)

INSERT @People (ContactID, FirstName, LastName)
EXEC dbo.GetPeopleByLastName @LastName = 'Alexander'
abatishchev
  • 98,240
  • 88
  • 296
  • 433
mikurski
  • 1,353
  • 7
  • 20