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.
Introduction
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.
Syntax
insert into Table(column1, column2….)
select TOP (N) column1, column2…..
from Table1
or
insert TOP (N) into Table (column 1, column2….) select column 1, column2…. from Table1
|
where N represents number of records.
Example
Firstly, create a table named Employee having
columns (ID, F_Name, L_Name, Salary, Region and City) and insert some values.
Create table
Employee(
ID int NOT NULL
PRIMARY KEY,
F_Name varchar(20)
NULL,
L_Name varchar(20)
NULL,
Salary decimal(10,2)
NULL,
Region varchar(10)
NULL,
City
varchar(20)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.

OUTPUT:
