如何从table1中选择前500行并将其插入到table2中? [英] How can I SELECT top 500 rows from table1 and INSERT them to table2?

查看:223
本文介绍了如何从table1中选择前500行并将其插入到table2中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了两个完全相同的表:t2和t1.

I have completely same defined 2 tables : t2 and t1.

t2有1000行,而t1完全为空.

t2 has 1000 rows and t1 is totally empty.

如何从t2中选择前500行并将其插入到t1中?

How can I SELECT top 500 rows from t2 and INSERT them to t1?

推荐答案

我将使用"emptyTable"和"populatedTable",因为问题令人困惑

I'll use "emptyTable" and "populatedTable" because the questions is confusing

重要 TOP给出500条任意行.一张桌子从不存在任何隐含或自然的顺序

Important TOP without an ORDER BY gives 500 arbitrary rows. There is never an implied or natural order to a table

INSERT INTO emptyTable
SELECT TOP 500 * 
FROM populatedTable
ORDER BY What -- need this to define TOP 500

此处缺少列列表通常被定义为不良做法,但仅在以下情况下有效

The lack of column list here is usually defined as bad practice but works only if

  • 表是相同的
  • emptyTable中没有IDENTITY列

ORDER BY是必需的以保证行顺序.看到这些.它也符合ANSI标准

ORDER BY is required to guarantee row order. See these. It's also in the ANSI standard

  • http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/05/20/without-order-by-there-is-no-default-sort-order.aspx
  • http://blogs.msdn.com/b/conor_cunningham_msft/archive/2008/08/27/no-seatbelt-expecting-order-without-order-by.aspx

这篇关于如何从table1中选择前500行并将其插入到table2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆