Solved

SQL Query not working populate table from other

Posted on 2014-12-16
5
82 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: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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

685 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