Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Converting datatypes?

Author  Topic 

spudhead
Starting Member

34 Posts

Posted - 2002-07-18 : 06:55:41
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
BEGIN

exec master.dbo.xp_readmail
@msg_id=@hMessage,
@message=@MessageText OUT,
@subject=@MessageSubject OUT,
@originator_address=@Origin OUT

IF ((SELECT COUNT(*) FROM forum_users WHERE email = @Origin) = 1) -- IF email from forum-recognised address
BEGIN
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

END
END

SET @hMessage = NULL

EXEC master.dbo.xp_findnextmsg @unread_only='true',@msg_id=@hMessage OUT
END


xpandre
Posting Yak Master

212 Posts

Posted - 2002-07-18 : 10:23:37
try

ThreadID='+cast(@existingID as varchar)+')'


Go to Top of Page

spudhead
Starting Member

34 Posts

Posted - 2002-07-18 : 11:29:14
Nope, sorry. I still get the same error, whether in the job history or in Query Analyser:
"ODBC error 170 (42000) Line 1: Incorrect syntax near 'Forum'."

I'm at a complete loss. My SQL ain't anything special but I'm still pretty sure there's nothing wrong with the code. I don't know how to try and fix it - I'm on SQL Server 2000, that any help? Why is it picking a word that's right in the middle of a text string to throw an error on? And why is it saying "Line 1"?



Go to Top of Page
   

- Advertisement -