I have a stored procedure which evaluates something. I would like to call this procedure many times with different arguments and insert everything into one table. Calling this procedure is what I would like to automate.
Say I have two tables, one has some first names, the second table has last names. I would like to cross join these two tables to get all possible combinations of first and last names. And then I would like to call my procedure for every row of the joined table.
CREATE TABLE #FN
(
FirstName varchar(255),
);
CREATE TABLE #LN
(
LastName varchar(255),
);
INSERT INTO #FN VALUES ('Augustinus'), ('Blepharo'), ('Gripus')
INSERT INTO #LN VALUES ('Sylla'), ('Petreius'), ('Cerinthus')
SELECT *
INTO #NAMES
FROM #FN
CROSS JOIN #LN
And now I need to automate the following code, by going row for row from #NAMES, instead of manually typing the arguments.
CREATE TABLE #RESULT (Val VARCHAR(10))
INSERT INTO #RESULT
EXEC('EXEC #myprocedure @arg1=''Augustinus'', @arg2=''Sylla'';
EXEC #myprocedure @arg1=''Augustinus'', @arg2=''Petreius'';
...')