Skip to end of metadata
Go to start of metadata

todo:

  • change table name to variable (both in selects and in alter table suggestion)
  • indicate where to change database name(s)










WITH db1_columns AS (
    SELECT
        COLUMN_NAME,
        DATA_TYPE,
        CHARACTER_MAXIMUM_LENGTH
    FROM
        [Gender Links (Online DB)].INFORMATION_SCHEMA.COLUMNS
    WHERE
        TABLE_NAME = 'Accounts'
),
db2_columns AS (
    SELECT
        COLUMN_NAME,
        DATA_TYPE,
        CHARACTER_MAXIMUM_LENGTH
    FROM
        INFORMATION_SCHEMA.COLUMNS
    WHERE
        TABLE_NAME = 'Accounts'
)
SELECT
    COALESCE(db1.COLUMN_NAME, db2.COLUMN_NAME) AS COLUMN_NAME,
    db1.DATA_TYPE AS DATA_TYPE_DB1,
    db2.DATA_TYPE AS DATA_TYPE_DB2,
    db1.CHARACTER_MAXIMUM_LENGTH AS MAX_LENGTH_DB1,
    db2.CHARACTER_MAXIMUM_LENGTH AS MAX_LENGTH_DB2,
    CASE
        WHEN db1.COLUMN_NAME IS NOT NULL AND db2.COLUMN_NAME IS NOT NULL THEN
            CASE
                WHEN db1.DATA_TYPE = db2.DATA_TYPE AND
                     (db1.CHARACTER_MAXIMUM_LENGTH = db2.CHARACTER_MAXIMUM_LENGTH OR
                      (db1.CHARACTER_MAXIMUM_LENGTH IS NULL AND db2.CHARACTER_MAXIMUM_LENGTH IS NULL))
                THEN 'Match'
                ELSE 'Mismatch'
            END
        WHEN db1.COLUMN_NAME IS NOT NULL AND db2.COLUMN_NAME IS NULL THEN 'MissingInDB2'
        WHEN db1.COLUMN_NAME IS NULL AND db2.COLUMN_NAME IS NOT NULL THEN 'ExtraInDB2'
    END AS ComparisonResult,
    CASE
        WHEN db1.COLUMN_NAME IS NOT NULL AND db2.COLUMN_NAME IS NOT NULL AND
             (db1.DATA_TYPE <> db2.DATA_TYPE OR db1.CHARACTER_MAXIMUM_LENGTH <> db2.CHARACTER_MAXIMUM_LENGTH) THEN
            'ALTER TABLE Accounts ALTER COLUMN ' + QUOTENAME(db2.COLUMN_NAME) + ' ' + db1.DATA_TYPE +
            CASE
                WHEN db1.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar') THEN
                    CASE WHEN db1.CHARACTER_MAXIMUM_LENGTH = -1 THEN '(MAX)'
                         ELSE '(' + CAST(db1.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')' END
                ELSE ''
            END + ';'
        WHEN db1.COLUMN_NAME IS NOT NULL AND db2.COLUMN_NAME IS NULL THEN
            'ALTER TABLE Accounts ADD ' + QUOTENAME(db1.COLUMN_NAME) + ' ' + db1.DATA_TYPE +
            CASE
                WHEN db1.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar') THEN
                    CASE WHEN db1.CHARACTER_MAXIMUM_LENGTH = -1 THEN '(MAX)'
                         ELSE '(' + CAST(db1.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')' END
                ELSE ''
            END + ';'
        WHEN db1.COLUMN_NAME IS NULL AND db2.COLUMN_NAME IS NOT NULL THEN
            'ALTER TABLE Accounts DROP COLUMN ' + QUOTENAME(db2.COLUMN_NAME) + ';'
        ELSE ''
    END AS SuggestedAlterScript
FROM
    db1_columns db1
FULL OUTER JOIN
    db2_columns db2 ON db1.COLUMN_NAME = db2.COLUMN_NAME
WHERE
    (db1.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext') OR
     db2.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext'))
ORDER BY
    COLUMN_NAME;






  • No labels