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
b_ID是从表A中的a_ID传递过来的继续关注
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
提示错误为:"过程 '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
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这个好了,,,
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这个对了吧:)
其中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
@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
@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
------------------------------------------------------------
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
@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这个好了,,,
@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这个对了吧:)