[转]存储过程里添加信息到第一个表时得到第一个表的新增ID,并用此ID添加信息到第二个表

ALTER PROCEDURE [dbo].[AddBbsTitle]
(@UserId uniqueidentifier,
 @UserName nvarchar(256),
 @Cid int,
 @Title nvarchar(500),
 @TitleColour nvarchar(50),
 @Content ntext,
 @IsTitle bit
)
AS
declare @TitleId int  //这里注意
INSERT INTO BbsTitle
   (UserId, UserName, CategoryId, Title, TitleColour, Body, AnswerUserId, AnswerUserName)
    VALUES
   (@UserId, @UserName, @Cid, @Title, @TitleColour, @Content, @UserId, @UserName)
    select @TitleId=@@Identity //这里注意
insert into BbsContent
 (UserId, UserName, Title, Body, TitleId, CategoryId )
 values
 (@UserId, @UserName, @Title, @Content, @TitleId, @Cid)  

 转自:http://blog.csdn.net/jxdom/article/details/1921492

posted @ 2012-06-28 21:24  MagiCube  阅读(300)  评论(0编辑  收藏  举报