如何为第一个主键列添加新条目时,如何自动增加表中的第二个主键列

时间:2012-06-08 15:13:03

标签: sql sql-server

我正在尝试找到一种方法,在为第一个主键列添加新条目时自动增加表中的第二个主键列。我想这里的例子最好,所以这里就是。

假设我有一张桌子:

CREATE TABLE T
(
     SecNum INT NOT NULL,
     EntryID INT NOT NULL,
     Value FLOAT,
) CONSTRAINT [PK_T] PRIMARY KEY CLUSTERED 
(
    [SecNum] ASC,
    [EntryID] ASC
)

我会运行以下声明:

INSERT INTO T (SecNum, Value) VALUES (0, 10)

我的表应该如下:

SECNUM | ENTRYID | VALUE
-------------------------
   0         0       10

我会运行以下声明:

INSERT INTO T (SecNum, Value) VALUES (0, 10)

我的表应该如下:

SECNUM | ENTRYID | VALUE
-------------------------
   0         0       10
   0         1       10

我会运行以下声明:

INSERT INTO T (SecNum, Value) VALUES (1, 20)

我的表应该如下:

SECNUM | ENTRYID | VALUE
-------------------------
   0         0       10
   0         1       10
   1         0       20

4 个答案:

答案 0 :(得分:2)

使用INSTEAD OF触发器可以实现这一点:

CREATE TRIGGER TriggerName
ON T
INSTEAD OF INSERT
AS
    -- THIS TOP BIT IS OPTIONAL, IT WILL ALLOW ENTRY ID TO BE OVERRIDDEN IF 
    -- IT IS SUPPLIED TO THE INSERT AND WILL NOT VIOLATE THE PRIMARY KEY
    IF NOT EXISTS 
        (   SELECT  1
            FROM    T
                    INNER JOIN inserted i
                        ON i.SecNum = T.secNum
                        AND i.EntryID = T.EntryID
            UNION
            SELECT  1
            FROM    inserted
            WHERE   EntryID IS NULL
        )
        BEGIN
            INSERT T (SecNum, EntryID, Value)
            SELECT  SecNum, EntryID, Value
            FROM    inserted
        END
    ELSE
    -- IF OVERRIDE ABILITY IS NOT REQUIRED JUST USE THE BELOW INSERT
        BEGIN
            INSERT T (SecNum, EntryID, Value)
            SELECT  i.SecNum, COALESCE(LastID, 0), i.Value
            FROM    inserted I
                    LEFT JOIN 
                    (   SELECT  SecNum, MAX(T.EntryID) + 1 [LastID]
                        FROM    T
                        GROUP BY SecNum
                    ) T
                        ON T.SecNum = i.SecNum

        END

Example here

HOWEVER 这不是很优雅。值得一提的是它真的有必要吗?您是否可以使用代理主键,并使用ROW_NUMBER()动态创建条目ID?

答案 1 :(得分:1)

使用instead of insert触发器

可以解决您的问题
 create trigger Trigger1 on T INSTEAD OF INSERT
 as
 begin                                  
       insert into T(SecNum,EntryID,Value)
       select SecNum,
             (select count(*) from T where SecNum = i.SecNum) as EntryID, 
             value 
       from inserted i
 end

答案 2 :(得分:1)

这样的事情怎么样:

INSERT INTO T (SecNum, Value, EntryId)
SELECT 0, 10, count(*)
FROM T WHERE SecNum = 0

这不是最干净的解决方案,也会表现得非常糟糕。但它应该完成工作。

答案 3 :(得分:1)

这是如何在不将值存储在表中的情况下完成的(我不知道你为什么要存储它)

表格

DECLARE @T TABLE
    (
      SecNum INT NOT NULL,
      EntryID INT,
      Value FLOAT
    )

数据

INSERT  INTO @T
        ( SecNum, Value )
VALUES  ( 0, 10 )
INSERT  INTO @T
        ( SecNum, Value )
VALUES  ( 0, 10 )
INSERT  INTO @T
        ( SecNum, Value )
VALUES  ( 1, 20 )

<强> QUERY

SELECT  SecNum,
        ROW_NUMBER() OVER ( PARTITION BY value ORDER BY Value ) - 1 AS EntryID,
        Value
FROM    @T

<强> RESULT

SecNum  EntryID Value
0          0    10
0          1    10
1          0    20

如果EntryID随SecNum AND Value更改,请使用此查询:

SELECT  SecNum,
        ROW_NUMBER() OVER ( PARTITION BY Value,SecNum ORDER BY Value, SecNum ) - 1 AS EntryID,
        Value
FROM    @t

结果2

SecNum  EntryID Value
0           0   10
0           1   10
1           0   10
1           0   20