SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Generate Alter Table Script Dynamically.


How to Generate Alter Table Script Dynamically.

Author
Message
santoooo
santoooo
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 253
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.
ChiragNS
ChiragNS
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6661 Visits: 1865
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"
santoooo
santoooo
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 253
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
ChiragNS
ChiragNS
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6661 Visits: 1865
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"
santoooo
santoooo
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 253
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
Vijaya Kadiyala
Vijaya Kadiyala
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3015 Visits: 409
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

Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



santoooo
santoooo
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 253
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..
Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25531 Visits: 12464
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search