Skip to end of metadata
Go to start of metadata
--just replace _etblPostGLHist and PostGL in the script below (and feel free to improve it with variables)


SELECT 
    col1.COLUMN_NAME AS PostGL_Column,
    col1.DATA_TYPE AS PostGL_DataType,
    col2.COLUMN_NAME AS _etblPostGLHist_Column,
    col2.DATA_TYPE AS _etblPostGLHist_DataType,
    CASE 
        WHEN col1.COLUMN_NAME IS NOT NULL AND col2.COLUMN_NAME IS NOT NULL THEN 'Match'
        WHEN col1.COLUMN_NAME IS NOT NULL AND col2.COLUMN_NAME IS NULL THEN 'Missing in _etblPostGLHist'
        WHEN col1.COLUMN_NAME IS NULL AND col2.COLUMN_NAME IS NOT NULL THEN 'Missing in PostGL'
    END AS ComparisonResult,
    CASE 
        WHEN col1.COLUMN_NAME IS NOT NULL AND col2.COLUMN_NAME IS NULL 
        THEN 'ALTER TABLE _etblPostGLHist ADD ' + col1.COLUMN_NAME + ' ' + col1.DATA_TYPE + 
             CASE WHEN col1.CHARACTER_MAXIMUM_LENGTH IS NOT NULL 
                  THEN '(' + CAST(col1.CHARACTER_MAXIMUM_LENGTH AS NVARCHAR(50)) + ')' 
                  ELSE '' 
             END + ';'
        ELSE NULL
    END AS AlterTableScript
FROM 
    INFORMATION_SCHEMA.COLUMNS col1
FULL OUTER JOIN 
    INFORMATION_SCHEMA.COLUMNS col2
ON 
    col1.COLUMN_NAME = col2.COLUMN_NAME
    AND col2.TABLE_NAME = '_etblPostGLHist'
    AND col1.TABLE_NAME = 'PostGL'
WHERE 
    col1.TABLE_NAME = 'PostGL'
    OR col2.TABLE_NAME = '_etblPostGLHist'
ORDER BY 
    col1.COLUMN_NAME, col2.COLUMN_NAME;

  • No labels