Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to check if a field already exists in a table

How to check if a field already exists in a table

Scheduled Pinned Locked Moved Database
databasequestiontutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SWDevil
    wrote on last edited by
    #1

    Hi, I need to check if a field exists in a certain table (in an SQL database), and if not then add that field to the table. For Instance I have Table "A" that should contain field "a". So I want to check if field a is already defined for the table, and if not then add it. How can I d o this? Is there an SQL statement to check if a field exists? Thanks

    C 1 Reply Last reply
    0
    • S SWDevil

      Hi, I need to check if a field exists in a certain table (in an SQL database), and if not then add that field to the table. For Instance I have Table "A" that should contain field "a". So I want to check if field a is already defined for the table, and if not then add it. How can I d o this? Is there an SQL statement to check if a field exists? Thanks

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      SWDevil wrote:

      Is there an SQL statement to check if a field exists?

      IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTableName' AND COLUMN_NAME = 'MyColumnName') BEGIN ALTER TABLE .... END


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        SWDevil wrote:

        Is there an SQL statement to check if a field exists?

        IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTableName' AND COLUMN_NAME = 'MyColumnName') BEGIN ALTER TABLE .... END


        Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        S Offline
        S Offline
        SWDevil
        wrote on last edited by
        #3

        If I need to add more than one fields can I write than one ALTER statements: IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTableName' AND COLUMN_NAME = 'MyColumnName') BEGIN ALTER TABLE .... ALTER TABLE ... ALTER TABLE ... ALTER TABLE ... END

        C 1 Reply Last reply
        0
        • S SWDevil

          If I need to add more than one fields can I write than one ALTER statements: IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTableName' AND COLUMN_NAME = 'MyColumnName') BEGIN ALTER TABLE .... ALTER TABLE ... ALTER TABLE ... ALTER TABLE ... END

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          You can write as many ALTER TABLE statements as you like. Or you can put it all in one ALTER TABLE statement. It is up to you.


          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups