Thursday, May 13, 2010

SQL SERVER – How To Insert a Record in a Table

We have seen the creation of a table in previous article. Once a table was created, the next step is insertion of new records in the table.

Today we will see how to insert a new record in an existing table. Here is the syntax for the simple Insert statement

Syntax :                                                                       INSERT                                                                        [INTO]                                                                        { <object>}{                                                                                 [(column_list )]                                                             {VALUES({DEFAULT|NULL|expression}                       |DEFAULT VALUES}

<object> ::=                                                               {                              [server_name.database_name.schema_name.                    |  database_name.schema_name ] .                                  |schema_name .                                                             ] table_or_view_name                                                      }

Now Let us see each of the Argument.

INTO

Is an optional keyword that can be used between INSERT and the target table.

server_name

Is the name of the linked server on which the table or view is located.

database_name

Is the name of the database.

schema_name

Is the name of the schema to which the table or view belongs.

table_or view_name

Is the name of the table or view that is to receive the data.

( column_list )

Is a list of one or more columns in which to insert data. column_list must be enclosed in parentheses and delimited by commas.

VALUES

Introduces the list of data values to be inserted.

DEFAULT VALUES

Forces the new row to contain the default values defined for each column.

Example :

Now let us see a simple example of inserting a record in the Employee table.

Create Table Employee
(EmpID Varchar(5),
EmpName Varchar(50),
DeptId Varchar(5),
Salary Bigint,
DateOfJoin Datetime Default getdate(),
MgrID Varchar(5) Null)             

Insert Into Employee
                             (EmpID,
                              EmpName,
                              DeptId,
                              Salary,
                              DateOfJoin,
                              MgrID                                                                                             )
                   Values( 1,'XXX',1,50000,Default,NULL)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.