Solved

Need help with not exists

Posted on 2014-12-15
2
91 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

630 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