Hi again,Apologies for (kinda) cross-posting this, I stuck it on the end of a thread in the general SQL Server forum and it got a bit overlooked. This script gets run by a job every 3 mins, and it's falling over with an "Error converting varchar value... to column of datatype int", and I think it's on this line:select @sbj1='New ICNA Forum Post (ThreadID='+@existingID+')'...where I'm trying to build up a string by dropping an ID number (datatype int) into it.So I tried:select @sbj1='New ICNA Forum Post (ThreadID='+CAST(@existingID AS varchar(100))+')'and:select @sbj1='New ICNA Forum Post (ThreadID='+CONVERT(varchar(100), @existingID)+')'Both of these result in the job running successfully, but no emails get sent and the job history shows the error "Incorrect syntax near 'Forum'." On the good side, it's supposed to be looping through the email-sending bit 4 times (there are currently 4 users) and sure enough, it repeats that error message 4 times.The full script follows below. Once again, I'd be hugely grateful if anyone could point out what I'm doing wrong, and how to do it right.Cheers.Declare @hMessage varchar(255),@msg_id varchar(255) Declare @MessageText varchar(8000),@message varchar(8000)Declare @MessageSubject varchar(8000),@subject varchar(8000)Declare @Origin varchar (8000), @originator_address varchar(8000)EXEC master.dbo.xp_findnextmsg @unread_only='true',@msg_id=@hMessage OUT WHILE @hMessage IS NOT NULL BEGINexec master.dbo.xp_readmail@msg_id=@hMessage,@message=@MessageText OUT,@subject=@MessageSubject OUT,@originator_address=@Origin OUTIF ((SELECT COUNT(*) FROM forum_users WHERE email = @Origin) = 1) -- IF email from forum-recognised addressBEGIN IF (CHARINDEX('(ThreadID=', @MessageSubject)>0) -- IF email has a thread ID BEGIN DECLARE @existingID int, @em1 varchar(100), @bdy1 varchar(8000), @sbj1 varchar(500) SELECT @existingID=CAST(SUBSTRING(@MessageSubject, (CHARINDEX('=', @MessageSubject)+1), (CHARINDEX(')', @MessageSubject)-(CHARINDEX('=', @MessageSubject)+1))) AS int) INSERT INTO forum_posts (body, thread_id) VALUES (@MessageText, @existingID) -- Do mailing declare em_cursor1 cursor for SELECT email FROM forum_users WHERE email_option='yes' open em_cursor1 fetch next from em_cursor1 into @em1 while @@FETCH_STATUS=0 begin select @bdy1='New ICNA Forum Post:'+CHAR(13)+CHAR(10)+CHAR(13)+CHAR(10)+@MessageText select @sbj1='New ICNA Forum Post (ThreadID='+@existingID+')' exec master.dbo.xp_sendmail @em1,@bdy1,@sbj1 fetch next from em_cursor1 into @em1 end close em_cursor1 deallocate em_cursor1 END ELSE -- IF email has no thread ID BEGIN DECLARE @newID int, @em2 varchar(100), @bdy2 varchar(8000), @sbj2 varchar(500) -- Create a new thread record and use the resulting ID to add a thread_post record INSERT INTO forum_threads (subject) VALUES (@MessageSubject) SELECT @newID=@@IDENTITY INSERT INTO forum_posts (body, thread_id) VALUES (@MessageText, @newID) -- Do mailing declare em_cursor2 cursor for SELECT email FROM forum_users WHERE email_option='yes' open em_cursor2 fetch next from em_cursor2 into @em2 while @@FETCH_STATUS=0 begin select @bdy2='New ICNA Forum Post:'+CHAR(13)+CHAR(10)+CHAR(13)+CHAR(10)+@MessageText select @sbj2='New ICNA Forum Post (ThreadID='+@newID+')' exec master.dbo.xp_sendmail @em2,@bdy2,@sbj2 fetch next from em_cursor2 into @em2 end close em_cursor2 deallocate em_cursor2 ENDENDSET @hMessage = NULL EXEC master.dbo.xp_findnextmsg @unread_only='true',@msg_id=@hMessage OUT END