Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

INSERT with SELECT vs INSERT with VALUES

3.67/5 (3 votes)
29 Nov 2010CPOL 24.4K  
INSERT with SELECT vs INSERT with VALUES
Assume we have a table Emp (EmpName VARCHAR(50), Age INT)

Query 1:

SQL
INSERT INTO Emp(EmpName, Age)
SELECT 'Ravi Kiran', 36


Query 2:

SQL
INSERT INTO Emp(EmpName, Age)
VALUES ('Ravi Kiran', 36)


Both the queries will achieve the same result. But which one is better? Run the below code:

SQL
SET NOCOUNT ON

PRINT CONVERT(VARCHAR, GETDATE(), 121)

INSERT INTO Emp(EmpName, Age)
SELECT 'Ravi Kiran', 36

PRINT CONVERT(VARCHAR, GETDATE(), 121)

INSERT INTO Emp(EmpName, Age)
VALUES ('Ravi Kiran', 36)

PRINT CONVERT(VARCHAR, GETDATE(), 121)


The result is:

2010-11-23 23:04:08.617
2010-11-23 23:04:08.640
2010-11-23 23:04:08.640

The INSERT...SELECT took 23 milliseconds where as INSERT...VALUES took 0 milliseconds. Now you know!

So, if you want to insert a single record in a table, use INSERT...VALUES instead of INSERT...SELECT.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)