I want to insert dynamic record in temporary table. I don't want to declare this table. I want its columns to be defined at run time. I am doing this because definition of table "Contract" may change in future. So temporary table(#x) should be changed accordingly. And this will be in a Stored Procedure.
I have tried following ways(which were marked as answers, none works though)
EXECUTE IMMEDIATE
'SELECT top 1 * FROM Contract'
INTO #x
select * from #x
declare @arj varchar(100)
set @arj= 'SELECT top 1 * FROM Contract'
select * into #x from
(SELECT top 1 * FROM Contract)
declare @arj varchar(100)
set @arj= 'SELECT top 1 * FROM Contract'
SELECT * into #x execute ('execute' +
--and this-- SELECT into #T1 execute ('execute ' + @SQLString ) @arj )
SELECT *from #x
This syntax should work but you need to create the sql dynamically and then execute it:
SELECT top 1 *
INTO #x
FROM Contract
Example:
SELECT TOP 0 * INTO #x from Contract
DECLARE @ARJ NVARCHAR(MAX)
SET @ARJ='INSERT INTO #x SELECT TOP 1 * FROM Contract'
EXECUTE sp_executesql @ARJ