Solved

SQL Query not working populate table from other

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query stumper 3 37
SQL VIEW 7 24
C# guarantee sql connection close 6 32
conditional join based on column 4 12
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

863 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

20 Experts available now in Live!

Get 1:1 Help Now