Solved

Need help with not exists

Posted on 2014-12-15
2
80 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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now