If you can change the stored procedure, you can easily put the number of required columns as a parameter:
CREATE PROCEDURE sp_GetDiffDataExample
@columnsStatement NVARCHAR(MAX)
AS
BEGIN
DECLARE @query NVARCHAR(MAX)
SET @query = N'SELECT ' + @columnsStatement + N' INTO ##TempTable FROM dbo.TestTable'
EXEC sp_executeSql @query
SELECT * FROM
DROP TABLE
END
In this case, you do not need to create a temporary table manually - it is created automatically. Hope this helps.
source
share