Hi,Using SQL SERVER 2000, T-SQL Language There is a table called retailer which is always up to date containing a list of all the valid retailers.The data comes in text formatIt is being loaded into a temporary table initially and it must be transferred to the holding table, all the rows in the temporary table are flagged 'N' i.e. not been loadedIf they are loaded successfully into the table then they are flagged 'Y' else 'R' rejected.Before that all the 'N' flagged records are to be checked for valid data like any nullsThen to load the data into a holding tableUsing a script containing stored procedure in whichCursor is being used for row by row fetching of dataAfter a row is fetched it will be checked for valid,i.e whether the retailer id is valid or not,customer name is null, or id is null all these checks are done inside the cursor after the first row is fetched.an example of the checking -- Fail if Retailer does not exist SELECT RETA_SK FROM RETA_RETAILER RETA1 INNER JOIN DERM_DEALER_RETAIL_MANAGER DERM1 ON RETA1.DERM_SK = DERM1.DERM_SK WHERE RETA1.RETAILER_ID = @RETAILER_ID AND COUN1.COUNTRY_CODE = 'SE' IF NOT EXISTS (SELECT RETA1.RETA_SK FROM RETA_RETAILER RETA1 INNER JOIN DERM_DEALER_RETAIL_MANAGER DERM1 ON RETA1.DERM_SK = DERM1.DERM_SK INNER JOIN COUN_COUNTRY COUN1 ON DERM1.COUN_COUNTRY_CODE = COUN1.COUNTRY_CODE WHERE RETA1.RETAILER_ID = @RETAILER_ID AND COUN1.COUNTRY_CODE = 'SE' ) BEGIN INSERT INTO ERLO_ERROR_LOOKUP (CUSTOMER_ID, FINANCE_AGREEMENT_NUMBER, REASON, LOADED_DATETIME ) VALUES (@CUSTOMER_ID, @AGREEMENT_NUMBER, 'RETAILER ID ('+CONVERT(VARCHAR,@RETAILER_ID) + ') DOES NOT EXIST', GETDATE() ) UPDATE LOAD_DATA_TABLE SET LOADED_INDICATOR = 'R' WHERE LOAD_ID = @LOAD_ID SET @ERROR_IND = 1 GOTO FETCH_NEXT_CURSOR_VALUE END/*--------------------------------------------------------------------------------------------------------------------------------*/ -- Fail if the customer name and company name are null IF @CUSTOMER_NAME IS NULL AND @COMPANY_NAME IS NULL BEGIN INSERT INTO ERLO_ERROR_LOOKUP (CUSTOMER_ID, FINANCE_AGR_NUMBER, REASON, LOADED_DATETIME ) VALUES (@CUSTOMER_ID, @AGREEMENT_NUMBER, 'NO CUSTOMER NAME OR COMPANY NAME', GETDATE() ) UPDATE LOAD_DATA_TABLE SET LOADED_INDICATOR = 'R' WHERE LOAD_ID = @LOAD_ID SET @ERROR_IND = 1 GOTO FETCH_NEXT_CURSOR_VALUE END
...If a row passes all the tests thenThen update the tables to insert the new arrived values. -- If there hasnt been an error, then load the data IF @ERROR_IND = 0 BEGIN -- If the Customer and Agreement already exist, then update the records IF EXISTS (SELECT CUST1.CUSTOMER_ID, FIAG1.FIAG_SK FROM CUST_CUSTOMER CUST1 INNER JOIN CUVE_CUSTOMER_VEHICLE CUVE1 ON CUST1.CUSTOMER_ID = CUVE1.CUSTOMER_ID INNER JOIN FIAG_FINANCE_AGREEMENT FIAG1 ON CUVE1.CUVE_SK = FIAG1.CUVE_SK WHERE CUST1.CUSTOMER_ID = @CUSTOMER_ID AND FIAG1.FIAG_SK = @AGREEMENT_NUMBER ) BEGIN -- UPDATE CUST_CUSTOMER UPDATE CUST_CUSTOMER SET ADDRESS_LINE_1 = ISNULL(@ADDRESS_LINE_1,ADDRESS_LINE_1), ADDRESS_LINE_2 = ISNULL(@ADDRESS_LINE_2, ADDRESS_LINE_2), ADDRESS_LINE_3 = ISNULL(@ADDRESS_LINE_3, ADDRESS_LINE_3), ADDRESS_LINE_4 = ISNULL(@ADDRESS_LINE_4, ADDRESS_LINE_4), CITY = ISNULL(@CITY,CITY), POSTCODE = ISNULL(@POSTCODE,POSTCODE), TELEPHONE_DAY_NUMBER = ISNULL(@DAYTIME_NUMBER, TELEPHONE_DAY_NUMBER), TELEPHONE_EVENING_NUMBER =ISNULL( @EVENING_NUMBER, TELEPHONE_EVENING_NUMBER), TELEPHONE_MOBILE_NUMBER = ISNULL(@MOBILE_NUMBER, TELEPHONE_MOBILE_NUMBER), PREFERRED_TELEPHONE_CONTACT = ISNULL(@PREFERRED_NUMBER, PREFERRED_TELEPHONE_CONTACT), EMAIL = ISNULL(@EMAIL, EMAIL), USER_LAST_UPDATED_BY = 1, LAST_UPDATED_DATE_TIME = GETDATE() WHERE CUSTOMER_ID = @CUSTOMER_ID
Now this process is taking many hoursKindly help me out and suggest if you spot any problems in the code or if you can suggest something which would reduce the data loading time that would be great.