如果是主外键关系,外键不能设置成自增的。也就是说,你的表A、B的a_ID不可能两个表中都是自增的。你要添加数据的要求,我看得不是很明白。

解决方案 »

  1.   

    要添加的数据为:a_t=a4,a_f=f4,b_c=b4类似结构如下
    其中a_ID=b_ID也就是每次在表A中添加一条数据要将其自增的a_ID传给表B表A
    a_ID(自增)   a_t     a_f
    -------------------------
    1             a1      f1
    2             a2      f2
    3             a3      f3
    表B
    ------------------------------
    b_ID(与表A的a_ID对应)     b_c
    ------------------------------
    1                         b1
    2                         b2
    3                         b3
      

  2.   

    CREATE PROCEDURE gg_update 
    @a int,
    @b int,
    @c int,
    @d intAS
    begin tran
      insert a,b,c from 表a values (@a,@b,@c)
      insert d from 表b values (@d)
      
      if @@error<>0 
          begin
           rollback tran
            return 0
          end
      else
         begin
          commit tran
          return -1
         end  
    go
      

  3.   

    b_ID是从表A中的a_ID传递过来的继续关注
      

  4.   

    CREATE PROCEDURE test 
    @inta int,
    @intb int,
    @intc int,
    @intd int,
    @ID int
    AS
    begin tran
      insert a,b,c from 表a values (@inta,@intb,@intc)
      select @ID = Max (a_ID) from 表a
      insert into 表b (a_ID,d) values (@ID,@intd) --表b 的 a_ID 应该不是自增量的,否则没法关联
      
      if @@error<>0 
          begin
           rollback tran
            return 0
          end
      else
         begin
          commit tran
          return @@error
         end  
    go
      

  5.   

    提示错误为:"过程 'test' 需要参数 '@b_ID',但未提供该参数。"以下是该过程
    ------------------------------------------------------------
    CREATE PROCEDURE test 
    @inta int,
    @intb int,
    @intc int,
    @intd int,
    @b_ID int
    AS
    begin tran
      insert a,b,c from 表a values (@inta,@intb,@intc)
      select @b_ID = @@identity from 表a --从表A中读取自增ID传给表B
      insert into 表b (b_ID,d) values (@b_ID,@intd) --表b 的 b_ID 应该不是自增量的,否则没法关联
      
      if @@error<>0 
          begin
           rollback tran
            return 0
          end
      else
         begin
          commit tran
          return @@error
         end  
    go
      

  6.   

    CREATE PROCEDURE testa 
    @inta int,
    @intb int,
    @intc int,
    @intd int,
    @b_ID int
    AS
    begin tran
      insert into a (a,b,c) values (@inta,@intb,@intc)
      select @b_ID = @@identity from a 
      insert into b (b_ID,d) values (@b_ID,@intd)
      
      if @@error<>0 
          begin
           rollback tran
            return 0
          end
      else
         begin
          commit tran
          return @@error
         end  
    go这个好了,,,
      

  7.   

    CREATE PROCEDURE test 
    @inta int,
    @intb int,
    @intc int,
    @intd intAS
    begin tran
    declare @b_ID int -- 是变量,不是传入的参数
      insert into table1 (a,b,c)  values (@inta,@intb,@intc)
      select @b_ID = @@identity from table1 --从表A中读取自增ID传给表B
      insert into table2 (b_ID,d) values (@b_ID,@intd) --表b 的 b_ID 应该不是自增量的,否则没法关联
      
      if @@error<>0 
          begin
           rollback tran
            return 0
          end
      else
         begin
          commit tran
          return @@error
         end  
    go这个对了吧:)