插入具有唯一数字ID的行

时间:2017-01-24 08:21:51

标签: sql sql-server

我有表dbo.Register,带有唯一的ID列。我需要在这个表中插入许多行,但是任何具有唯一标识符的行,如:

INSERT INTO dbo.Register (ID, Name, State, Comment)
Select
NEWID(),
Name,
State,
Comment
From dbo.OtherTable

但是NEWID()返回长字符串: 2D0D098E-2FFE-428A-B4EF-950C89FFF83A ,但我需要唯一的整数ID。例如,此表中的下一个免费ID。

编辑。 我无法修改此表。我只需要在当前结构中插入行。

样本记录:

+--------+------------+--------------+------------+
|   ID   |    Name    |    State     |  Comment   |
+--------+------------+--------------+------------+
| 153299 | Zapytania  | Przyjeto     | NieDotyczy |
| 153300 | Zapytania  | Przyjeto     | NieDotyczy |
| 153301 | Zapytania  | Przyjeto     | NieDotyczy |
| 153302 | Zapytania  | Przyjeto     | NieDotyczy |
| 153303 | Zapytania  | Przyjeto     | NieDotyczy |
| 153304 | Dyspozycje | Zakonczono   | NieDotyczy |
| 153305 | Zapytania  | DoRealizacji | NULL       |
| 153306 | Zapytania  | Przyjeto     | NieDotyczy |
| 153307 | Zapytania  | Przyjeto     | NieDotyczy |
| 153308 | Dyspozycje | Przyjeto     | NieDotyczy |
+--------+------------+--------------+------------+

ID列的定义已经是:

[ID] [int] IDENTITY(1,1) NOT NULL,

3 个答案:

答案 0 :(得分:5)

您可以通过以下方式执行此操作

DECLARE @ID INT;
SELECT @ID=ISNULL(MAX(ID),0) FROM dbo.Register
--Isnull to handle first record for the table

INSERT INTO dbo.Register (ID, Name, State, Comment)
Select
ROW_NUMBER() OVER(ORDER BY(SELECT 1))+ @ID,
Name,
State,
Comment
From dbo.OtherTable

根据编辑,无需在“插入”列表中指定列。您可以跳过该列并执行INSERT& SELECT。

INSERT INTO dbo.Register ( Name, State, Comment)
Select 
    Name,
    State,
    Comment
    From dbo.OtherTable

最好你需要查看IDENTITY (Property) (Transact-SQL)

答案 1 :(得分:2)

运行下一个脚本....

ALTER TABLE dbo.Register
DROP column ID

ALTER TABLE dbo.Register
add ID int identitity (1, 1)    

INSERT INTO dbo.Register (Name, State, Comment)
Select
 Name,
 State,
 Comment
From dbo.OtherTable

编辑:

使用新信息,只需

INSERT INTO dbo.Register (Name, State, Comment)
Select
    Name,
    State,
    Comment
From dbo.OtherTable

ID列将自行处理......

答案 2 :(得分:0)

如果你不能IDIDENTITY这个更好的主意,你可以使用SEQUENCE

CREATE SEQUENCE [dbo].[RegisterId]  
    START WITH 153309  
    INCREMENT BY 1;  
GO

然后,

INSERT [dbo].[Register]
    (
        [ID],
        [Name],
        [State],
        [Comment]
    )
SELECT
        NEXT VALUE FOR [dbo].[RegisterId] [ID],
        [Name],
        [State],
        [Comment]
    FROM
        [dbo].[OtherTable];

或者,如果您无法更改架构,则可以使用临时表来使用IDENTITY列,

DECLARE @maxId INT = SELECT MAX([ID]) + 1 FROM [dbo].[Register];

DECLARE @createTemp NVARCHAR(4000) = N'
CREATE TABLE #TempRegister
(
    [ID] INT IDENTITY(' + CAST(@maxId, NVARCHAR(14)) + ', 1),
    [Name] NVARCHAR(?),
    [State] NVARCHAR(?),
    [Comment] NVARCHAR(?)
);';

EXEC sp_executesql @createTemp;
GO

INSERT #TempRegister
    (
        [Name],
        [State],
        [Comment]
    )
SELECT
        [Name],
        [State],
        [Comment]
    FROM
        [dbo].[OtherTable];

INSERT [dbo].[Register]
SELECT
        [ID],
        [Name],
        [State],
        [Comment]
    FROM
        #TempRegister;

DROP TABLE #TempRegister;