1

I want to insert data manually into temporary table from select statement:

  Select into #temp from (select 1,2
    Union 
    select 2,4 
    Union 
    Select 8,12) as b
0

1 Answer 1

4

You need to give columns a name (here I have named the columns a and b):

Select a, b into #temp 
from 
(
     select a = 1, b = 2 
     Union 
     select 2, 4 
     Union 
     Select 8, 12
) as t

select * from #temp

a   b
-----
1   2
2   4
8   12

Only the first SELECT clause of a UNION needs the explicit column names.

Sign up to request clarification or add additional context in comments.

4 Comments

Do i need to pass the columns a and b in the inner query if i will pass select 1,2 instead of select a=1,b=2 ?
I'm sure not what you mean. The above answers the question you asked.
@mahadevdhyani Mitch try to explain that a columnname is necessary. So he choose a and b. Otherwise you'll receive an Error "Server: Msg 8155 No column name was specified for column".
You can remove the subquery portion of this by moving the into to the first query. It is syntactically a little odd but perfectly valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.