SQL Server 插入示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13386894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:06:59  来源:igfitidea点击:

SQL Server Insert Example

sqlsql-serverinsert

提问by n00b

I switch between Oracle and SQL Server occasionally, and often forget how to do some of the most trivial tasks in SQL Server. I want to manually insert a row of data into a SQL Server database table using SQL. What is the easiest way to do that?

我偶尔会在 Oracle 和 SQL Server 之间切换,并且经常忘记如何在 SQL Server 中完成一些最琐碎的任务。我想使用 SQL 手动将一行数据插入到 SQL Server 数据库表中。最简单的方法是什么?

For example, if I have a USERS table, with the columns of ID (number), FIRST_NAME, and LAST_NAME, what query do I use to insert a row into that table?

例如,如果我有一个 USERS 表,其中包含 ID(编号)、FIRST_NAME 和 LAST_NAME 列,我应该使用什么查询将行插入到该表中?

Also what syntax do I use if I want to insert multiple rows at a time?

如果我想一次插入多行,我还使用什么语法?

回答by n00b

To insert a single row of data:

插入单行数据:

INSERT INTO USERS
VALUES (1, 'Mike', 'Jones');

To do an insert on specific columns (as opposed to all of them) you must specify the columns you want to update.

要对特定列(而不是所有列)进行插入,您必须指定要更新的列。

INSERT INTO USERS (FIRST_NAME, LAST_NAME)
VALUES ('Stephen', 'Jiang');

To insert multiple rows of data in SQL Server 2008 or later:

在 SQL Server 2008 或更高版本中插入多行数据:

INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');

To insert multiple rows of data in earlier versions of SQL Server, use "UNION ALL" like so:

要在早期版本的 SQL Server 中插入多行数据,请使用“UNION ALL”,如下所示:

INSERT INTO USERS (FIRST_NAME, LAST_NAME)
SELECT 'James', 'Bond' UNION ALL
SELECT 'Miss', 'Moneypenny' UNION ALL
SELECT 'Raoul', 'Silva'

Note, the "INTO" keyword is optional in INSERT queries. Source and more advanced querying can be found here.

请注意,“INTO”关键字在 INSERT 查询中是可选的。可以在此处找到源代码和更高级的查询。

回答by RameezAli

Here are 4 ways to insert data into a table.

以下是将数据插入表的 4 种方法。

  1. Simple insertion when the table column sequence is known.

    INSERT INTO Table1 VALUES (1,2,...)

  2. Simple insertion into specified columns of the table.

    INSERT INTO Table1(col2,col4) VALUES (1,2)

  3. Bulk insertion when...

    1. You wish to insert every column of Table2 into Table1
    2. You know the column sequence of Table2
    3. You are certain that the column sequence of Table2 won't change while this statement is being used (perhaps you the statement will only be used once).

    INSERT INTO Table1 {Column sequence} SELECT * FROM Table2

  4. Bulk insertion of selected data into specified columns of Table2.

  1. 表列顺序已知时的简单插入。

    INSERT INTO Table1 VALUES (1,2,...)

  2. 简单插入到表的指定列中。

    INSERT INTO Table1(col2,col4) VALUES (1,2)

  3. 批量插入时...

    1. 您希望将 Table2 的每一列插入到 Table1 中
    2. 你知道表2的列顺序
    3. 您确定在使用此语句时 Table2 的列顺序不会更改(也许您将只使用该语句一次)。

    INSERT INTO Table1 {Column sequence} SELECT * FROM Table2

  4. 将所选数据批量插入到 Table2 的指定列中。

.

.

INSERT INTO Table1 (Column1,Column2 ....)
    SELECT Column1,Column2...
       FROM Table2

回答by Abhishek

I hope this will help you

我希望这能帮到您

Create table :

创建表:

create table users (id int,first_name varchar(10),last_name varchar(10));

Insert values into the table :

将值插入表中:

insert into users (id,first_name,last_name) values(1,'Abhishek','Anand');