Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

No results... Expand / Collapse
Author
Message
Posted Friday, June 6, 2014 2:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
-- 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
Post #1578191
Posted Friday, June 6, 2014 2:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 12:52 PM
Points: 13,636, Visits: 11,509
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




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1578195
Posted Friday, June 6, 2014 3:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
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
Post #1578212
Posted Friday, June 6, 2014 4:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
-- 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
Post #1578237
Posted Friday, June 6, 2014 5:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1578241
Posted Friday, June 6, 2014 5:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
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)?
Post #1578250
Posted Friday, June 6, 2014 6:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1578257
Posted Friday, June 6, 2014 6:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
aye, got it all working fine now, tested and well :)

Though, I still didn't get the insert script working :p
Post #1578270
Posted Friday, June 6, 2014 6:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 12:52 PM
Points: 13,636, Visits: 11,509
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:
INSERT (Transact-SQL)

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




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1578271
Posted Friday, June 6, 2014 7:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, July 13, 2014 5:52 PM
Points: 35, Visits: 101
haha I tried to understand it but it completely fkedup my brain :p
Post #1578307
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse