Replace the column with another column when it does not exist

  • I have a script that loops through a series of tables to send data to a table from each of the tables. My issue is that not all tables have the columns I need in them. What I would like is to replace the column with another column when it does not exist. Something like below

    Select Misisng_Column(A.Name, replace with B.Name) as Name

    FROM SomeTable A

    Cross Join (Select Name FROM AnotherTable) B

    AnotherTable has one record in it. To avoid a Cartesian issue. Like I said just an example

    In my real script the table aliased as A is from a list of tables in a sys.tables query that loops through to the end.

    Any help would be greatly appreciated.

  • If you're using the sys tables (i.e. views) as source of the table names, use the columns view to identify the required column name & store this along with the table name.

    Then use dynamic SQL to generate the SQL statement.

  • laurie-789651 (6/25/2014)


    If you're using the sys tables (i.e. views) as source of the table names, use the columns view to identify the required column name & store this along with the table name.

    Then use dynamic SQL to generate the SQL statement.

    +1

    Cheers

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply