I made a slight change to your script as tpprmf is what
determines if an index is a primary key which isn't always tpinid = 1.
-- File : Find missing indexes.sql
-- Purpose: To find indexes in OneWorld that are not generated in the database
-- Author : Jim Smith (
[email protected])
--
--
-- Select database
--
USE jde_crp
GO
SET NOCOUNT ON
DECLARE @OW_Table_Name VARCHAR(20)
DECLARE @OW_Index_Name VARCHAR(20)
DECLARE @OW_Index_ID NUMERIC
DECLARE @OW_Primary_Key CHAR(1)
DECLARE @filler VARCHAR(20)
--
-- Create temporary table to store user defined table names
--
--CREATE TABLE #TempTable (name VARCHAR(255) COLLATE Latin1_General_CI_AI PRIMARY KEY)
--
-- NOTE: Notice that there is a collation attribute specified in the above statement
-- If your Central Objects database has the same collation as your target database
-- then simply use the statement below. However, if for some reason there is a
-- collation mismatch between both databases, then modify the above statement to
-- match your target database.
--
CREATE TABLE #TempTable (name VARCHAR(255) PRIMARY KEY)
INSERT INTO #temptable SELECT DISTINCT name FROM ..sysobjects WHERE xtype = 'U'
--
-- For each OneWorld index defined in the Primary Index Header table (F98712),
-- verify if it has been generated in the database selected in the beginning of the
-- script.
--
DECLARE OW_Index_Csr CURSOR FORWARD_ONLY FOR
SELECT tpobnm, tpinid, tpprmf FROM jde_py7334.py7334.f98712 WHERE tpobnm IN (SELECT name FROM #TempTable) ORDER BY tpobnm
FOR READ ONLY
OPEN OW_Index_Csr
FETCH NEXT FROM OW_Index_Csr INTO @OW_Table_Name, @OW_Index_ID, @OW_Primary_Key
WHILE (@@FETCH_STATUS = 0) BEGIN
--IF(@OW_Index_ID = 1)
IF(@OW_Primary_Key = '1')
SET @OW_Index_Name = RTRIM(@OW_Table_Name) + '_PK'
ELSE
SET @OW_Index_Name = RTRIM(@OW_Table_Name) + '_' + CAST(@OW_Index_ID AS VARCHAR(3))
SET @filler = ''
SELECT @filler = name FROM jde_crp..sysindexes WHERE name = @OW_Index_Name
IF (@filler = '')
PRINT @OW_Index_Name
FETCH NEXT FROM OW_Index_Csr INTO @OW_Table_Name, @OW_Index_ID, @OW_Primary_Key
END
CLOSE OW_Index_Csr
DEALLOCATE OW_Index_Csr
DROP TABLE #TempTable