Solved

SQL Query not working populate table from other

Posted on 2014-12-16
5
85 Views
Last Modified: 2014-12-18
I am using the below sql and running into an issue.

It does populate correctly if the table loanselect is empty however. It does not add a new record from the loansummary after the loanselect has been populated.

IF NOT EXISTS(SELECT 1 FROM OUTLOOKREPORT.DBO.loanselect D INNER JOIN emdb.emdbuser.loansummary ON D.XREFID = emdb.emdbuser.loansummary.XREFID)
INSERT INTO OUTLOOKREPORT.DBO.loanselect (Borrower, [address],[status],xrefid)
SELECT BorrowerFirstName + ' ' + BorrowerLastname,Address1,CurrentMileStoneName,xrefid FROM emdb.emdbuser.loansummary

Open in new window

0
Comment
Question by:desiredforsome
[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
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40503352
>It does populate correctly if the table loanselect is empty however.
Makes sense, as loanselect is the main table in the IF NOT EXISTS(..)

>It does not add a new record from the loansummary after the loanselect has been populated.
Likely because once there's a row, then if it matches the JOIN criteria then IF NOT EXISTS(..) returns a row, it doesn't execute the INSERT statement.

I think you'll need to give us some sample data sets to fully flush out this question.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40503717
You don't need an IF NOT EXISTS(), you need a WHERE NOT EXISTS() so that each row can be checked separately:

INSERT INTO OUTLOOKREPORT.DBO.loanselect (Borrower, [address],[status],xrefid)
SELECT BorrowerFirstName + ' ' + BorrowerLastname,Address1,CurrentMileStoneName,xrefid
FROM emdb.emdbuser.loansummary lsum
WHERE NOT EXISTS(
    SELECT 1
    FROM OUTLOOKREPORT.DBO.loanselect lsel
    WHERE
        lsel.XREFID = lsum.XREFID
    )
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 40503752
Move the existence check into the INSERT-statement:
INSERT	OUTLOOKREPORT.DBO.loanselect
(	Borrower
,	[address]
,	[status]
,	xrefid
)
SELECT	a.BorrowerFirstName + ' ' + a.BorrowerLastname
,	a.Address1
,	a.CurrentMileStoneName
,	a.xrefid
FROM	emdb.emdbuser.loansummary	a
LEFT
JOIN	OUTLOOKREPORT.DBO.loanselect	b	ON	a.XREFID = b.XREFID
WHERE	b.XREFID IS NULL

Open in new window

0
 

Author Closing Comment

by:desiredforsome
ID: 40507212
PERRRFECT!!!
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40507372
My code did the same thing except there was no possibility of duplicate INSERTs if the value appeared more than once in the loanselect table.  That is, be aware that if the same XREFID appears more than once in the loanselect table, you will insert multiple rows into the loansummary table.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

691 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