Free Web Development Online Tutorials, Learn to Code
How to write insert query in SQL, Learn SQL Database Development, MS SQL Tutorial Online

SQL Insert Query Example in SQL Server Database

Inserting new record to a database table

Here is the insert query syntax in SQL table

INSERT INTO TableName
(field1,field2, field3, field4 )
VALUES
    (value1, value2, value3,	value4)

While specifying values in insert query, if the value type is string, then you need to pass the value within single quote

Here is an practical example how your SQL query will look like

INSERT INTO [tbContactList]
(   [fullName]
    ,[email]
    ,[Organisation]
    ,[Address]
    ,[City]
    ,[lastNotified]        
)
VALUES
(  'Mr Alexa'
    ,'alexa@google.com'
    ,'Google INC'
    ,'USA'
    ,'Kolkata'
    ,getdate()
)
GO

Now if you notice in above query, for all value types we have used single quote, except date type

Insert Query using Select Statement

You also can write insert query using select statement, this type of insert statement are very useful when you want to pull data from one table to another and table structure match, you also can insert data from view to table, take a look at the example below

Insert into [dbo].[tbOldStudent]
SELECT [Firstname]
    ,[Lastname]
    ,[Email]
    ,[ContactNumber]
    ,[DOB]
FROM [dbo].[tbStudent]

Insert Query Failing Reason

Sometimes you may experience that insert query failing, Here are some of the reason to look at to find the root cause

  • If column data type and value data type is not same
  • If the value data length is more than specified in column

If you are inserting more than one table you may need to use Transaction

You should also read Primary Key and Foreign Key

Comment
Name
Email
Website
Subscribe