dbMakeConsistent_mssqlserver.sql 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. -- Licensed Materials - Property of IBM
  2. --
  3. -- BI and PM: CM
  4. --
  5. -- (C) Copyright IBM Corp. 2008, 2009
  6. --
  7. -- US Government Users Restricted Rights - Use, duplication or disclosure
  8. -- restricted by GSA ADP Schedule Contract with IBM Corp.
  9. -- Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  10. -- Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  11. IF OBJECT_ID('makeCSConsistent') IS NOT NULL
  12. DROP PROC makeCSConsistent
  13. GO
  14. CREATE PROCEDURE makeCSConsistent AS
  15. DECLARE @tablename char(20)
  16. DECLARE @strsql Nvarchar(500)
  17. SET NOCOUNT ON
  18. DECLARE tables CURSOR FOR
  19. SELECT TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'CMID' and TABLE_NAME like 'CM%' and TABLE_SCHEMA = user_name()
  20. OPEN tables
  21. FETCH NEXT FROM tables INTO @tablename
  22. WHILE @@fetch_status = 0
  23. BEGIN
  24. SET @strsql = 'delete from ' + @tablename + ' where CMID not in (select CMID from CMOBJECTS)'
  25. EXEC sp_executesql @strsql
  26. FETCH NEXT FROM tables INTO @tablename
  27. END
  28. CLOSE tables
  29. DEALLOCATE tables
  30. SET @strsql = 'insert into CMOBJNAMES (CMID, LOCALEID, MAPDLOCALEID, ISDEFAULT, NAME) select CMID, 24, 24, 1, ''REPAIRED BY CONSISTENCY CHECK '' + CONVERT(nvarchar(255), newid()) from CMOBJECTS where CMID not in (select CMID from CMOBJNAMES)'
  31. EXEC sp_executesql @strsql
  32. SET @strsql = 'insert into CMPOLICIES (CMID) select CMID from CMOBJECTS where CMID not in (select CMID from CMPOLICIES)'
  33. EXEC sp_executesql @strsql
  34. SET @strsql = 'insert into CMSTOREIDS (CMID, STOREID) select CMID, substring(''i00000000000000000000000000000000'',1,33 - len(CONVERT(nvarchar(10), CMID))) + CONVERT(nvarchar(10), CMID) from CMOBJECTS where CMID not in (select CMID from CMSTOREIDS)'
  35. EXEC sp_executesql @strsql
  36. SET @strsql = 'update CMSTOREIDS set STOREID = NULL where UPPER(STOREID) in (select UPPERSTOREIDS.STOREID from (select b.CMID, UPPER(b.STOREID) as STOREID from CMSTOREIDS b) as UPPERSTOREIDS group by UPPERSTOREIDS.STOREID having COUNT(UPPERSTOREIDS.CMID) > 1)'
  37. EXEC sp_executesql @strsql
  38. SET @strsql = 'update CMSTOREIDS set STOREID = substring(''i00000000000000000000000000000000'',1,33 - len(CONVERT(nvarchar(10), CMID))) + CONVERT(nvarchar(10), CMID) where CMID in (select CMID from CMSTOREIDS where STOREID is null)'
  39. EXEC sp_executesql @strsql
  40. SET NOCOUNT OFF
  41. GO
  42. EXEC makeCSConsistent
  43. DROP PROC makeCSConsistent