CREATE PROCEDURE [dbo].[spInsertForumPost] 
	@in_topic_id		bigint,
	@in_forum_id		int,
	@in_poster_id		int,
	@in_poster_name		varchar(50),
	@in_poster_ip		varchar(30),
	@in_post_time		datetime,
	@in_post_subject	varchar(255),
	@in_post_text		text,
	@in_post_attachment	int
AS
BEGIN
	INSERT INTO tblForumPosts (
		topic_id,
		forum_id,
		poster_id,
		poster_name,
		poster_ip,
		post_time,
		post_subject,
		post_text,
		post_attachment
		) 
	VALUES(
		@in_topic_id,
		@in_forum_id,
		@in_poster_id,
		@in_poster_name,
		@in_poster_ip,
		@in_post_time,
		@in_post_subject,
		@in_post_text,
		@in_post_attachment
		);
	UPDATE tblForum SET 
		forum_posts = (forum_posts + 1),
		forum_last_post_id = @in_topic_id,
		forum_last_post_subject = @in_post_subject,
		forum_last_post_time = @in_post_time,
		forum_last_poster_name = @in_poster_name 
	WHERE forum_id=@in_forum_id;
	UPDATE tblForumTopics SET 
		topic_replies = (topic_replies + 1),
		topic_last_poster_id = @in_poster_id,
		topic_last_poster_name = @in_poster_name,
		topic_last_post_time = @in_post_time 
	WHERE topic_id=@in_topic_id;
	IF @in_post_attachment = 1
	BEGIN
		UPDATE tblForumAttachments SET post_id=@@IDENTITY WHERE post_id=0 AND poster_name=@in_poster_name;
	END
END
