简体   繁体   中英

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

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.

I know a option would be to create another column make it IDENTITY(1,1) and copy that data.

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

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. So if you have existing code that inserts a value into the identity column it will fail. 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.

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. 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.

You'll obviously need to stop trying to insert misc_id with new records. You'll want to retrieve the SCOPE_IDENTITY() column after inserting records.

-- 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:

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 .

Usage:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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