October 24, 2011

Add more columns in SELECT * INTO

Today I faced a very strange situation in SELECT * INTO.
I was trying to insert all records into another table but i also wanted to
add some column in derived table.

Let’ try to do it.
CREATE TABLE [dbo].[TableB]
(
    [ID] [int] NULL,
    [Val] [char](4) NULL
) ON [PRIMARY]
SELECT *
,CAST(Null AS DATETIME) AS CurrDate
,CAST(Null AS NVARCHAR(100)) AS Details
,CAST(Null AS INT) AS TCount 
INTO DerivedTable   
FROM 
TableB 
CREATE TABLE [dbo].[DerivedTable](
    [ID] [int] NULL,
    [Val] [char](4) NULL,
    [CurrDate] [datetime] NULL,
    [Details] [nvarchar](100) NULL,
    [TCount] [int] NULL
) ON [PRIMARY]
GO

That’s It.
Enjoy Learning.

No comments:

Post a Comment