I have a dynamic query which I call and I put the Result set in a variable table
INSERT INTO @outTable
EXEC sp_executesql @query;
The problem is that @outTable should change When adding column in @query - @query is another stored procedure.
I tried to use a CTE (WITH), but it does not work with EXEC.
Is this possible do to in T-SQL?
Something like this
DECLARE @outTable TABLE (
ID int,
Record_ID int,
Order_ID nchar(16),
...and around 30 columns
);
SET @query = N'EXEC [OrderDep].[Order_Find] @FreeWhere =N'' WHERE '+ @Where +'''';
BEGIN TRY
INSERT INTO @outTable
EXEC sp_executesql @query;
END TRY
BEGIN CATCH