ADD A COLUMN TO A TABLE IN T-SQL

  • Harry Drenth

    Say Hey Kid

    Points: 670

    I want to add a column to a table in T-SQL by using the following command:

    ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL

    This works the first time when the column doesn't exist yet.

    However I want to prevent the alter table command to be executed in case the column already exists.

    I can test if it exists with the following command:

    SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'DBID_TRIP'

    AND COLUMN_NAME = 'SESSIONID'

    How do I combine the test and the alter table command in T-SQL for SQL CE since BEGIN and END statements are not supported?

    My solution would be:

    IF NOT EXISTS (SELECT *

    WHERE NOT EXISTS (SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'DBID_TRIP'

    AND COLUMN_NAME = 'SESSIONID')

    BEGIN

    ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL

    END

    Anyone an idea or solution in T-SQL.

    Regards,

    Harry Drenth

  • Adi Cohn-120898

    SSC-Dedicated

    Points: 33944

    I don’t have SQL Server CE edition, so I can’t check it, but why do you think that you need to use begin end block? Can’t you just use this code?

    IF NOT EXISTS (SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'DELIVERYINTRIP'

    AND COLUMN_NAME = 'PODID')

    ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Harry Drenth

    Say Hey Kid

    Points: 670

    hi adi,

    This is the result:

    Major Error 0x80040E14, Minor Error 25501

    > IF NOT EXISTS (SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'DELIVERYINTRIP'

    AND COLUMN_NAME = 'PODID')

    ALTER TABLE DELIVERYINTRIP ADD PODID UNIQUEIDENTIFIER NULL

    There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = IF ]

  • dharris-1058198

    SSC Journeyman

    Points: 80

    From "SQL Server 2005 Compact Edition Books Online"

    "Queries that can typically be run on SQL Server Compact Edition can also run on Microsoft SQL Server; however, many of the features of Transact-SQL are absent from SQL Server Compact Edition, and only a single SQL statement can be executed in a command.

    ...

    Note:

    SQL Server Compact Edition does not support scripting because scripting depends on SQL Management Objects (SMO). SMO is not a feature of SQL Server Compact Edition."

    Technically, an IF ... THEN ... construct is two statements (the IF evaluation and the THEN action). Also, while "IF" and "THEN" are reserved words in SSCE (for compatibility with the rest of the SQL world), they are not defined in the grammar available for SSCE.

  • vimal_panchal

    SSC Enthusiast

    Points: 199

    I am facing the same problem, is anybody found any solution for this?

    How can we achieve this?

    Is there any alternative for this?

  • SQLRNNR

    SSC Guru

    Points: 281243

    vimal_panchal (4/1/2010)


    I am facing the same problem, is anybody found any solution for this?

    How can we achieve this?

    Is there any alternative for this?

    You would be better served by starting a new thread on this.

    Post your question with relevant error messages, queries, table structures and sample data if necessary.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Atif-ullah Sheikh

    SSChampion

    Points: 12495

    Check this...

    http://msdn.microsoft.com/en-us/library/ms174123(SQL.90).aspx

    And you can use if condition in your application code. Generate data set from the query to check the column using Information_Schema and take decission at application level code.

    Stored Procedures are NOT supported in SQL Server CE.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • shashi24365

    SSC Rookie

    Points: 42

    when m running this query <>go

    IF NOT EXISTS (SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = '[DBO].CUSTOMER'

    AND COLUMN_NAME = 'CUSTOMER_ID')

    ALTER TABLE [DBO].CUSTOMER ADD CUSTOMER_ID int NULL

    go

    </>

    AND ITS GIVING ERROR:

    <>

    Msg 2705, Level 16, State 4, Line 5

    Column names in each table must be unique. Column name 'CUSTOMER_ID' in table '[DBO].CUSTOMER' is specified more than once.

    </>

    BUT HERE I WANT "Query executed successfully" is it possible to do this???

    please help!!!! thanks in advance....

  • dmoore2168

    SSC Journeyman

    Points: 88

    Remove the [DBO] from the WHERE table_name = '[DBO].CUSTOMER'

    Your IF NOT EXISTS should look like:

    IF NOT EXISTS (SELECT *

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'CUSTOMER'

    AND COLUMN_NAME = 'CUSTOMER_ID')

    ALTER TABLE [DBO].CUSTOMER ADD CUSTOMER_ID int NULL

Viewing 9 posts - 1 through 9 (of 9 total)

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