Solved

SQL Query not working populate table from other

Posted on 2014-12-16
5
79 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
5 Comments
 
LVL 65

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:ScottPletcher
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:ScottPletcher
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
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.

708 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

12 Experts available now in Live!

Get 1:1 Help Now