INSERT WHEN PRIMARY JEY NOT EXIST IN

i HAVE THE FOLLOWING SQL.

What i want is to insert into tableResort anything from temp_resort that isn't already in resort, based on rcode.

surely the following sql is doing that but im getting a :
Violation of PRIMARY KEY constraint 'PK_Resort'. Cannot insert duplicate key in object 'dbo.RESORT'.

how?
INSERT INTO RESORT(RCODE, CCODE, ISLAND) 
SELECT RCODE, CCODE, ISLAND FROM TEMP_RESORT T
WHERE T.RCODE NOT IN
(
	SELECT RCODE
	FROM RESORT	
)

Open in new window

scm0smlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jinesh KamdarCommented:
Is ur primary key a composite key of (RCODE, CCODE) ?
0
scm0smlAuthor Commented:
nope
0
gatorvipCommented:
try to group based on RCODE, CCODE, ISLAND , that will at least eliminate duplicated rows. If you still get the error, it means you have duplicate values for the primary key in the RESORT table, whichever one it is.
INSERT INTO RESORT(RCODE, CCODE, ISLAND) 
SELECT RCODE, CCODE, ISLAND FROM TEMP_RESORT T
WHERE T.RCODE NOT IN
(
        SELECT RCODE
        FROM RESORT     
)
group by RCODE, CCODE, ISLAND

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jinesh KamdarCommented:
Is RCODE the PK for TEMP_RESORT as well as RESORT? How about trying this?
INSERT INTO RESORT (RCODE, CCODE, ISLAND) 
SELECT RCODE, CCODE, ISLAND FROM TEMP_RESORT T
WHERE NOT EXISTS (SELECT 1 FROM RESORT WHERE RCODE = T.RCODE)

Open in new window

0
scm0smlAuthor Commented:
THE PROBLEM WAS WITH DUPLICATE RCODES IN THAT THEIR WAS ONE SAY FOR MARAKECH, BUT THERE IS A MARAKECH IN TWO DIFFERENT COUNTRIES.

DAMN.
0
Jinesh KamdarCommented:
Then in that case, you will have to change ur existing PK from just the RCODE column to include the COUNTRY column as well, thereby making a composite PK.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jinesh KamdarCommented:
Thx for the accept, though I didn't understand the 'B' grade !!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.