HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

SELECT INTO creates a new table?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
newcreatesintoselecttable

Problem

I have an original table MessageQueue with six columns. When I try to do a SELECT INTO that table it creates a new (local) table called .MessageQueue with only three columns when I use the following code. What am I doing wrong?

declare 
    @TempTable table (idx smallint Primary Key IDENTITY(1,1), OneId int, OtherId int)

declare 
    @OneId int,
    @OtherId int,
    @date datetime = dbo.GetFloorDate(getdate()),
    @i int = 1

insert @TempTable select Id, OtherId from One where @date = (select dbo.GetSomeDate (Id))

select MessageId = 9999, OneId, OtherId into MessageQueue from @TempTable

select * from MessageQueue

Solution

Yes, SELECT..INTO creates a new table

You'd need this to add rows to an existing table

INSERT MessageQueue (MessageQueue, OneId, OtherId) 
SELECT MessageId = 9999, OneId, OtherId 
   from @TempTable

Code Snippets

INSERT MessageQueue (MessageQueue, OneId, OtherId) 
SELECT MessageId = 9999, OneId, OtherId 
   from @TempTable

Context

StackExchange Database Administrators Q#4805, answer score: 11

Revisions (0)

No revisions yet.