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 2005 Forums
 Transact-SQL (2005)
 If Statment Ending a Loop

Author  Topic 

thedryden
Starting Member

23 Posts

Posted - 2008-02-04 : 12:36:17
In the following code, it appears that if the Bolded if Statment is true, then it will end the loop at the last bolded END. Because of this not all of the records that should be added are added, which is causing me no end of headaches...

DECLARE @SQL as varchar(4000)
, @RowNum as smallint
, @CUID as varchar(55)
, @FullName as varchar(25)
, @EmailID as smallint
, @UserID as smallint
, @IsThere as smallint
, @Emails as varchar(200)
, @BodyText as varchar(4000)
, @SubjectText as varchar(1000)
, @FileTag as varchar(1000)
, @Count as smallint
, @Subjects as varchar(4000)
, @CUID2 as varchar(55)

DELETE FROM CTS.dbo.EMAIL_ARRAY

UPDATE CTS.dbo.EMAIL_FILES
SET FILETAG = 'added to'
WHERE FILEID = 14

SET @SQL = 'INSERT INTO CTS.dbo.EMAIL_ARRAY VALUES( '+CHAR(39)+'Testing'+CHAR(39)+', '+CHAR(39)+''+CHAR(39)+', 1 )'
EXEC( @SQL )
SET @SQL = 'INSERT INTO CTS.dbo.EMAIL_ARRAY VALUES( '+CHAR(39)+'Tested'+CHAR(39)+', '+CHAR(39)+''+CHAR(39)+', 3 )'
EXEC( @SQL )
SET @SQL = 'INSERT INTO CTS.dbo.EMAIL_ARRAY VALUES( '+CHAR(39)+'Testing'+CHAR(39)+', '+CHAR(39)+''+CHAR(39)+', 2 )'
EXEC( @SQL )

DECLARE EMAILY_ARRAY cursor for
SELECT CUID
, FULL_NAME
, EMAIL_ID
FROM CTS.dbo.EMAIL_ARRAY
ORDER BY CUID

SET @RowNum = 0
SET @CUID = ''
SET @FullName = ''
SET @EmailID = 0
SET @UserID = 0
SET @IsThere = 0
SET @SQL = ''
SET @Emails = ''

OPEN EMAILY_ARRAY
FETCH NEXT FROM EMAILY_ARRAY
INTO @CUID, @FullName, @EmailID

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @IsThere = max( CASE WHEN LEN(FULL_NAME) > 0 THEN 2 ELSE 1 END )
, @UserID = max( USER_ID )
FROM CTS.dbo.EMAIL_RECIPIENTS
WHERE upper(CUID) = upper(@CUID)

IF @IsThere IS NULL
BEGIN
SELECT @UserID = max( USER_ID )
FROM CTS.dbo.EMAIL_RECIPIENTS

SET @UserID = @UserID + 1

SET @SQL = 'INSERT INTO EMAIL_RECIPIENTS(USER_ID, CUID, FULL_NAME) VALUES( '+cast( @UserID as varchar(5) ) +', '+CHAR(39)++@CUID++CHAR(39)+', '+CHAR(39)++@FullName++CHAR(39)+' ) '
EXEC( @SQL )
END
ELSE
BEGIN
IF @IsThere = 1 AND LEN(@FullName) > 0
BEGIN
SET @SQL = 'UPDATE EMAIL_RECIPIENTS
SET FULL_NAME = '+CHAR(39)++@FullName++CHAR(39)+'
WHERE upper(CUID) = upper('+CHAR(39)++@CUID++CHAR(39)+')'
EXEC( @SQL )
END
END
SELECT @IsThere = max( EMAIL_ID )
FROM CTS.dbo.EMAIL_DISTRO
WHERE USER_ID = @UserID
AND EMAIL_ID = @EmailID

IF @IsThere IS NULL
BEGIN
SET @SQL = 'INSERT INTO CTS.dbo.EMAIL_DISTRO VALUES( '+cast( @UserID as varchar(5) ) +', '+cast( @EmailID as varchar(5) )+' )'
EXEC( @SQL )

IF @Emails <> ''
BEGIN
SET @Emails = @Emails + ','
END

SET @Emails = @Emails + cast( @EmailID as varchar(3) )
END

SET @CUID2 = @CUID

FETCH NEXT FROM EMAILY_ARRAY
INTO @CUID, @FullName, @EmailID

IF @CUID <> @CUID2
BEGIN
SET @SQL = ''
SET @SQL = 'DECLARE SUBJECT_ARRAY cursor for
SELECT e.SUBJECT
FROM CTS.dbo.EMAIL e
INNER JOIN CTS.dbo.EMAIL_DISTRO d ON e.EMAIL_ID=d.EMAIL_ID
WHERE e.EMAIL_ID IN ( ' + @Emails + ' )
AND d.USER_ID = ' + cast( @UserID as varchar(3) )

IF @Emails <> ''
BEGIN
EXEC( @SQL )

OPEN SUBJECT_ARRAY
FETCH NEXT FROM SUBJECT_ARRAY
INTO @SubjectText

SET @Subjects = ''

WHILE @@FETCH_STATUS = 0
BEGIN
SET @Subjects = @Subjects + '<li>' + @SubjectText + '</li>'

FETCH NEXT FROM SUBJECT_ARRAY
INTO @SubjectText
END

CLOSE SUBJECT_ARRAY
DEALLOCATE SUBJECT_ARRAY
PRINT'TST'
END

SET @Emails = ''
END

END

CLOSE EMAILY_ARRAY
DEALLOCATE EMAILY_ARRAY

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-02-04 : 14:06:11
I think you have a scoping issue. As far as I know, you cannot use a "dynamic" cursor in this way.

When you EXEC the sql that declares the cursor it only has scope durring that EXEC statement. So, the cursor no longer exists when the code after teh EXEC executes (which is also probably causing a memory leak). I *think* you need to create one big block of dynamic sql (the whole thing in the IF @Emails <> '' block) and execute it as one unit if you want to continue down your current path. Or, there are several other options which are probably better.
Go to Top of Page

thedryden
Starting Member

23 Posts

Posted - 2008-02-04 : 17:59:17
Actually through further testing it was my use of the Global veriable @@FETCH_STATUS. When the while in the bolded section got to the end it would set the variable globally to -1. This was the value being read by EMAILY_ARRAY which caused it to end. The solution I came up with was to create an @Fetch variable that I would set to the @@FETCH_STATUS of EMAILY_ARRAY so that when the global variable changed it did not affect my loop.
Go to Top of Page
   

- Advertisement -