It is not possible to select additional column names if the popup temporary table is created again with additional column names

Although I lost the temporary table, it does not allow me to select new column names if I create a temporary table with the same table name with additional column names.

I am using SQL Server 2008.

Try this code, for example:

IF (object_id('tempdb..#xyz') IS NOT NULL)
    DROP TABLE #xyz;

CREATE TABLE #xyz(
     [a] [datetime] NULL,
     [b] [nvarchar](255) NULL,
     [c] [nvarchar](255) NULL
) ON [PRIMARY]
GO

IF (object_id('tempdb..#xyz') IS NOT NULL)
    DROP TABLE #xyz;

CREATE TABLE #xyz(
     [a] [datetime] NULL,
     [b] [nvarchar](255) NULL,
     [c] [nvarchar](255) NULL,
     [d] [nvarchar](255) NULL
) ON [PRIMARY]

SELECT [d] FROM  #xyz

It says:

Msg 207, Level 16, State 1, Line 13
Invalid column name 'd'.

Again, if we try to select *, as Martin Parkin mentioned, he gives the result with the column name d.

I could not select *for my dynamic columns, and it does not allow me to select additional columns.

Why?

Fiddle

+3
source share
1 answer

GO DROP CREATE. .

IF (object_id('tempdb..#xyz') IS NOT NULL)
DROP TABLE #xyz;

CREATE TABLE #xyz(
     [a] [datetime] NULL,
     [b] [nvarchar](255) NULL,
     [c] [nvarchar](255) NULL
) ON [PRIMARY]
GO

IF (object_id('tempdb..#xyz') IS NOT NULL)
DROP TABLE #xyz;

GO

CREATE TABLE #xyz(
     [a] [datetime] NULL,
     [b] [nvarchar](255) NULL,
     [c] [nvarchar](255) NULL,
     [d] [nvarchar](255) NULL
) ON [PRIMARY]

SELECT [d] FROM  #xyz
+2

All Articles