Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. declare @Command_BulkInsert VARCHAR(max)
  2. declare @file VARCHAR(1000) = 'G:\RELEASE 5.11.26\MCR.UNET.CREPORT_ACCOUNTS.csv'
  3. Declare @startRow varchar(10) = '2', @endRow varchar(10) = '1000000'
  4. DECLARE @countryId INT
  5.  
  6. SELECT @countryId = countryId FROM customer.country WHERE countryCode = 'SN'
  7.  
  8.  
  9. IF OBJECT_ID('tempdb..#AccountsForDelete') IS NOT NULL DROP TABLE #AccountsForDelete
  10. CREATE TABLE #AccountsForDelete(accountId INT)
  11.  
  12. IF OBJECT_ID('tempdb..#AccountList') IS NOT NULL DROP TABLE #AccountList
  13.  
  14. CREATE TABLE #AccountList
  15. (
  16. DAO VARCHAR(10),
  17. CustomerID VARCHAR(20),
  18. AccountCategory VARCHAR(20),
  19. Balance MONEY,
  20. Currency VARCHAR(6),
  21. AccountNumber VARCHAR(20),
  22. AccountStatus VARCHAR(50),
  23. AccountOpeningDate DATE,
  24. AccountLastTrans MONEY,
  25. AccountLastTransDate DATE,
  26. SystemDate DATE
  27. )
  28.  
  29. SET @Command_BulkInsert = '
  30. BULK INSERT #AccountList from ''' + @file + '''
  31. with (CODEPAGE = ''ACP'', FIRSTROW = ' + @startRow + ', LASTROW = ' + @endRow + ', FIELDTERMINATOR = ''|'', ROWTERMINATOR = ''' + CHAR(10)+ ''' , MAXERRORS=0)';
  32.  
  33. EXEC(@Command_BulkInsert)
  34.  
  35. UPDATE a SET a.statusId = 'inactive'
  36. FROM customer.customer AS c
  37. JOIN customer.account AS a ON a.actorId = c.actorId
  38. LEFT JOIN #AccountList AS aa ON aa.CustomerID = c.customerNumber AND aa.AccountNumber = a.accountNumber
  39. WHERE c.countryId = @countryId AND aa.AccountNumber IS NULL
  40.  
  41. INSERT INTO #AccountsForDelete
  42. SELECT MIN(accountId)
  43. FROM customer.customer AS c
  44. JOIN customer.account AS a ON a.actorId = c.actorId
  45. WHERE c.countryId = @countryId GROUP BY a.actorId, a.accountNumber
  46. HAVING COUNT(*) > 1
  47.  
  48. BEGIN TRY
  49.  
  50. DELETE customer.account WHERE accountId IN (SELECT accountId FROM #AccountsForDelete)
  51.  
  52. END TRY
  53. BEGIN CATCH
  54. DECLARE @errmsg nvarchar(2048) = error_message(), @severity tinyint = error_severity(), @state tinyint =error_state(),
  55. @errno int = error_number(), @lineno int = error_line()
  56. set @errmsg = 'Line ' + ltrim(str(@lineno)) + '. Errno ' + ltrim(str(@errno)) + ': ' + @errmsg
  57.  
  58. RAISERROR('%s', @severity, @state, @errmsg)
  59.  
  60. IF @@TRANCOUNT > 0
  61. ROLLBACK TRANSACTION
  62. END CATCH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement