2011-05-19 79 views
0

我使用sql server 2005作为asp.net项目。我想运行一个SQL文件,其中包含上一版本的所有数据库更改,以便将数据库轻松带到最新版本。SQL Server部署脚本

我基本上只有一堆alter table,create table,create index,alter view,调用stored proc等语句。但是我想把它包装在一个事务中,所以如果它的任何部分都失败了,那么所有的更改都不会通过。否则,它可能会造成一些非常麻烦的调试。

此外,如果您知道管理数据库部署的更好方法,请告诉我!

回答

0

我做一个PowerShell脚本类似using SMO.

伪代码将是:

$SDB = SourceDBObject 
$TDB = TargetDBObject 

ForEach $table in $SDB.Tables 
{ 
    Add an entry to a hash table with the name 
    and some attributes (rowcount, columns, datasize) 
} 

# Same thing for $TDB 

# Compare the two arrays, and make a list of all the ones that exist in the source but not in the target, or that are different 

# Same thing for Procs and Views 

# Pass this list to a SMO.Scripter as an UrnCollection object, and it will script them out in dependency order (it's an option), with drops 

# Wrap the script in a transaction and execute it on target server 

# Use SQLBulkCopy class to transfer data server-to-server 
0

您使用的是什么版本的Visual Studio?在Visual Studio 2010中,从我能记得的Visual Studio 2008 - 在“数据”下的菜单中,有两个选项 - “模式比较”和“数据比较”。这应该让你朝着正确的方向前进。

0
BEGIN TRANSACTION @TranName; 
USE AdventureWorks2008R2; 
DELETE FROM AdventureWorks2008R2.HumanResources.JobCandidate 
    WHERE JobCandidateID = 13; 

COMMIT TRANSACTION @TranName; 

你应该在一个事务中执行的一切

0

注意某些DDL语句必须是批处理的第一条语句(批次从交易分开)。 (GO是SSMS和SQLCMD中的默认批量分隔符)。