Solved

IF not exists query

Posted on 2014-12-15
2
78 Views
Last Modified: 2014-12-15
Can someone take a look at my sql below. It is probably more like Pseudo code right now, because
I did not include all of the script. Take a look at lines 14 and lines 17. I do an if exists and a select into PersonId
Can I take the value in PersonId at line 14 and put it into the value TargetTable.PersonId at line 17 ?


1  OPEN CURS_EMP
2  @BreakOutOfLoop INT

3  SELECT @lchFITS_SUFFIX = CONVERT(INT, @lchSuffix)
4  FETCH NEXT FROM CURS_EMP
5  INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX
6  SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1
7

8 WHILE @@FETCH_STATUS = 0
9 BEGIN
10      SET @SnafterIdIsUnique = 0
11
12      WHILE(@SnafterIdIsUnique = 0)
13       BEGIN
14          IF NOT EXISTS(SELECT PersonsId =  GetRandomId())
15          BEGIN
16                UPDATE TargetTable
17                SET TargetTable.PersonId = PersonId
18                SET @SnafterIdIsUnique = 1

19          END
20       END
0
Comment
Question by:brgdotnet
2 Comments
 
LVL 7

Accepted Solution

by:
Phil Davidson earned 500 total points
ID: 40501852
What version of SQL Server is this?  There is a phrase "my sql" in the comment.

I would rewrite lines 14 through 18 to this:

SET @specialVar = ISNULL(PersonID, GetRandomID())
UPDATE TargetTable
                SET TargetTable.PersonId = @specialVar
                SET @SnafterIdIsUnique = 1

Open in new window


If it is MySQL, the ISNULL should be IFNULL.

Lines 8 through 18 as they were written seem verbose.  Maybe there is a reason to do it like that.
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40501885
No not MySQL. I was taking sql in the possessive sense, like "My sql script"
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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 43
MS SQL with ODBC 5 36
Need a SQL query that creates a header row and one or more detail rows. 7 31
Increment column based of a FK 8 21
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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