I am reading a tutorial about TOP keyword in T-SQL. But I don't really understand the examples given in the tutorial.
Example 1:
INSERT TOP n INTO table_name1 (Col1, Col2)
SELECT Col3, Col4 FROM table_name2;
The tutorial mentions that the TOP here doesn't really achieve anything since the database doesn't have a concept of order, which I kind of get.
And is it correct to assume that the n
rows that are chosen from table_name2 are completely random records?
Example 2:
The tutorial gives another example that improves the first statement.
INSERT INTO table_name1 (Col1, Col2)
SELECT TOP n Col3, Col4
FROM table_name2
Order By Col1;
Here is the confusing part. Putting the TOP keyword inside the SELECT statement is supposed to give us a better control over what rows get chosen and inserted through the ORDER BY clause. So I would assume the ORDER BY clause is part of the SELECT statement in the second example. But Col1 isn't part of table_name2.
This confused the hell out of me.
Thanks for any insight.
Best Answer
When an order isn't specified, it will usually use the sort fields and directions from the design of the primary key of
table_name2
, but is not guaranteed to. As Aaron Bertrand saidIt certainly wouldn't hurt to add your order clause in, even if you want them to return in the index order of the PK.
From the tutorial example
Gaaaah. That's horrible coding. That will order by
Col1 ASC
of table_name2, even though yourSELECT
clause isn't explicitly returning a Col1 from the second table. It is NOT ordered byCol3
of the 2nd table akaCol1
of the 1st table. The MS docs forINSERT INTO
do not list anORDER BY
clause - theORDER BY
is part of the inner SELECT clause.It's always better to explicitly specify which table you are inserting into and selecting from. E.g. this would be a slightly better example.
After this alteration there's no confusion about what
Col1
means (nor the sort order of it). I'd still recommend throwing in a sort order ofCol3
andCol4
if Col1 isn't unique for all entries intable_name2
, as demonstrated above. You cannot alias the table that records are inserted into (e.g.table_name1 AS t1 (t1.Col1, t1.Col2)
as that is not legal syntax for existing versions of SQL Server.Opinion-based recommendation: avoid the tutorial website that you found.