Though I have dropped the temporary table, it does not let me select the new column names if the temporary table with same table name with additional column names will be created.
I have been using SQL Server 2008.
Try this code for instance:
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
This says:
Msg 207, Level 16, State 1, Line 13
Invalid column name 'd'.
Again, if we try like selecting *
as mentioned by Martin Parkin, it gives result with column name d
.
I could not select *
for my dynamic columns and it doesn't let me to select additional columns.
Why?
You need GO
to separate DROP
and CREATE
statements. They need to be in separate batches.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.