日期:2014-05-18  浏览次数:20390 次

存储过程中涉及多个表多个语句的问题

我的表结构如下:
create   table   dt_articleclass
(
      acid   tinyint   primary   key   identity(1,1),
      acname   varchar(50)   not   null
)

create   table   dt_article
(
      aid   int   primary   key   identity(1,1),
      atitle   varchar(50)   not   null,
      acontent   text   not   null,
      aauthor   varchar(30)   not   null,
      acid   tinyint   not   null   foreign   key   references   dt_articleclass(acid)
)

create   table   dt_articlepulish
(
      apid   int   primary   key   identity(1,1),
      aptime   datetime   not   null,
      apaccount   int   default   0,
      uid   int   not   null   foreign   key   references   dt_user(uid),  
      aid   int   not   null   foreign   key   references   dt_article(aid)
)

要编写存储article的存储过程,因为涉及到三个表,不知该怎样编写存储过程,我编写的如下,但是不可以

create   proc   addarticle
      @acid   int,
      @title   varchar(50),
      @content   text,
      @author   varchar(50),
      @addtime   datetime,
      @userid   int
as
      declare   @aid   int
      insert   into   dt_article(atitle,acontent,aauthor,acid)   values(@title,@content,@author,@acid)
      select   @aid=max(aid)   from   dt_article
      insert   into   dt_articlepublish(aptime,uid,aid)   values(@addtime,@userid,@aid)
go

请问那里出了问题



------解决方案--------------------
create proc addarticle
@acid int,
@title varchar(50),
@content text,
@author varchar(50),
@addtime datetime,
@userid int
as
begin
declare @aid int
insert into dt_article(atitle,acontent,aauthor,acid) values(@title,@content,@author,@acid)
select @aid=max(aid) from dt_article
insert into dt_articlepublish(aptime,uid,aid) values(@addtime,@userid,@aid)
end
go

------解决方案--------------------
要先dt_articleclass中有对应的acid,才能向dt_article插入,再向dt_articlepublish中插入