June 6, 2014 at 2:09 am
-- SSIS IMPORT
GO
-- Wijziging aanpassen van dummy table naar artikelen table.
UPDATE a
SET [omschrijving]= SP.[omschrijving]
,[verkoopprijs]= SP.[verkoopprijs]
,[gewijzigd]= getDate()
FROM [artikelen] a
LEFT OUTER JOIN [Hofstede].[dbo].[sparepartsupdate] SP
ON a.[PartNrFabrikant] = SP.[PartNrFabrikant]
WHERE (A.omschrijving != SP.[omschrijving]) OR (A.[verkoopprijs] != SP.[verkoopprijs]);
GO
-- Controleren wat er verwijdert is door te kijken wat er WEL in de artikel table zit maar niet meer in de dummy table.
UPDATE a
set [Verwijderd]= getDate()
from [artikelen] a
LEFT OUTER JOIN [Hofstede].[dbo].[sparepartsupdate] SP
ON a.[PartNrFabrikant] NOT IN (SP.[PartNrFabrikant])
GO
-- De toegevoegde artikelen toevoegen aan de artikel table.
UPDATE a
SET [omschrijving]= SP.[omschrijving]
,[verkoopprijs]= SP.[verkoopprijs]
,[toegevoegd]= getDate()
FROM [artikelen] a
LEFT OUTER JOIN [Hofstede].[dbo].[sparepartsupdate] SP
on sp.[PartNrFabrikant] NOT IN (a.[PartNrFabrikant])
GO
-- De dummy table na gebruik weer verwijderen.
-- UITGESCHAKELD VOOR TEST -- DELETE sparepartsupdate
the second and third query are supposed to check what changed and the 3rd one checks what has been deleted...
I deleted about 250 rows in Sparepartsupdate but it doesn't recognise them after executing this query which takes about half an hour
I have also changed a few but either these will not be recognised.
what am I doing wrong this time? ;p
June 6, 2014 at 2:37 am
Second query:
UPDATE a
set [Verwijderd] = getDate()
from [artikelen] a
WHERE NOT EXISTS (SELECT 1 FROM [Hofstede].[dbo].[sparepartsupdate] SP WHERE a.[PartNrFabrikant] = SP.[PartNrFabrikant]);
The third query, shouldn't that be an INSERT statement?
ps: dt-fout in commentaar van tweede query π
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
June 6, 2014 at 3:18 am
yupp...
After an hour of trying everything I could possibly think of I came to the conclusion that I am unable to create anything with insert into:p
June 6, 2014 at 4:59 am
-- De toegevoegde artikelen toevoegen aan de artikel table.
INSERT INTO artikelen AR
SET [toegevoegd]= getDate()
LEFT JOIN [Hofstede].[dbo].[sparepartsupdate]
WHERE [artikelen] not in [sparepartsupdate]
I can't get any further so basically I'm pretty stuck on my insert query :p
June 6, 2014 at 5:26 am
The first query may not be doing what you expect. If you reference a column from a left-joined table in the WHERE clause, the left join is converted to an INNER join - except for the special case WHERE [column] IS NULL.
To preserve the outer join, change your query so that the filter is applied to the join, like this:
UPDATE a
SET [omschrijving] = SP.[omschrijving]
,[verkoopprijs] = SP.[verkoopprijs]
,[gewijzigd] = getDate()
FROM [artikelen] a
LEFT OUTER JOIN [Hofstede].[dbo].[sparepartsupdate] SP
ON a.[PartNrFabrikant] = SP.[PartNrFabrikant]
AND ((A.omschrijving <> SP.[omschrijving]) OR (A.[verkoopprijs] <> SP.[verkoopprijs]));
Note that for rows in artikelen which don't have a match in SP, [omschrijving] and [verkoopprijs] will be set to NULL - is this what you are expecting?
Is PartNrFabrikant a primary key in either table?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 6, 2014 at 5:46 am
If I change WHERE to AND it gives me this..:
(408014 row(s) affected)
which with where is:
(2054 row(s) affected)
---------------------------------------------
Isn't this because it doesn't know what to do with AND so it affects every value found by "ON"? (Which is everything that did NOT change)?
June 6, 2014 at 6:09 am
pk2dpvp (6/6/2014)
If I change WHERE to AND it gives me this..:(408014 row(s) affected)
which with where is:
(2054 row(s) affected)
---------------------------------------------
Isn't this because it doesn't know what to do with AND so it affects every value found by "ON"? (Which is everything that did NOT change)?
It's because the outer join is converted to an inner join. You need to decide what you want to do here. My guess is you want an inner join, where non-matching rows would be unaffected by the update. If this is the case, write the join as an inner join and it doesn't matter where you put the filter.
Alternatively, if you want all rows to be affected by the update, with matching rows taking values from the second table and nonmatching rows taking NULL values, then write it as a left join with the filter in the join.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 6, 2014 at 6:33 am
aye, got it all working fine now, tested and well π
Though, I still didn't get the insert script working :p
June 6, 2014 at 6:37 am
pk2dpvp (6/6/2014)
aye, got it all working fine now, tested and well πThough, I still didn't get the insert script working :p
Take a look at the following MSDN page:
SET is not part of the INSERT syntax.
Also, you use NOT IN usually in the following way:
WHERE columnA NOT IN (SELECT columnB FROM anotherTable) -- you should have a list of values here
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
June 6, 2014 at 7:47 am
haha I tried to understand it but it completely fkedup my brain :p
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply