Solved

SQL Query not working populate table from other

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL SELECT query help 7 42
MS SQL + Insert Into Table - If Doesnt Exist 9 36
Unable to Uninstall Visual Studio 2015 7 28
Sql Query 6 68
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

825 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