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. Adding multiple columns with condition

Adding multiple columns with condition

Scheduled Pinned Locked Moved Database
questiondatabasehelp
6 Posts 3 Posters 22 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
    simpledeveloper
    wrote on last edited by
    #1

    Hi - I have an alter command written as below:

    ALTER TABLE [dbo].[Cases]
    ADD [DocketNumber] NVARCHAR (50) NULL,
    [AdministrativeHearingDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
    [Locations] NVARCHAR (1000) NULL,
    [InterpreterNeeded] BIT DEFAULT ((0)) NOT NULL,
    [VoluntaryDisclosure] BIT DEFAULT ((0)) NOT NULL,
    [PenaltyAdjustment] FLOAT (53) DEFAULT ((0)) NOT NULL,
    [FinancialHardship] INT NULL,
    [AdministrativeHearing] INT NULL;

    I am assuming this statement is adding all those columns so How can I write conditional sql statement that says if exists then add, do I have to write for each individual column or is there any way I can write all this with one sql statement? Any help please? Thanks in advance.

    M J 2 Replies Last reply
    0
    • S simpledeveloper

      Hi - I have an alter command written as below:

      ALTER TABLE [dbo].[Cases]
      ADD [DocketNumber] NVARCHAR (50) NULL,
      [AdministrativeHearingDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
      [Locations] NVARCHAR (1000) NULL,
      [InterpreterNeeded] BIT DEFAULT ((0)) NOT NULL,
      [VoluntaryDisclosure] BIT DEFAULT ((0)) NOT NULL,
      [PenaltyAdjustment] FLOAT (53) DEFAULT ((0)) NOT NULL,
      [FinancialHardship] INT NULL,
      [AdministrativeHearing] INT NULL;

      I am assuming this statement is adding all those columns so How can I write conditional sql statement that says if exists then add, do I have to write for each individual column or is there any way I can write all this with one sql statement? Any help please? Thanks in advance.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Are you asking how to add a specific column only if a condition exists? That is not possible. You could change the structure to use an Attribute table Cases CaseID - PK DocketNumber Date Locations (should this be an attribute as many are implied) Attributes AttrID CaseID Key - eg InterpreterNeeded Value - Y (I would use a string type for the value field) Then you can select from Cases and inner join the Attributes needed for a particular query.

      Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

      1 Reply Last reply
      0
      • S simpledeveloper

        Hi - I have an alter command written as below:

        ALTER TABLE [dbo].[Cases]
        ADD [DocketNumber] NVARCHAR (50) NULL,
        [AdministrativeHearingDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
        [Locations] NVARCHAR (1000) NULL,
        [InterpreterNeeded] BIT DEFAULT ((0)) NOT NULL,
        [VoluntaryDisclosure] BIT DEFAULT ((0)) NOT NULL,
        [PenaltyAdjustment] FLOAT (53) DEFAULT ((0)) NOT NULL,
        [FinancialHardship] INT NULL,
        [AdministrativeHearing] INT NULL;

        I am assuming this statement is adding all those columns so How can I write conditional sql statement that says if exists then add, do I have to write for each individual column or is there any way I can write all this with one sql statement? Any help please? Thanks in advance.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        Try this:

        IF NOT EXISTS (
        SELECT *
        FROM sys.columns
        WHERE object_id = OBJECT_ID(N'dbo.Cases')
        AND name = 'DocketNumber'
        )
        BEGIN
        ALTER TABLE [dbo].[Cases]
        ADD [DocketNumber] NVARCHAR (50) NULL
        END

        But you have to check and add every column separately.

        Wrong is evil and must be defeated. - Jeff Ello

        S 1 Reply Last reply
        0
        • J Jorgen Andersson

          Try this:

          IF NOT EXISTS (
          SELECT *
          FROM sys.columns
          WHERE object_id = OBJECT_ID(N'dbo.Cases')
          AND name = 'DocketNumber'
          )
          BEGIN
          ALTER TABLE [dbo].[Cases]
          ADD [DocketNumber] NVARCHAR (50) NULL
          END

          But you have to check and add every column separately.

          Wrong is evil and must be defeated. - Jeff Ello

          S Offline
          S Offline
          simpledeveloper
          wrote on last edited by
          #4

          Thanks a lot to add another column - AdministrativeHearingDate, do I need to have another if statement? And thanks for jumping in and helping me buddy.

          J 1 Reply Last reply
          0
          • S simpledeveloper

            Thanks a lot to add another column - AdministrativeHearingDate, do I need to have another if statement? And thanks for jumping in and helping me buddy.

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Yes.

            Wrong is evil and must be defeated. - Jeff Ello

            S 1 Reply Last reply
            0
            • J Jorgen Andersson

              Yes.

              Wrong is evil and must be defeated. - Jeff Ello

              S Offline
              S Offline
              simpledeveloper
              wrote on last edited by
              #6

              Thank you my friend

              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