Tuesday, August 9, 2011

INSERT INTO SYNTAX IN SQL SERVER 2005

The INSERT INTO statement is used to insert new records in a table

There are two ways of using INSERT INTO statement

1. The first form doesn't specify the column names where the data will be inserted, only their values

INSERT INTO table-mane
VALUES ('value1' , 'value2')

2. The second form specifies both the column names and the values to be inserted:

INSERT INTO Table-name (column1,column2,column3)
VALUES ('Value1' , 'Value2' , 'Value3')

Lets say you have the following table name (mytable),


 now you want to insert new record in all columns of the table So the syntax would be

INSERT INTO mytable
values ( 'Khurram' , 'Kazmi' , 'Karachi')

The result of this syntax would be


In this syntax we insert in all three columns, what if we want to insert information in only 2 columns ??

For this we will use the 2nd syntax

INSERT INTO mytable (firstName , LastName)
values ('Mehdi' , 'Rehmani')

By using this syntax we will get the following output


Make sure that the column (city) is allowed to be null, otherwise you will  receive an error

No comments:

Post a Comment