How To Insert Data in SQL Server Table
INSERT INTO Statement is used to insert new records in a database table.
Syntax:
INSERT INTO TableName(Col1,Col2,Col3,…ColN)
VALUES(Val1,Val2,Val3,…ValN)
Col1, Col2, Col3, … , ColN are column names up to N number of columns, and Val1, Val2, Val3, …, ValN are values for the table columns Col1, Col2, Col3, … , ColN respectively.
Before we insert records into a table, we need to create a table first. So, we will create a table tblEmployee, and then we insert records into that table.
CREATE TABLE tblEmployee
(
EmpID INT PRIMARY KEY,
EmpFirstName NVARCHAR(20),
EmpLastName NVARCHAR(20),
EmpAge INT,
EmpGender NVARCHAR(10),
EmpEmail NVARCHAR(20),
EmpSalary INT,
EmpJoiningDate DATETIME,
EmpCountry NVARCHAR(20),
EmpCity NVARCHAR(20)
)
GO
Let’s insert new records into a table tblEmployee using INSERT INTO statement.
INSERT INTO Without Specifying Column Names
If you want to insert records for all the columns of a table, then there is no need to specify column names, as shown below.
Note: If you have IDENTITY Column in a table, then you cannot skip column names, Column names are mandatory in case of IDENTITY Column
INSERT INTO tblEmployee VALUES (1,'john','dave',26,'male','john@gmail.com',26000,'2010-02-01','USA','Washington'); INSERT INTO tblEmployee VALUES (2,'kamran','akmal',35,'male','kami@gmail.com',25000,'2011-01-01','Pakistan','Lahore'); INSERT INTO tblEmployee VALUES (3,'umar','akmal',36,'male','umar@gmail.com',38000,'2015-01-01','Pakistan','Karachi');
Insert Multiple Rows With Single INSERT INTO Statement
Values for multiple rows can be inserted with single INSERT INTO Statement just by putting comma(,) between two rows values.
INSERT INTO tblEmployee ( EmpID,EmpFirstName,EmpLastName,EmpAge,EmpGender,EmpEmail,EmpSalary,EmpJoiningDate,EmpCountry,EmpCity ) VALUES (4,'hafeez','sheikh',25,'male','hafeez@gmail.com',25000,'2016-05-05','India','Mumbai'), (5,'amar','sidhu',36,'male','amar@gmail.com',25000,'2016-05-05','India','New Dehli'), (6,'simran','sidhu',25,'female','simran@gmail.com',33000,'2018-01-01','India','Mumbai')
INSERT INTO With Selected Columns
Values can be inserted into a table with selected columns. In the following example, we will insert rows with only EmpID, EmpFirstName, EmpGender, EmpEmail, and EmpSalary columns. NULL values will be inserted automatically for those columns for which we did not specify values.
INSERT INTO tblEmployee ( EmpID,EmpFirstName,EmpGender,EmpEmail,EmpSalary ) VALUES (7,'diyana','female','diyana@gmail.com',50000)
INSERT INTO With SELECT Statement
We can insert new records with SELECT Statement as shown below.
INSERT INTO tblEmployee ( EmpID,EmpFirstName,EmpLastName,EmpAge,EmpGender,EmpEmail,EmpSalary,EmpJoiningDate,EmpCountry,EmpCity ) SELECT 8,'karishma','rai',27,'female','krai@gmail.com',70000,'2004-01-01','UK','London'
Recommended Readings
- Advanced SQL Queries For Practice With Solution
- SQL Queries For Practice With Solution
- SQL Interview Questions and Answers
- STORED PROCEDURE in SQL Server
- How To Join Tables Data in SQL Server
- How to use Transaction in SQL Stored Procedure
- Difference Between IN and NOT IN Operators in SQL
- How To Modify Date in SQL Using DATEADD
- How To Get Year From Date in SQL Server
- How To Get Month From Date in SQL Server
- How To Get Day From Date in SQL Server
- How To Use ROW_NUMBER Function in SQL
- Date and Time Functions in SQL Server
- How To Find Nth Highest Salary in SQL Server
- How to Backup Table Using SELECT INTO Statement
- How To Use HAVING Clause in SQL Server
- Aggregate Functions in SQL Server
- How To Group Data Using Group By in SQL Server
- How To Truncate Table in SQL Server
- How To Delete Data From Table in SQL Server
- How To Update Table Data in SQL Server
- How To Sort Data Using Order By Clause in SQL
- How To Select Distinct Records in SQL Server
- How to Filter Data From Table in SQL Server
- Round Off Values Using Ceiling and Floor in SQL
- How To Find Square Root Of A Number in SQL Server
- How To Select Data From Table in SQL Server
- How To Insert Data in SQL Server Table
- How To Add NOT NULL Constraint in SQL Server
- How To Add Check Constraint on SQL Server Table
- How To Add Default Constraint on SQL Server Table
- Unique Key Constraint in SQL Server
- How to add Foreign Key Constraint in SQL Server
- How To Add Identity To SQL Server Table Column
- How to add Primary Key Constraint in SQL Server
- How To Create Alter and Drop Table in SQL Server
- How To Create Alter and Drop Database in SQL