Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to Generate Alter Table Script Dynamically. Expand / Collapse
Author
Message
Posted Thursday, January 29, 2009 4:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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.
Post #645738
Posted Thursday, January 29, 2009 4:58 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 4:53 AM
Points: 2,366, Visits: 1,844
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"
Post #645746
Posted Thursday, January 29, 2009 11:59 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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
Post #646675
Posted Friday, January 30, 2009 12:22 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 4:53 AM
Points: 2,366, Visits: 1,844
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"
Post #646680
Posted Monday, February 2, 2009 12:35 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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
Post #647835
Posted Tuesday, February 3, 2009 11:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, 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



Post #649166
Posted Tuesday, March 17, 2009 8:22 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 10:13 PM
Points: 83, 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..
Post #677500
Posted Tuesday, March 17, 2009 3:59 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:16 PM
Points: 4,576, Visits: 8,347
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.
Post #677961
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse