Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am working on GIS Database with number of tables that are similar the challenges in this tables they don't have the same number of column e.g. other table A has 5 table B has 3.
what I want to do is to make all the tables have the same number of column e.g. if table A has 5 column and Table B must have 5 columns also not 3 because all those tables have the same data.
I want to achieve this by writing sql script that gone create all the columns that are not in the table if the columns is already the it must ignore it and add other columns

What I have tried:

Below is the script that I tried the challenge with this one I have to insert one column at time, what I want is to insert multiple columns that are not included in the table once.

USE GIS_DOD_Working
Go
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '[dbo].[LO17_BUILDINGS_F000]' AND COLUMN_NAME = 'CAT')
BEGIN

    ALTER TABLE [dbo].[LO17_BUILDINGS_F000] ADD 
       [CAT]  nvarchar (254) NULL 

    

END
Posted
Updated 13-Mar-18 3:49am
Comments
CHill60 13-Mar-18 8:24am    
Sounds like your database design is poor. Multiple tables with the same columns screams "do some normalization!" to me
thembale 13-Mar-18 9:23am    
yes the design is poor that what am trying to solve now

1 solution

As mentioned in the comments, this is an extremely bad database design.

But if you just want to generate a list of scripts to add the missing columns to the tables, something like this should work:
SQL
WITH cteTables (Name) As
(
    -- List of the tables to modify:
              SELECT N'[dbo].[Table1]'
    UNION ALL SELECT N'[dbo].[Table2]'
    UNION ALL SELECT N'[dbo].[Table3]'
),
cteColumns (Name, Definition) As
(
    -- List of the columns to create:
              SELECT N'Column1', N'nvarchar(254) NULL'
    UNION ALL SELECT N'Column2', N'nvarchar(254) NULL'
),
cteToCreate As
(
    SELECT
        T.Name As TableName,
        C.Name As ColumnName,
        C.Definition
    FROM
        cteTables As T
        CROSS JOIN cteColumns As C
    WHERE
        Not Exists
        (
            SELECT 1
            FROM sys.columns As E
            WHERE E.object_id = OBJECT_ID(T.Name)
            And E.name = C.Name
        )
)
SELECT
    N'ALTER TABLE ' + T.TableName + N' ADD '
    + STUFF(
        (
            SELECT N', ' + QUOTENAME(C.ColumnName) + N' ' + C.Definition 
            FROM cteToCreate As C 
            WHERE C.TableName = T.TableName 
            ORDER BY C.ColumnName 
            FOR XML PATH(''), TYPE
        ).value('.', 'varchar(max)'), 
        1, 2, N'')
    + N';'
FROM
    cteToCreate As T
GROUP BY
    TableName
;

If you execute that, you'll get a list of commands you need to execute to create the missing columns. For example:
ALTER TABLE [dbo].[Table1] ADD [Column2] nvarchar(254) NULL;
ALTER TABLE [dbo].[Table3] ADD [Column1] nvarchar(254) NULL, [Column2] nvarchar(254) NULL;
 
Share this answer
 
Comments
thembale 14-Mar-18 7:34am    
Thanks You great solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900