简体   繁体   English

SQL Server,使用存储过程插入数据

[英]SQL Server, Using a stored procedure to insert data

I'm trying to insert data into my database by using a stored procedure but 3 of my columns are using the int identity type and I cannot insert. 我正在尝试通过存储过程将数据插入数据库中,但是我的3列使用的是int身份类型,因此无法插入。 It keeps saying cannot do this whilst identity insert is off 一直说不能关闭身份插入功能

When IDENTITY_INSERT is on, it just means that you can put your own data in IDENTITY column. 启用IDENTITY_INSERT时,仅表示您可以将自己的数据放入IDENTITY列中。 It doesn't disable the FK constraint you have on the table. 它不会禁用表上的FK约束。 You can delete the FK constraint, or disable it, and risk having logically inconsistent data in your DB, or you can fix your SP so you won't insert any duplicate values. 您可以删除或禁用FK约束,并冒着数据库中逻辑上不一致的数据的风​​险,或者可以修复SP,以免插入任何重复的值。

Something is amiss. 有点不对劲。 Three columns in a single table of type Identity ? 单一类型为Identity表中的 I'm having difficulty imagining what they could represent, and I have to wonder where the natural keys are. 我很难想象它们可以代表什么,而且我想知道自然键在哪里。

In any case, IDENTITY_INSERT isn't something you want to putz with casually. 无论如何,IDENTITY_INSERT都不是您随随便便想要的东西。 It's an administrative feature to allow ad hoc changes to the data, for example bulk loading the database. 这是一项管理功能,允许对数据进行临时更改,例如批量加载数据库。

If you do actually know what the identities are (as input to your stored procedure) then the table is misdefined, because it's supposed to be the identity source. 如果您确实知道身份是什么(作为存储过程的输入),则表定义不正确,因为它应该是身份源。 If you don't know, or you're willing to let the table generate identity values, then you simply don't mention those columns in your INSERT statement. 如果您不知道,或者愿意让表生成标识值,则只需在INSERT语句中不提及这些列。 At most, the generated values would be OUTPUT parameters to your stored procedure. 最多,生成的值将是存储过程的OUTPUT参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM