declare @temp table
(
ItemID varchar(100),
)
INSERT INTO @temp (ItemID, Qt)
EXEC A 'LA'
I want to then take the temp table results in field1 (will show field1 and field2) and loop those results using stored procedure B.
declare @temp table
(
ItemID varchar(100),
)
INSERT INTO @temp (ItemID, Qt)
EXEC A 'LA'
I want to then take the temp table results in field1 (will show field1 and field2) and loop those results using stored procedure B.
Figured I'd provide an example (I was 99% sure my comment was correct but thought I'd double-check.
CREATE PROC dbo.A @x varchar(10) AS SELECT @x FROM (VALUES (1),(1),(1))x(x);
CREATE PROC dbo.B AS SELECT * FROM ##xx;
IF object_id('tempdb..##xx') IS NOT NULL DROP TABLE ##xx;
CREATE TABLE ##xx (col1 varchar(10));
INSERT ##xx EXEC dbo.A 'hi';
EXEC dbo.B;
Results:
col1
------
hi
hi
hi
You could create a User-Defined Table Type, declare a variable as that type (instead of declaring a table variable) and send that over to a procedure which is expecting that type - as a READONLY
.
For example:
CREATE TYPE SomeTableType AS TABLE(
[Id] INT,
[Val] VARCHAR(10)
)
Create a procedure that takes this type as a READONLY
parameter:
CREATE PROCEDURE p_some_procedure_name
@table SomeTableType READONLY
AS
SELECT * FROM @table;
Now you can declare an instance of your type anywhere (say another procedure), populate it with data and call p_some_procedure_name
with it:
DECLARE @temp_table SomeTableType;
INSERT @temp_table(Id, Val) VALUES (1, 'First'), (2, 'Second');
EXEC p_some_procedure_name @temp_table;
If possible (which is often the case), avoid looping.