-2

I am very new to this. I have created this table and I had no problems doing it:

CREATE TABLE [dbo].[Urban_Rail] (
[Id]      INT           IDENTITY (1, 1) NOT NULL,
[Country] VARCHAR (50)  NOT NULL,
[City]    VARCHAR (50)  NOT NULL,
[Type]    VARCHAR (50) NOT NULL,
[Gauge]   NCHAR (10)    NULL,
[Year]    NCHAR (10)    NULL,
[Status]  VARCHAR (50)  NULL,
[Notes]   VARCHAR (500) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

Now I am trying to fill in few thousand rows of data using this statement:

INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Algiers','Metro','1435','2011','Open','');
INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Algiers','Tram','1435','2011','Open','');
INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Batna','Tram','','','Planned','');

But I get errors saying that the name of each column is not valid. That does not look true to me. I am wondering if it has something to do with the Id instead which is an auto increment identity which I have left without value in my query. What am I doing wrong here? Some help will be appreciated.

4

3 回答 3

1

从插入中删除 ID(因为它是一个身份),或者如果您希望特定 ID 在运行插入之前打开 Identity_Insert 并在之后再次关闭。

SET IDENTITY_INSERT [dbo].[Urban_Rail] ON 

INSERT INTO Urban_Rail (Id,Country,City,Type,Gauge,Year,Status,Notes) VALUES (1,'Algeria','Algiers','Metro','1435','2011','Open','');

SET IDENTITY_INSERT [dbo].[Urban_Rail] OFF
于 2013-10-09T16:12:38.667 回答
0

IDENTITY 列不应包含在 INSERT 语句中。它将自动填充下一个值。

于 2013-10-09T16:11:20.713 回答
0

“ID”列是一个标识列,由于数据库分配了它,因此不允许您插入它。将其从插入的列名和值列表中删除。

如果您确实需要插入标识列,您可以使用

SET IDENTITY_INSERT <table> ON

但我猜你不需要那个,因为你的 ID 看起来是空白的。

于 2013-10-09T16:12:35.153 回答