I have database table of 100,000 rows, imported from CSV each week using an SSIS package. Usually updates, but sometimes it can add rows.
I see a few exceptions with staging table during the update rows - I don't know why? and how to update from staging to destination table?
This is the merge code :
MERGE INTO [PWCGFA_BG].[dbo].[Bank_Guarantees] WITH (HOLDLOCK) AS bg USING [PWCGFA_BG].[dbo].[stagingBG] AS stgbg ON bg.IATA_CODE = stgbg.IATA_CODE WHEN MATCHED THEN UPDATE set bg.LEGAL_NAME=stgbg.LEGAL_NAME, bg.TRADING_NAME=stgbg.TRADING_NAME, bg.COUNTRY=stgbg.COUNTRY, bg.CURRENCY=stgbg.CURRENCY, bg.LANGUAGE=stgbg.LANGUAGE, bg.STATUS=stgbg.STATUS, bg.BANK_NAME=stgbg.BANK_NAME, bg.BANK_GUARANTEE_AMOUNT=stgbg.BANK_GUARANTEE_AMOUNT, bg.BANK_GUARANTEE_CURRENCY=stgbg.BANK_GUARANTEE_CURRENCY, bg.BANK_GUARANTEE_EXPIRY_DATE=stgbg.BANK_GUARANTEE_EXPIRY_DATE, bg.ACCREDITATION_DATE=stgbg.ACCREDITATION_DATE, bg.CLASS_PAX_OR_CGO=stgbg.CLASS_PAX_OR_CGO, bg.LOCATION_TYPE=stgbg.LOCATION_TYPE, bg.XREF=stgbg.XREF, bg.IRRS=stgbg.IRRS, bg.TAX_CODE=stgbg.TAX_CODE, bg.COUNTRY_CODE=stgbg.COUNTRY_CODE, bg.CITY=stgbg.CITY, bg.DEF=stgbg.DEF, bg.OWN_SHARE_CHANGE=stgbg.OWN_SHARE_CHANGE WHEN NOT MATCHED BY bg THEN INSERT (IATA_CODE,LEGAL_NAME,TRADING_NAME,COUNTRY,CURRENCY,LANGUAGE,STATUS,BANK_NAME,BANK_GUARANTEE_AMOUNT,BANK_GUARANTEE_CURRENCY,BANK_GUARANTEE_EXPIRY_DATE,ACCREDITATION_DATE,CLASS_PAX_OR_CGO,LOCATION_TYPE,XREF,IRRS,TAX_CODE,CITY,DEF,OWN_SHARE_CHANGE) VALUES (stgbg.IATA_CODE,stgbg.LEGAL_NAME,stgbg.TRADING_NAME,stgbg.COUNTRY,stgbg.CURRENCY,stgbg.LANGUAGE,stgbg.STATUS,stgbg.BANK_NAME,stgbg.BANK_GUARANTEE_AMOUNT,stgbg.BANK_GUARANTEE_CURRENCY,stgbg.BANK_GUARANTEE_EXPIRY_DATE,stgbg.ACCREDITATION_DATE,stgbg.CLASS_PAX_OR_CGO,stgbg.LOCATION_TYPE,stgbg.XREF,stgbg.IRRS,stgbg.TAX_CODE,stgbg.CITY,stgbg.DEF,stgbg.OWN_SHARE_CHANGE) WHEN NOT MATCHED BY stgbg THEN DELETE 
MERGEfrom the staging table toBank_Guaranteesinstead of only anUPDATE. In that case you could get rid of theLookupand theBank_Guarantees Destination