Solved

Need help with not exists

Posted on 2014-12-15
2
87 Views
Last Modified: 2014-12-19
Can someone take a look at my not exists statement. I think it is hanging when I run my sql stored procedure.
Look at my not exists statement : IF NOT EXISTS(SELECT FUAD_ITS_CLERK_ID = @lchFITS_PREFIX + RandomId())
It does not look right to me, so maybe I just don't understand how to do it properly.


OPEN CURS_EMP
@lchFITS_SUFFIX= CONVERT(INT, @lchSuffix)
FETCH NEXT FROM CURS_EMP
INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX
SELECT lchFITS_SUFFIX = @lchFITS_SUFFIX+1

WHILE @@FETCH_STATUS = 0
BEGIN
        SET @ClerkIdIsUnique = 0
        WHILE(@ClerkIdIsUnique = 0)
        BEGIN
             IF NOT EXISTS(SELECT FUAD_ITS_CLERK_ID = @lchFITS_PREFIX + RandomId())
            BEGIN
                     UPDATE SMT_FCTS_FUAD_TOPL_USR SET
                        FUAD_ITS_CLERK_ID =  @lchFITS_PREFIX + RandomId()
                         WHERE PSUS_USER_NUM = @lchPSUS_USER_NUM
                         AND (FUAD_ITS_CLERK_ID = @lchPSUS_USER_NUM
                     
                         UPDATE SMT_FCTS_FITS_STATE_CDS
                         SET FITS_SUFFIX = CONVERT(VARCHAR, @lchFITS_SUFFIX)
                         FETCH NEXT FROM CURS_EMP
                    INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX
                         SET @ClerkIdIsUnique = 1
                         SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1
               END
               ELSE SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1
          END
0
Comment
Question by:brgdotnet
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
ID: 40502176
I'm assuming RandomID() is a User Defined Function.

Maybe try assigning RandomID() to a variable and use that in the Select statement - see if that is better.
0
 
LVL 48

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40502951
Definitively, you need to post here the code of RandomID function or else we won't be able to help you.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question