Jameson
Jameson

Reputation: 31

Loop through SQL results and insert them into a temporary table

I'm writing a stored procedure. I'll explain my problem with some sample code below.

-- Create a temp table 
CREATE TABLE #FoundRequests 
(
    RequestID INT
);

SELECT RequestID 
FROM Requests 
-- Don't worry about the WHERE clause for now

Which returns a result set:

| ID   | RequestID |
+------+-----------+
| 1    | 824       |
| 2    | 922       |
| 3    | 954       |

How would I get reach RequestID from this result set and insert each found value into my temp #FoundRequests table?

I tried different things like STUFF() and SPLIT(@ID_VALUE , '') but I can't get it to work.

Upvotes: 0

Views: 1023

Answers (1)

Stivan
Stivan

Reputation: 1127

You could insert all records from Requests table into #FoundRequests with the following query:

insert into #FoundRequests(
     RequestID INT
)
select RequestID from Requests;

If needed you can also add where clause into your select to filter results.

Upvotes: 1

Related Questions