BruceyBandit
BruceyBandit

Reputation: 4334

select into to insert into

In TSQL, I am modifying an existing code and have a simple question that I just need to make certain, I am change a select into statement into an insert into statement like below:

INSERT INTO #listToProcess (CurrentSeatID, CurrentYGrid)
SELECT DISTINCT @CurrentSeatID'CurrentSeatID',
                @CurrentYGrid'CurrentYGrid'

I'm just wondering that as I mentioned the column names in the insert into, can I remove those existing column names mentioned in the '' marks or do they need to be left in for another purpose?

Upvotes: 0

Views: 82

Answers (1)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

They are not required and can be avoided even if insert has many columns, if you write your query like this:

INSERT INTO #tmp (
   col1, col2, col3,                                          -- #1
   col4,                                                      -- #2
   col5, col6, col7                                           -- #3
)
SELECT value1, value2, value3                                 -- #1
   some_complex_expression_that_will_take_one_or_more_lines,  -- #2
   value5, value6, value7                                     -- #3
FROM some_table 

Of course, this is just a trick to easily spot mapping between columns and values. You can do whatever fits you.

Upvotes: 1

Related Questions