Skip to content Skip to sidebar Skip to footer

How To Copy Top 1000 Records From 7000 Records In Existing Table To Other New Table

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 recor

Solution 1:

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

Solution 2:

In SQL Server

SELECT top 1000*INTO newTableName
FROM oldTableName;

In MySQL

SELECT*INTO newTableName
FROM oldTableName Limit 1000;

Solution 3:

You can use ROW_NUMBER in a common table expression.

WITH CTE AS(
   SELECT Col1, Col2, Col3, RN =ROW_NUMBER() OVER (ORDERBY Col1)
   FROM dbo.TableA
)
INSERTINTO 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.

Post a Comment for "How To Copy Top 1000 Records From 7000 Records In Existing Table To Other New Table"