Top Keyword With Insert Statement In SQL
In this article i will explain how to use the top keyword in a insert query to insert only the top few records.
Basically, most of us know about top, we can
use it in a select query to get the top element from the query. But TOP can also
be used with Insert, Delete and Update commands for the table as well. We will
see how we can use TOP with Insert.
insert into Table(column1, column2….)
select TOP (N) column1, column2…..
insert TOP (N) into Table (column 1, column2….)
select column 1, column2….
where N represents number of records.
Firstly, create a table named Employee having
columns (ID, F_Name, L_Name, Salary, Region and City) and insert some values.
ID int NOT NULL
And the same structure for.Employee1. We have
records in Employee table and we are retrieving the top data from it and
insert it into Employee1 table.
Employee table data
To insert only the top few records.