blog post

The INSERT INTO statement is used to add new data to a database.
The INSERT INTO statement adds a new record to a table.
INSERT INTO can contain values for some or all of its columns.
INSERT INTO can be combined with a SELECT to insert records.

The SQL INSERT INTO syntax

The general syntax is:
INSERT INTO table-name (column-names) 
VALUES (values) 

SQL INSERT INTO Examples

Problem: Add a record for a new customer
INSERT INTO Customer (FirstName, LastName, City, Country, Phone)
VALUES ('Craig', 'Smith', 'New York', 'USA', 1-01-993 2800)

The SQL INSERT combined with a SELECT

The general syntax is:
INSERT INTO table-name (column-names) 
SELECT column-names
  FROM table-name

SQL INSERT INTO with SELECT Example

Problem: The Bigfoot Brewery supplier is also a customer.
Add a customer record with values from the supplier table.
INSERT INTO Customer (FirstName, LastName, City, Country, Phone)
SELECT LEFT(ContactName, CHARINDEX(' ',ContactName) - 1), 
       SUBSTRING(ContactName, CHARINDEX(' ',ContactName) + 1, 100), 
       City, Country, Phone
  FROM Supplier
 WHERE CompanyName = 'Bigfoot Breweries'

Note: ContactName is parsed into FirstName and LastName.
Parsing takes place with built-in functions: LEFT, SUBSTRING, and CHARINDEX. WHERE condition

  • Share This Story


Pleas give your valuable comments to improve the contents.

about author

Atul kumar shukla


I'm Atul Kumar Shukla, working as a web developer.
Welcome to my Blog. This is my personal blog where i post my own questions and answers , articles about ASP.Net., SQL Server, HTML, CSS, Jquery and many more..

Visit Profile

Leave a comment

Name
E-mail
Url
Message

Get More Stuff
Subscribing to the TheOneblogs newsletter and get intersting stuff and updates to your email inbox.
Your Email

 

Style Switcher

Change Color

×! We use cookies to improve your user experience. More information