[表格名称]一词附近的自动递增ID/语法不正确 [英] Auto Increment ID / Incorrect syntax near the word [table name]

查看:77
本文介绍了[表格名称]一词附近的自动递增ID/语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个ID应该自动增加的数据库表.
我将身份规范设置为是,并将其递增1"
我也把"SET IDENTITY_INSERT MyTable ON"放了,但还是有错误.
单词[表名]附近的语法不正确"

我怎样才能插入到表中,并且每次插入时make id都会自动增加?

Hi

I have database table where the id should increment automatically.
I set the "identity specification to yes and increment it by 1 "
also I put the "SET IDENTITY_INSERT MyTable ON " but I still have the error.
"Incorrect syntax near the word [table name]"

How I Can insert to the table and the make id increment automatically every time of insertion ?

推荐答案

SET IDENTITY_INSERT ON(http://msdn.microsoft.com/en-us/library/aa259221(v=sql.80).aspx [ ^ ])允许将显式值插入表的标识列中.
如果为OFF,则不能发出INSERT语句,这会影响身份字段.

如果您希望身份文件可以自己使用,只需从insert语句中忽略该文件即可.
假设您有以下模式:
SET IDENTITY_INSERT ON(http://msdn.microsoft.com/en-us/library/aa259221(v=sql.80).aspx[^]) allows explicit values to be inserted into the identity column of a table.
If it is OFF, you can not issue INSERT statements, that affect an identity filed.

If you want an identity filed to work on it''s own, just omit that filed from the insert statement.
Let''s suppose, you have this schema:
CREATE TABLE [dbo].[students](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
 CONSTRAINT [PK_students] PRIMARY KEY ([id] ASC)
)


比您可以使用以下DML语句:


Than you can use this DML statement:

insert into students(name) values('John');


...,服务器将在[id]字段中添加下一个可用值.


...and the server will add the next available value to the [id] field.


报价:

创建表人员
(
P_Id int主键标识,
姓氏varchar(255)NOT NULL,
名字varchar(255),
地址varchar(255),
城市varchar(255)
)

CREATE TABLE Persons
(
P_Id int PRIMARY KEY IDENTITY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)


这篇关于[表格名称]一词附近的自动递增ID/语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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