SQL Server 2008--sql Command insert

Source: Internet
Author: User

Syntax for the insert command for T-sql:

INSERT [into]

{Table_name|view_name}

[{(Column_name,column_name,...)}]

{VALUES (expression,expression,...)}

In square brackets is optional.

The list of column names must be enclosed in parentheses, separated by commas.

The keyword values are required, and the data values are surrounded by parentheses, separated by commas.

It is best to avoid including spaces in the name;

If the name of the table or view is the same as the reserved word or contains spaces, you must enclose the name in square brackets or double quotation marks .

However, if you do, it is best to use square brackets, because sometimes you want to set the column data value to resemble the value of Acme's rockets, where you enclose Acme's rockets in double quotes to facilitate the addition of data, as follows:

VALUES

(...

"Acme ' s Rockets",

... )

When enclosing a table or view name with double quotation marks instead of square brackets, the reference identifier is used, that is, when the database is created, the following settings are used:

SET QUOTED_IDENTIFIER ON

If the database has set QUOTED_IDENTIFIER ON, you cannot enclose data values like Acme's rockets with double quotes.

To ensure that the Query Editor is set to allow double quotation marks to separate strings, you can place the following code at any query that requires quotation marks:

SET QUOTED_IDENTIFIER OFF

GO

Note that using SET QUOTED_IDENTIFIER off has a hidden problem: when using T-SQL commands, it is often possible to enclose reserved words in double quotation marks, rather than square brackets, but if QUOTED_IDENTIFIER is set to OFF, You can only enclose reserved words in square brackets. If you set QUOTED_IDENTIFIER to ON, you cannot enter a string similar to Acme's in the name, but the single quotation mark in the code must be represented by two single quotation marks:

VALUES

(...

' ACME ' s rockets ',

... )

If the column is not specified in the INSERT statement, the number of data values provided must be the same as the number of columns in the table to be inserted, and if a list of columns is specified, the number of data values must be the same as the number of columns in the specified list in the INSERT statement.

It is recommended that you always indicate column names, even if you insert data into all the columns in the table in the order in which they are laid out in the table.

Ability to insert multiple rows of data from an INSERT statement, you can do this by enclosing each line that you want to add with its own separate pair of parentheses, and separating the parentheses with commas, as follows:

VALUES

         (...

          Acme ' s rockets,

          ... ),

         (...

          Tom ' s rockets,

          ... ),

          ...

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.