how to copy top 1000 records from 7000 records in existing table to other new table

亡梦爱人 提交于 2019-12-12 01:42:16

问题


I have a table A which consists more than 7k records,Now i am creating a new table B .In my new table B I need to copy only 1000 records from table A which has more than 7000 records.

No condition applies, it may be any thousand records from 7000 .


回答1:


In SQL Server

SELECT top 1000 *
INTO newTableName
FROM oldTableName;

In MySQL

SELECT *
INTO newTableName
FROM oldTableName Limit 1000;



回答2:


INSERT INTO TABLEB(Col1, Col2, .... colN)
    SELECT TOP 1000 Col1, Col2, .... colN FROM TABLEA



回答3:


You can use ROW_NUMBER in a common table expression.

WITH CTE AS(
   SELECT Col1, Col2, Col3, RN = ROW_NUMBER() OVER (ORDER BY Col1)
   FROM dbo.TableA
)
INSERT INTO dbo.TableB(Col1, Col2, Col3)
    SELECT Col1, Col2, Col3
    FROM CTE
    WHERE RN <= 1000

Then it's easy to change the logic what should be exported. You could change the ORDER BY, apply a PARTITION BY(f.e. to copy duplicates), use multiple ORDER BY comma separated or change the number you want to export.



来源:https://stackoverflow.com/questions/17088946/how-to-copy-top-1000-records-from-7000-records-in-existing-table-to-other-new-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!