将记录插入到具有标识列的表中的 SQL 语句? [英] SQL statement to insert record into table that has identity column?

查看:37
本文介绍了将记录插入到具有标识列的表中的 SQL 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一行手动插入到我的 SQL Server 数据表中.该表有 5 列,一个标识和四个数据列.我看了这篇文章,但是当我运行所选答案的查询时(当然是在用我的表名替换GroupTable"之后),我的 SQL Server 2005 管理工作室崩溃了.

I am attempting to insert a row manually into my SQL Server data table. This table has 5 columns, one identity and four data columns. I looked at this post, but when I run the selected answer's query (after replacing 'GroupTable' with my table name of course), my SQL Server 2005 management studio crashes.

我尝试了以下语句的变体,但显然到目前为止没有运气:

I have tried variations of the following statements, but obviously no luck thus far:

INSERT INTO MyTable (id, col1, col2, col3, col4)
VALUES (0, 'Column 1 Value', 'Column 2 Value', 'Column 3 Value', 'Column 4 Value')

INSERT INTO MyTable (col1, col2, col3, col4)
VALUES ('Column 1 Value', 'Column 2 Value', 'Column 3 Value', 'Column 4 Value')

任何指针将不胜感激.

推荐答案

通常,如果列设置为这样,则省略 id 列会生成新的自动 ID.在您的情况下,您可以使用

Usually, omitting the id column would generate a new automatic ID if the column is set so. In your case, you can use

SET IDENTITY_INSERT MyTable ON;

INSERT INTO MyTable (id, col1, col2, col3, col4)
VALUES (4567, 'Value1', 'Value2', 'Value3', 'Value4');

SET IDENTITY_INSERT MyTable OFF;

这篇关于将记录插入到具有标识列的表中的 SQL 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆