SQL Server series: INSERT statement

Source: Internet
Author: User

1. Insert Syntax

[With <common_table_expression> [,... n] ]INSERT {        [TOP (expression) [PERCENT] ]         [ into]         { <Object> |rowset_function_limited[With (<Table_Hint_Limited> [... n] ) ]        }    {        [(column_list)]         [<output clause>]        { VALUES( {DEFAULT | NULL |Expression}[,... N])[,... N]         |derived_table|execute_statement| <Dml_table_source>        | DEFAULT VALUES         }    }}[;]

In this structure, insert is the actual operation of the statement, the INTO keyword has no real meaning, the purpose is to enhance the readability of this statement. The INTO keyword is optional and it is recommended that you include the keyword in the statement. When you add data using the Entity Framework, the INSERT statement you execute does not use the INTO keyword.

2. Single insert

INSERT  into [dbo].[Product]    ([ProductName],[UnitPrice],[CreateDate])VALUES     ('LINQ to SQL', -,GETDATE());

3. Multiple Inserts

  SQL Server 2012 supports inserting multiple rows of records one at a time, by adding additional comma-delimited insertion values when needed.

INSERT  into [dbo].[Product]    ([ProductName],[UnitPrice],[CreateDate])VALUES     ('LINQ to SQL', -,GETDATE()),    ('LINQ to Object', -,GETDATE());

4. INSERT into ... SELECT statement

When inserting multiple records at one time and the data that needs to be inserted is selected from another data source, you can use inert into ... SELECT statement.

The different data sources include:

◊ Another table in the database

◊ data table in another database on the same server

INERT into ... Select syntax:

INSERT  into <  table name > [<column list>] <  SELECT statement >

Example: inserting multiple records at a time from another database's data table as a data source

 UsePortalGOINSERT  into [dbo].[Product]    ([ProductName],[UnitPrice],[CreateDate])SELECT    [ProductName],[UnitPrice],[CreateDate] from     [Northwind].[dbo].[Product]GO

Example: Declaring a variable of type table, inserting more than one record at a time into a variable

 UsePortalGODECLARE @tbl TABLE(ProductNameVARCHAR( -)NULL, CreateDateDATETIME NULL)INSERT  into @tblSELECT    [ProductName],[CreateDate] from     [dbo].[Product]SELECT *  from @tblGO

5. References:

Http://msdn.microsoft.com/zh-cn/library/ms174335.aspx

SQL Server series: INSERT statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.