Additional SQL Server features and topics not covered by specific categories
Yes, it is possible to pass a Memory-Optimized Table Variable to a child proc and update the table in the child proc. However, the table variable must be declared as READONLY in the child proc. If you want to manipulate the table in the child proc, you can create a new memory-optimized table variable in the child proc and insert the contents of the READONLY table variable into it. You can then manipulate the new table variable and return it to the parent proc. Here is an example of how you can modify your code to achieve this:
CREATE OR ALTER PROCEDURE dbo.ParentSproc
AS
BEGIN
DECLARE @mytablevariable AS CustomerType;
INSERT INTO @mytablevariable VALUES (1, 'Bob', '11/10/2023'), (2, 'Sally', '11/11/2023'), (3, 'John', '11/12/2023');
DECLARE @newtablevariable AS CustomerType;
INSERT INTO @newtablevariable SELECT * FROM @mytablevariable;
EXEC dbo.ChildSproc @newtablevariable OUTPUT;
SELECT * FROM @newtablevariable;
END;
GO
CREATE OR ALTER PROCEDURE dbo.ChildSproc
@mytablevariable AS CustomerType READONLY,
@newtablevariable AS CustomerType OUTPUT
AS
BEGIN
INSERT INTO @newtablevariable SELECT * FROM @mytablevariable;
UPDATE @newtablevariable
SET CustomerName = 'Banana'
WHERE CustomerID = 1;
END;
GO
DECLARE @mytablevariable AS CustomerType;
INSERT INTO @mytablevariable VALUES (1, 'Bob', '11/10/2023'), (2, 'Sally', '11/11/2023'), (3, 'John', '11/12/2023');
DECLARE @newtablevariable AS CustomerType;
INSERT INTO @newtablevariable SELECT * FROM @mytablevariable;
EXEC dbo.ChildSproc @newtablevariable OUTPUT;
SELECT * FROM @newtablevariable;
In the modified code, the parent proc creates a new memory-optimized table variable @newtablevariable and inserts the contents of @mytablevariable into it. The parent proc then calls the child proc with @newtablevariable as an OUTPUT parameter. The child proc inserts the contents of @mytablevariable into a new memory-optimized table variable @newtablevariable, manipulates @newtablevariable, and returns it to the parent proc. The parent proc then selects and returns the contents of @newtablevariable.
References: