我使用 SQL Server 2008 R2。
我正在寻找我描述为依赖身份的功能。
我将通过一个例子来解释。
考虑这样一张表:
脚本
CREATE TABLE [dbo].[Rooms](
    [RoomID] [int] NOT NULL,
    [ItemID] [int] NOT NULL,
    [ItemDescription] [nvarchar] (250))
GO
数据:
RoomID  ItemID  ItemDescription
------  ------  --------------- 
7       1       Door
7       2       Window (West)
7       3       Window (North)
8       1       Door
8       2       Table #1
8       3       Table #2
7       4       Table #1 
8       4       Chair #1 
7       5       Table #2 
7       6       Table #3 
8       5       Chair #2
(谁能告诉秘密如何在这里格式化示例表?)
我希望能够像这样声明一个依赖标识列:
ItemID [int] Identity(RoomID,1,1) NOT NULL
中的新行应触发对where =[Rooms]最大值的测试并加 1。ItemIDRoomID@roomID
而不是使用更改更新来RoomID删除并插入所需的数据。
现在我像这样以编程方式做到这一点:
DECLARE @roomID INT
SET @roomID = 7
INSERT INTO [Allocation].[dbo].[Rooms]
    ([RoomID], [ItemID], [ItemDescription]) VALUES (@roomID,
    (SELECT max([ItemID])+1 FROM [Allocation].[dbo].[Rooms] WHERE [RoomID]=@roomID)
    ,'Chair #1')
GO
那么,有这样的功能吗?
在可能的情况下没有,我可以对服务器进行编程以自动为我设置下一个依赖标识,给定特定的表、父列和依赖标识列吗?