Based on this post how to create temp table based on column number? I am able create temp table with name #VarTemp. But How can I pump the data from exec stored procedure? it is throwing error
Invalid object name ‘#VarTemp’
This is my code:
DECLARE @ColCount int = 20
DECLARE @Ct int = 1
DECLARE @SQL nvarchar(max) = ''
SET @SQL = 'CREATE TABLE #VarTemp('
WHILE @Ct < @ColCount+1
BEGIN
SET @SQL += 'Col' + CAST(@Ct as nvarchar(8)) + ' nvarchar(256),'
SET @Ct = @Ct + 1
END
SET @SQL = LEFT(@SQL, (LEN(@SQL) - 1))
SET @SQL += ')'
Exec (@SQL)
INSERT into #VarTemp EXEC sp_FindStringInTable 'Nareshbhai%', 'dbo', 'aspnet_Membership'
--- Get error Invalid object name '#VarTemp'.
Your temp table is scoped to the
EXECcommand… it will be automatically dropped as soon as that statement completes.If you dynamically build the temp table, you’ll have to insert into the temp table in the same dynamic sql statement… and you’ll also have to read from it in the same dynamic sql statement as well: