As a simple example, suppose I have the following stored procedure in my SQL Server database that produces an output:
CREATE PROCEDURE MyProc
(@Id INT,
@Var1 VARCHAR(100))
AS
BEGIN
SELECT
Id = @Id,
Var1 = @Var1,
Var2 = LTRIM(@Id) + ' - ' + @Var1
END
Now, what I would like to do is have the output from this stored procedure inserted into a table with other columns such as:
DECLARE @NewTable TABLE
(
ExtraCols INT,
Id INT,
Var1 VARCHAR(100),
Var2 VARCHAR(MAX)
)
So, I would like to do something along the lines of:
DECLARE @RunVars TABLE (ExtraCols INT, Id INT, Var1 VARCHAR(100))
INSERT INTO @RunVars (ExtraCols, Id, Var1)
VALUES (123, 1, 'Test'),
(456, 2, 'Test 1')
Then, after running each row of @RunVars through the stored procedure, to end up with:
@NewTable:
ExtraCols Id Var1 Var2
---------------------------------------------
123 1 'Test' '1 - Test'
456 2 'Test 1' '2 - Test 1'
I know I could create a temporary table to get the data from the stored procedure and then do a join myself, but I'm wondering if there's any better way to accomplish this - kind of a join or cross apply to the stored procedure.
SELECT...INTO...in your procedure?