Once the database upgrade program

Database upgrade program

 

First, the problem faced

Database upgrade in the project is a matter frequently encountered, this work is more complicated, especially online database upgrade need to be very careful, we first look at the problem usually faced:

1, the table changes, including the addition of a field, the field type or the length modified, replaced the primary key and the like. For upgrades table can not delete reconstruction, you need to be modified individually, or write scripts to upgrade.

2. Modify modify, view of view is relatively simple, nothing more than an increase of the field, the field is canceled, does not affect the underlying data. Upgrading view can delete reconstruction.

3, the same modifications, changes and view the stored procedure stored procedure, you can delete reconstruction, both of which can be done by the script.

4, modify the function, if used in the project to upgrade the functions and stored procedures, functions, like, not repeat them.

5, the above basically covers most of the database upgrade scenarios, such work can be done in a database management tool, you can also write scripts to complete. I want to say this is not a problem, but sometimes we do not know that table, the field is changed, unless you have a place to make a record every change, however, when the upgrade will inevitably fall.

Our company has a development platform, which has developed a platform Invoicing, set oa system, Invoicing and extends two industry Invoicing Edition: shoes and medicine pass through, are interested in the above products can Quguan network www.hfbpm.com trial, the product line as shown below:

 

Database and basic functions are inherited, namely Invoicing use development platform in the table and shoes on medicine through the use of generic inventory tables, oa use development platform in the table, then the database upgrade between them very often, just getting by local manual tweaks, targeted upgrades, and later we found the workload is very large, and often wrong.

Is there a way to once and for all, the difference between the database upgrade? As long as ideology is not landslide solutions than problems!

Approach is definitely yes.

Second, the solution

To resolve the database upgrade, mainly to resolve the upgrade for tables, views, stored procedures, functions, followed by three relatively simple, you can delete reconstruction, the difficulty is how to get the script to create the three? Just to get a complete script, as you can sql statement is executed, mssqlServer also given method (the following will introduce specific). Upgrade table to be relatively trouble, because the table can not be deleted reconstruction, must be columns, primary keys, constraints and other itemized compare upgrade to the new table does not provide a method to get the script to create the table, has to handle.

The following details the upgrade process.

1, table creation

MsSqlServer provides no way to get the script to create a table, you need to generate their own to create a script based on column attributes for which we write a stored procedure Sys_TableScript_MSSQL to do it, stored procedure code in the following figure:

 

Due to space constraints, detailed code is not posted, this method is also received from the blog garden, a little change about the results after the implementation of the output is as follows:

 

The acquisition is a complete script to create tables, the script as a normal sql statement can be executed.

2, Table upgrade

When the table already exists for the column to upgrade, if the column does not create a direct, if the column exists, whether to upgrade judged on the length, type, number of decimal places, allowed to be null, the default value change has occurred, as long as there is above it a change will occur upgrade.

Sql script to create columns, as follows:

The ALTER  TABLE table name ADD Column Name Type   not  null  default  ' defaults '

E.g

ALTER TABLE dx_ZhiBan ADD  leaderName  varchar(50)  not null default '未填'

如果列已经存在需要使用修改列的sql脚本,如下:

ALTER TABLE 表名 alter column 列名 类型 not null

例如

alter table dx_ZhiBan alter column Leader nvarchar(50) not null

修改列时如果修改默认值,修改列的脚本不支持直接修改默认值,因为列一旦创建了默认值,那么就创建了一个约束,需要先删除这个约束,再重新创建默认值。删除默认值约束需要先找到默认值约束的名字,再执行删除约束脚本。查找默认值约束的sql脚本如下:

select c.name from sysconstraints a

inner join syscolumns b on a.colid=b.colid

inner join sysobjects c on a.constid=c.id

where a.id=object_id('表名') and b.name='列名'

找到约束的名字如下图:

 

删除默认值约束的sql脚本如下图:

alter table 表名 drop constraint 默认值约束名

例如:

alter table dx_ZhiBan drop constraint DF__dx_ZhiBan__Leade__3FE65219

删除默认值后,再执行创建默认值的sql脚本,如下:

alter table 表名 add default '默认值' for 列名 with values

例如:

alter table dx_ZhiBan add default '未填' for Leader with values

表的升级除了列还包括主键,主键的升级和默认值类似(因为他们都属于约束),需要先删除原来,再创建新的。查找主键约束的sql脚本如下:

  Select name from sysobjects where Parent_Obj=OBJECT_ID('表名') and xtype='PK'

例如:

 Select name from sysobjects where Parent_Obj=OBJECT_ID('dx_ZhiBan') and xtype='PK'

查找结果如下:

 

删除主键约束的sql脚本如下:

Alter table dx_ZhiBan Drop PK_dx_ZhiBan

创建主键的sql脚本如下:

ALTER TABLE dx_ZhiBan ADD PRIMARY KEY (ID, leader )

注意,联合主键用逗号分隔,另外,需要说明的是在升级之前要判断主键是否需要升级,如果主键没有变化就不需要升级。

3、视图升级

视图升级过程较简单,删除掉重新创建即可。删除视图的sql脚本如下:

drop view 视图名

获取视图创建脚本的sql脚本,如下:

EXEC sp_helptext @objname='视图名称'

执行后结果如下图:

 

获取到该脚本后,当做普通的sql语句执行即可。

4、存储过程、函数升级

二者的升级和视图类似,不再赘述,不同的是删除存储过程的sql脚本是:

drop procedure 存储过程名

删除函数的sql脚本是:

drop function 函数名

5、数据库升级工具

数据库的升级是都能通过sql脚本来完成的,把这些脚本管理起来需要借助程序来完成,我们使用net的WinForm来编写程序。如下图:

 

使用这个工具可以选择那些对象需要升级(没有勾选的不升级),升级的时候能看到进度和升级结果。

本方案并不是十全十美的,有些问题还没解决,例如列名称修改、如何删除多余的列等。其他不当之处欢迎大家留言指正。

Guess you like

Origin www.cnblogs.com/legweifang/p/11332769.html