• Nice question. Not terribly hard, but interesting. It could have been made a bit harder by omitting the column list in the INSERT:

    INSERT INTO #ATable

    SELECT 2 AS col2, 1 AS col1, 3 AS col3

    For the record, I am not condoning the above as good practice. In my book, both omitting the column list of the INSERT statement and adding aliases to the SELECT list of an INSERT ... SELECT are bad practices.

    A final remark - I don't see how the version in the explanation, that uses comments instead of aliases, is any clearer than the original. The column names in the comments don't match reality, so these comments are obfuscating the code instead of clarifying it.

    Here's the way I write INSERT statements, to make it easier to say which expression in the SELECT list matches which column in the INSERT list:

    INSERT INTO #ATable

    (col1, col2, col3)

    SELECT 2, 1, 3;

    (Where I change the column positions based on the length of the column names and expressions in the SELECT list).

    I usually limit myself to three columns per line, because I don't like to scroll horizontally. So when there's a long column list, I use this style:

    INSERT INTO #BTable

    (col1, col2, col3,

    col4, col5, col6,

    col7, col8)

    SELECT 2, 1, 3,

    4, 5, 6,

    7, 8;


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/