dbCheckConsistency_mssqlserver.sql 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. CREATE TABLE #CMCHECKCONSISTENCY (INCONSISTENCY INT not null, DESCRIPTION varchar(255), CMID int)
  12. GO
  13. DROP PROC getInconsistentCMIDs
  14. GO
  15. -- CMSCRIPT_SKIP_COMMAND
  16. DROP PROC printInconsistentCMIDs
  17. GO
  18. CREATE PROCEDURE getInconsistentCMIDs AS
  19. DECLARE @tablename char(20)
  20. DECLARE @CMID int
  21. DECLARE @columnname varchar(30)
  22. DECLARE @strsql Nvarchar(500)
  23. SET NOCOUNT ON
  24. DECLARE tables CURSOR FOR
  25. SELECT TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'CMID' and TABLE_NAME like 'CM%' and TABLE_SCHEMA = user_name()
  26. OPEN tables
  27. FETCH NEXT FROM tables INTO @tablename
  28. WHILE @@fetch_status = 0
  29. BEGIN
  30. SET @strsql = 'insert into #CMCHECKCONSISTENCY (INCONSISTENCY,DESCRIPTION , CMID) select 1,''' + @tablename + ''', CMID from ' + @tablename + ' where CMID not in (select CMID from CMOBJECTS)'
  31. EXEC sp_executesql @strsql
  32. FETCH NEXT FROM tables INTO @tablename
  33. END
  34. CLOSE tables
  35. DEALLOCATE tables
  36. SET @strsql = 'insert into #CMCHECKCONSISTENCY (INCONSISTENCY,DESCRIPTION , CMID) select 2, ''CMOBJECTS'', CMID from CMOBJECTS where CMID not in (select CMID from CMOBJNAMES) or CMID not in (select CMID from CMPOLICIES)'
  37. EXEC sp_executesql @strsql
  38. SET @strsql = 'insert into #CMCHECKCONSISTENCY (INCONSISTENCY,DESCRIPTION , CMID) select 3, ''CMSTOREIDS'', CMID from CMSTOREIDS where STOREID is null'
  39. EXEC sp_executesql @strsql
  40. SET @strsql = 'insert into #CMCHECKCONSISTENCY (INCONSISTENCY,DESCRIPTION , CMID) select 4, ''CMSTOREIDS'', s.CMID from CMSTOREIDS s where UPPER(s.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)'
  41. EXEC sp_executesql @strsql
  42. SET NOCOUNT OFF
  43. GO
  44. -- CMSCRIPT_SKIP_COMMAND
  45. CREATE PROCEDURE printInconsistentCMIDs AS
  46. DECLARE @tablename char(20)
  47. DECLARE @CMID int
  48. DECLARE @columnname varchar(30)
  49. DECLARE @strsql Nvarchar(500)
  50. SET NOCOUNT ON
  51. DECLARE summary CURSOR FOR
  52. SELECT DESCRIPTION, CMID FROM #CMCHECKCONSISTENCY WHERE INCONSISTENCY = 1 ORDER BY DESCRIPTION, CMID ASC
  53. OPEN summary
  54. FETCH NEXT FROM summary INTO @tablename, @CMID
  55. IF @@fetch_status = 0
  56. BEGIN
  57. PRINT 'CMIDs not in CMOBJECTS table'
  58. PRINT 'Table Orphaned CMID'
  59. PRINT '----- -------------'
  60. END
  61. WHILE @@fetch_status = 0
  62. BEGIN
  63. PRINT @tablename + RTRIM(CONVERT(varchar(10), @CMID))
  64. FETCH NEXT FROM summary INTO @tablename, @CMID
  65. END
  66. CLOSE summary
  67. DEALLOCATE summary
  68. DECLARE summary CURSOR FOR
  69. SELECT DESCRIPTION, CMID FROM #CMCHECKCONSISTENCY WHERE INCONSISTENCY = 2 ORDER BY DESCRIPTION, CMID ASC
  70. OPEN summary
  71. FETCH NEXT FROM summary INTO @tablename, @CMID
  72. IF @@fetch_status = 0
  73. BEGIN
  74. PRINT ''
  75. PRINT 'CMIDs in CMOBJECTS table but not in CMOBJNAMES or CMPOLICIES tables'
  76. PRINT 'Table CMID'
  77. PRINT '----- -------------'
  78. END
  79. WHILE @@fetch_status = 0
  80. BEGIN
  81. PRINT @tablename + RTRIM(CONVERT(varchar(10), @CMID))
  82. FETCH NEXT FROM summary INTO @tablename, @CMID
  83. END
  84. CLOSE summary
  85. DEALLOCATE summary
  86. DECLARE summary CURSOR FOR
  87. SELECT DESCRIPTION, CMID FROM #CMCHECKCONSISTENCY WHERE INCONSISTENCY = 4 ORDER BY DESCRIPTION, CMID ASC
  88. OPEN summary
  89. FETCH NEXT FROM summary INTO @tablename, @CMID
  90. IF @@fetch_status = 0
  91. BEGIN
  92. PRINT ''
  93. PRINT 'CMIDs in CMSTOREIDS table which have non-unique STOREIDs'
  94. PRINT 'Table CMID'
  95. PRINT '----- -------------'
  96. END
  97. WHILE @@fetch_status = 0
  98. BEGIN
  99. PRINT @tablename + RTRIM(CONVERT(varchar(10), @CMID))
  100. FETCH NEXT FROM summary INTO @tablename, @CMID
  101. END
  102. CLOSE summary
  103. DEALLOCATE summary
  104. DECLARE summary CURSOR FOR
  105. SELECT DESCRIPTION, CMID FROM #CMCHECKCONSISTENCY WHERE INCONSISTENCY = 3 ORDER BY DESCRIPTION, CMID ASC
  106. OPEN summary
  107. FETCH NEXT FROM summary INTO @tablename, @CMID
  108. IF @@fetch_status = 0
  109. BEGIN
  110. PRINT ''
  111. PRINT 'CMIDs in CMSTOREIDS table where STOREID is null'
  112. PRINT 'Table CMID'
  113. PRINT '----- -------------'
  114. END
  115. WHILE @@fetch_status = 0
  116. BEGIN
  117. PRINT @tablename + RTRIM(CONVERT(varchar(10), @CMID))
  118. FETCH NEXT FROM summary INTO @tablename, @CMID
  119. END
  120. CLOSE summary
  121. DEALLOCATE summary
  122. SET NOCOUNT OFF
  123. GO
  124. EXEC getInconsistentCMIDs
  125. GO
  126. -- CMSCRIPT_SKIP_COMMAND
  127. EXEC printInconsistentCMIDs
  128. GO
  129. DROP PROC getInconsistentCMIDs
  130. GO
  131. -- CMSCRIPT_SKIP_COMMAND
  132. DROP PROC printInconsistentCMIDs
  133. GO
  134. -- CMSCRIPT_SKIP_COMMAND
  135. DROP TABLE #CMCHECKCONSISTENCY
  136. GO