I wanted to do an insert into with a union, and someone suggested this:
SELECT x INTO ##temp
FROM (SELECT x FROM y UNION ALL SELECT x FROM z) UN
It works, but what is the UN? Unfortunately, Googling for “t-sql un” isn’t very helpful :p
Note: I found out that you can just do SELECT x INTO ##temp FROM y UNION ALL SELECT x FROM b but I’m still curious about UN.
EDIT: Ok, so it’s an alias, but why is it required to make this work? If I remove it, it won’t execute.
It’s not a keyword. It’s an alias. Any string could have been used there instead of “UN”.
In more complete form, it is:
@Tomalak is correct. In this case, the alias is required. Without the alias, the error is:
I simplified the query a bit and used the AdventureWorks database:
this receives the error above. In contrast:
works just fine. An alternative is