简体   繁体   English

没有身份插入列

[英]Insert into column without having IDENTITY

I have a table 我有桌子

CREATE TABLE [misc]
(
    [misc_id] [int] NOT NULL,
    [misc_group] [nvarchar](255) NOT NULL,
    [misc_desc] [nvarchar](255) NOT NULL
)

where misc_id [int] not null should have been IDENTITY (1,1) but is not and now I'm having issues misc_id [int] not null应该是IDENTITY (1,1)但不是,现在我遇到了问题

With a simple form that insert into this table but since misc_id is looking for a number that a user would not know unless they have access to the database. 使用插入到该表中的简单表格,但是由于misc_id正在寻找用户无法访问的号码,除非他们有权访问数据库。

I know a option would be to create another column make it IDENTITY(1,1) and copy that data. 我知道一个选项是创建另一个使其成为IDENTITY(1,1)并复制该数据。

Is there another way I will be able to get around this? 还有另一种方法可以解决这个问题吗?

INSERT INTO misc (misc_group, misc_desc)
VALUES ('#misc_group#', '#misc_desc#')

I have SQL Server 2012 我有SQL Server 2012

Changing a int column to an identity can cause problems because by default you cannot insert a value into an identity column without use the set identity_insert command on. 将int列更改为标识可能会导致问题,因为默认情况下,如果不使用set identity_insert命令,则无法将值插入到Identity列中。 So if you have existing code that inserts a value into the identity column it will fail. 因此,如果您已有将值插入到Identity列中的代码,它将失败。 However its much easier to allow SQL Server to insert values(that is change it to an identity column) so I would change misc_id into an identity column and make sure that there are no programs inserting values into misc_id. 但是,允许SQL Server插入值(将其更改为Identity列)要容易得多,因此我将misc_id更改为Identity列,并确保没有程序将值插入misc_id。

If altering the table is not an option, you can try having a different table with the latest [misc_id] value inserted, so whenever you insert a new record into the table, you retrieve this value, add 1, and use it as your new Id. 如果不能更改表,则可以尝试插入另一个具有最新[misc_id]值的表,因此,每当在表中插入新记录时,都将检索该值,将其添加1,并将其用作新表。 ID。 Just don't forget to update the table after. 只是不要忘了之后要更新表。

You should re-create your table with the desired identity column. 您应该使用所需的标识列重新创建表。 The following statements will get you close. 以下语句将使您接近。 SQL Server will automatically adjust the table's identity field to MAX(misc_id) + 1 as you're migrating data. 在您迁移数据时,SQL Server会自动将表的标识字段调整为MAX(misc_id)+ 1。

You'll obviously need to stop trying to insert misc_id with new records. 您显然需要停止尝试在新记录中插入misc_id。 You'll want to retrieve the SCOPE_IDENTITY() column after inserting records. 插入记录后,您需要检索SCOPE_IDENTITY()列。

-- Note: I'd recommend having SSMS generate your base create statement so you know you didn't miss anything.  You'll have to export the indexes and foreign keys as well.  Add them after populating data to improve performance and reduce fragmentation.
CREATE TABLE [misc_new]
(
    [misc_id] [int] NOT NULL IDENTITY(1,1),
    [misc_group] [nvarchar](255) NOT NULL,
    [misc_desc] [nvarchar](255) NOT NULL
    -- Todo: Don't forget primary key but can be added later (not recommended).
)
GO

SET IDENTITY_INSERT misc_new ON;

INSERT INTO misc_new
(
    [misc_id],
    [misc_group],
    [misc_desc]
)
SELECT
    [misc_id],
    [misc_group],
    [misc_desc]
FROM misc
ORDER BY misc_id;

SET IDENTITY_INSERT misc_new OFF;
GO

EXEC sp_rename 'misc', 'misc_old';
EXEC sp_rename 'misc_new', 'misc';
GO

In MSSQL 2012 you can use SEQUENCE objects: MSSQL 2012您可以使用SEQUENCE对象:

CREATE SEQUENCE [dbo].[TestSequence] 
 AS [BIGINT]
 START WITH 1
 INCREMENT BY 1
GO

Change 1 in START WITH 1 with MAX value for [misc_id] + 1 . 改变1START WITH 1MAX value for [misc_id] + 1

Usage: 用法:

INSERT INTO misc (misc_id, misc_group, misc_desc)
VALUES (NEXT VALUE FOR TestSequence, '#misc_group#','#misc_desc#')

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

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