How to Generate Alter Table Script Dynamically.

  • Hi Friends,

    I want to update the tables structure from DevelopmentDB to PreProductionDB and then to ProductionDB

    is there any way of getting the alter script for the table if any changes in the table structue in developmentdb that we need to update in the preproductiondb and productiondb.

    In my scenario, i want to pass the table name and i have to compare the table struction in both dbs and update accordingly.. programatically...

    please help me in this scenario...

    Thank you.

    ---

    Santooo.

  • Hi

    check this link for free tools

    http://www.sqlservercentral.com/articles/Tools/64908/.

    You will also find T-SQL code for the same if your search this site.

    The best way of doing this is to generate the alter script for each database modification at the time of modification and then apply it to testing/production.

    What we do is generate the alter script for each modification, keep a record of it in a XL file. XL File and the change(alter) script is kept in VSS. For each release we mark whether the change script has been applied to testing/production or not.

    "Keep Trying"

  • ok,

    But in my scenario i want the Alter Script, to update. Coz, we are developing a tool to update the other dbs, for that purpose i want to generate the script and finally i will execute that script...

    There is a possibility of generating the script from the syscolumns table right, i am trying this method... if anyone is having better solution please share..

    thank you .

    ---

    Santooo

  • Hi

    Use syscolumns or Information_Schema.columns. I have a script which compares two databases and generates the differences. Let me know if you need it.

    "Keep Trying"

  • hi all,

    Here is the Code i am using for building the alter script, i am building the alter script for each and every column, and finally i am using that.

    the below code will not generate the constraints scripts.,..

    Dear Chirag,

    please correct me if anything wrong with this code. and please post how we can do with the Information_Schema.columns.

    SELECT

    '[' + SC.Name + '] ' + '[' + ST.NAME + ']' +

    CASE

    WHEN (ST.Name='bigint') THEN ''

    WHEN (ST.Name='binary') THEN ' (' + CONVERT(varchar(10), SC.Length) + ')'

    WHEN (ST.Name='bit') THEN ''

    WHEN (ST.Name='char') THEN ' (' + CONVERT(varchar(10), SC.Length) + ')'

    WHEN (ST.Name='datetime') THEN ''

    WHEN (ST.Name='decimal') THEN ' (' + CONVERT(varchar(10), SC.Prec) + ', ' + CONVERT(varchar(10), SC.Scale) + ')'

    WHEN (ST.Name='float') THEN ''

    WHEN (ST.Name='image') THEN ''

    WHEN (ST.Name='int') THEN ''

    WHEN (ST.Name='money') THEN ''

    WHEN (ST.Name='nchar') THEN ' (' + CONVERT(varchar(10), SC.Length) + ')'

    WHEN (ST.Name='ntext') THEN ''

    WHEN (ST.Name='numeric') THEN '(' + CONVERT(varchar(10), SC.Prec) + ', ' + CONVERT(varchar(10), SC.Scale) + ')'

    WHEN (ST.Name='nvarchar') THEN ''

    WHEN (ST.Name='real') THEN ''

    WHEN (ST.Name='smalldatetime') THEN ''

    WHEN (ST.Name='smallint') THEN ''

    WHEN (ST.Name='smallmoney') THEN ''

    WHEN (ST.Name='sql_variant') THEN ''

    WHEN (ST.Name='sysname') THEN ''

    WHEN (ST.Name='text') THEN ''

    WHEN (ST.Name='timestamp') THEN ''

    WHEN (ST.Name='tinyint') THEN ''

    WHEN (ST.Name='uniqueidentifier') THEN ''

    WHEN (ST.Name='varbinary') THEN ' (' + CONVERT(varchar(10), SC.Length) + ')'

    WHEN (ST.Name='varchar') THEN ' (' + CONVERT(varchar(10), SC.Length) + ')'

    ELSE ' ' END + ' ' + CASE WHEN SC.isnullable = 0 THEN ' NOT NULL ' ELSE ' NULL ' END

    + ' ' + CASE WHEN SCmnts.text is not null THEN ' DEFAULT ' + SCmnts.text ELSE '' END AS ColumnScript,

    SC.Name, SC.xtype, SC.xusertype, SC.length, SC.xprec, SC.xscale, SC.cdefault, SC.type, SC.usertype, SC.prec, SC.scale, SC.isnullable

    FROM SYSCOLUMNS SC

    INNER JOIN SYSTYPES ST ON SC.xusertype = ST.xusertype

    LEFT JOIN SYSCOMMENTS SCmnts ON SC.cdefault = SCmnts.ID

    WHERE SC.ID IN (SELECT ID FROM SysObjects WHERE [NAME] ='sysobjects')

    the above code will not give the

    Thank you,

    ----

    Santooo

  • Hi

    There are somany tools which provides this, but all these are based on comparing the DevDB with PrePordDB and list out the difference between these and prepare alter script for this. But again this is also not the efficient way to do this.

    Because in DevDB you may 10 new columns added but out of this you want to promotye only 5 columns.. In this case how would the tool knows that it has to preare only for 5 columns.

    In Simple words, when you are promoting your changes to the other environments then you have to be very sure of what you are promoting.

    Thanks -- Vj

  • Hi Vijaya,

    What you said is true,

    Here i am developing the tool to update the DevDB to ProdDB,

    not only table structure of Main table.. and some other supporting tables which is holding all required data to run the application..(our framework tables.)

    i have to do using dotnet coding..

    Thank you.

    Santosh..

  • santoooo (2/2/2009)


    hi all,

    please correct me if anything wrong with this code. and please post how we can do with the Information_Schema.columns.

    - no identites;

    - no collations;

    - "DEFAULT" is actually a constraint, probably named, this script will lose it;

    - no other constraints;

    - column name with "[" or "]" in it will cause an error (look for QIOTENAME in BOL)

    etc.

    _____________
    Code for TallyGenerator

Viewing 8 posts - 1 through 7 (of 7 total)

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