【问题标题】:SQL - Insert Into table based on a counter valueSQL - 根据计数器值插入表
【发布时间】:2016-07-29 03:49:58
【问题描述】:

我有tabA:

________________________
|ID  |EMPLOYEE|CODE    |
|49  |name1   |mobile  |
|393 |name2   |none    | 
|3002|name3   |intranet|
________________________

ID 列 (tabA) 基于以下 tabB 中的计数器:

_________________
|TYPE       |ID  |
|intranet   |3003|
|mobile     |50  | 
|none       |394 |
__________________

我想使用 ID 计数器在 tabA 中插入新行(因为它是下一个可用的 ID)。如何根据计数器值插入表格?

我正在尝试这种方法,这会导致尝试插入重复类型,而不是 max(ID):

 INSERT INTO tabA (ID, EMPLOYEE, CODE)  

VALUES ((select max(ID) from tabB where TYPE = 'A'),name4,'intranet');

我希望看到tabA:

________________________
|ID  |EMPLOYEE|CODE    |
|3000|name1   |mobile  |
|3001|name2   |none    | 
|3002|name3   |intranet|
|3003|name4   |intranet|
________________________

标签B:

_________________
|TYPE       |ID  |
|mobile     |2999|
|none       |3002| 
|intranet   |3004|
__________________

【问题讨论】:

    标签: sql sql-server sql-server-2008 sql-update sql-insert


    【解决方案1】:

    使用insert . . . select:

    INSERT INTO tabA(ID, EMPLOYEE, CODE)  
        select max(ID), 'name4', 'intranet'
        from tabB
        where TYPE = 'A';
    

    【讨论】:

    • 我们需要添加Group By
    • 我觉得你打错字了,应该是'name4'
    • 不幸的是,我得到了与我尝试的相同的错误。违反主键......无法在对象'A'中插入重复键......
    • @AlkippeNikephoros,尝试使用max(ID) + 1
    • 这会插入错误的 ID 并且不使用来自 tabB 的计数器(因此在运行查询后 tabB ID 不会改变)。 :我想使用计数器值,这将导致 tabB 在使用该 id 时增加 1。
    【解决方案2】:

    这将解决您的问题:(假设您的 ID 是增量的)

    INSERT INTO tabA (ID, EMPLOYEE, CODE)
    
     VALUES ((SELECT TOP 1 ID + 1 FROM tabA ORDER BY ID DESC),'name4', 'intranet')
    

    【讨论】:

    • 不幸的是,ID 并不总是递增的。通过系统有一个复选框。如果选中该框然后取消选中(这会导致 tabA 删除具有复选框的员工所在的行)。然后再次勾选,它将使用 tabB 中的计数器并将员工分配给它从 tabB 获得的 id。因此我需要使用 tabB 中的计数器来插入员工。 - 它还会导致一些非常高的整数(超过 tabB 中的 ID 计数器超过 300 万)
    • 所以你的意思是,它是表 B 中的最高 ID?是吗?
    • 您的问题变得令人困惑。我的意思是,如果您不连续递增,如何在 tabB 中生成不会在插入期间重复的 ID
    • 没有。它是 type = 'intranet' 的值(tabA 中的代码)。基本上 tabB 被用来确保不创建重复的 ID,但不使用计数器手动插入和 ID 会导致重复的 id。- 移动、无、内联网是三个不同的计数器。我需要获取最高的 Intranet ID 并将其用于插入,插入后 tabB 的 Intranet ID 应为 3004,因为我插入了 |3003|name4|intranet。
    • 我的意思是 - tabA 不会是增量的,就像您删除 ID 为 3001 的行一样,如果您想再次添加它们,它会为用户创建一个新 ID。 tabB 是连续递增的,从计数器中使用 1 个 ID 后,计数器会在下次插入其他人时递增。
    【解决方案3】:

    我建议以下答案:

    @tabA 和@tabB 我假设是你上面的 tabA 和 tabB。

    declare @tabA table
    (
        id integer,
        employee nvarchar(10),
        code nvarchar(10)
    )
    
    insert into @tabA values (49, 'name1','mobile');
    insert into @tabA values (393, 'name2','none');
    insert into @tabA values (3002, 'name3','intranet');
    
    declare @tabB table
    (
        type_ nvarchar(10),
        ID integer
    )
    
    insert into @tabB values ('intranet',3003);
    insert into @tabB values ('mobile',50);
    insert into @tabB values ('none', 394);
    
    insert into @tabA
    select MAX(b.ID), 'name4','intranet'
    from @tabB B
    where b.type_ = 'intranet'
    
    declare @max integer
    
    select @max = MAX(ID) from @tabB where type_ = 'intranet'
    
    declare @max_1 integer
    set @max_1 = @max + 1
    
    update @tabB
    set ID = @max_1
    from @tabB B
    where type_ = 'intranet'
    
    select * from @tabA
    
    select * from @tabB
    
    ----------------------------
    what I got from @tabA
    
    49      |name1| mobile
    393     |name2| none
    3002    |name3| intranet
    3003    |name4| intranet
    
    what I got from @tabB
    
    intranet|3004
    mobile  |50
    none    |394
    

    【讨论】:

    • 谢谢,它可以满足我的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多