CREATE PROCEDURE [dbo].[spInsertForumTopic] 
	@in_forum_id				int,
	@in_topic_title				varchar(255),
	@in_post_message			text,
	@in_topic_time				datetime,
	@in_topic_views				int,
	@in_topic_replies			int,
	@in_topic_status			int,
	@in_topic_first_poster_id	int,
	@in_topic_first_poster_name	varchar(50),
	@in_poster_ip				varchar(30),
	@in_post_attachment			int
AS
BEGIN
	DECLARE @id_post bigint;
	DECLARE @id_topic bigint;
	EXEC spInsertForumPost 0,
						   @in_forum_id,
						   @in_topic_first_poster_id,
						   @in_topic_first_poster_name,
						   @in_poster_ip,
						   @in_topic_time,
						   @in_topic_title,
						   @in_post_message,
						   @in_post_attachment;
	SET @id_post = @@IDENTITY;
	INSERT INTO tblForumTopics (
		forum_id,
		topic_title,
		topic_time,
		topic_views,
		topic_replies,
		topic_status,
		topic_first_post_id,
		topic_first_poster_id,
		topic_first_poster_name,
		topic_last_post_id,
		topic_last_post_time,
		topic_last_poster_id,
		topic_last_poster_name
		) 
	VALUES
		(
		@in_forum_id,
		@in_topic_title,
		@in_topic_time,
		@in_topic_views,
		@in_topic_replies,
		@in_topic_status,
		@id_post,
		@in_topic_first_poster_id,
		@in_topic_first_poster_name,
		@id_post,
		@in_topic_time,
		@in_topic_first_poster_id,
		@in_topic_first_poster_name
		);
	SET @id_topic = @@IDENTITY;
	UPDATE tblForumPosts SET topic_id=@id_topic WHERE post_id=@id_post;
	UPDATE tblForum SET 
		forum_topics=(forum_topics + 1),
		forum_last_post_id=@id_topic 
	WHERE forum_id=@in_forum_id;
	IF @in_post_attachment = 1
		BEGIN
			UPDATE tblForumAttachments SET post_id=@id_post WHERE post_id=0 AND poster_name=@in_topic_first_poster_name;
		END
END
