0

I need some SQL that checks if the column name exists and if so adds a '_2' to the end of the name. I am pulling the data from excel files, and there are a few instances where the excel headers are identical. So it might have Name, Number, Address, Address. So when I create the table I want the second one to be Address_2. But I have hundreds of these files so I am looking for a quick fix in the SQL when creating the table.

Maybe something like this:

CREATE Table X(
IF EXISTS colName (colName_2) (varchar 200)
colName (varchar 200)
)
J. Tate
  • 163
  • 1
  • 10
  • 3
    How you achieve that depends entirely on how you're generating the SQL for creating the table in the first place. Is that in Excel, in SQL, with a wizard, something else? There is no `WITH AUTO_RENAME_DUPLICATES` magic switch, so you'll have to fix up the statement regardless. (No, you can't put code inside a `CREATE TABLE`, it's a fixed, static entity. You can dynamically create the `CREATE TABLE` statement, of course, but that's another thing.) – Jeroen Mostert Oct 11 '17 at 10:38
  • Ok that answers the question then. I will have to rework the entire way I am importing the Excel data. Thank you! – J. Tate Oct 11 '17 at 10:43
  • Possible duplicate of [How to check if a column exists in SQL Server table](https://stackoverflow.com/questions/133031/how-to-check-if-a-column-exists-in-sql-server-table) – Tamas Rev Oct 11 '17 at 11:30

0 Answers0