2011-08-26 54 views
1

我已经做了一些谷歌搜索和搜索,但我一直没能找到关于此事的很多帮助。我正在设计一个利用Microsoft SQL Server 2008服务器的Web服务。相关结构是这样的...有一个主数据库,其中包含所有主要帐户/公司信息(公司名称,地址等)。此外,每个帐户/公司都有数据库,其中包含该帐户(用户,设置等)的所有相关(元?)数据。将SQL Server 2008数据库架构克隆到新的数据库 - 以编程方式(PHP?)

SQL2008 Server 
|---MainDatabase 
|-------Accounts Table 
|-----------Account Record where ID = 1 
|-----------Account Record where ID = 2 
|-----------Account Record where ID = 3 
|---AccountDatabase00001 
|-------Users Table for account where ID = 1 
|---AccountDatabase00001 
|-------Users Table for account where ID = 2 

当一个新的帐户创建(比方说,ID = 3),我试图找出一种方法来克隆表模式和AccountDatabase0001意见(不是数据)到一个名为AccountDatabase00003新的数据库。只要能以某种方式从PHP页面调用,我几乎可以使用任何语言来执行重复操作。

有没有人遇到过这样的PHP脚本,或者任何其他此类语言的脚本?有没有一个命令我可以发送SQL服务器为我做这个?我敢肯定,我可以通过手动遍历结构和编写SQL语句来创建每个对象,但我希望更简单。

回答

0

我发现了一个远程简单的方法来完成这个在PHP中的一个自定义编写的存储过程,只需要在您想克隆的数据库中存在一点帮助(对我来说,它总是AccountDatabase_1)。将表名称传递给存储过程,并返回需要运行的脚本以创建它(我们将在第二个数据库上执行该脚本)。对于视图,创建脚本实际上存储在Information_Schema.Views表中,因此您只需从中拖出视图名称和创建代码即可轻松创建克隆。

存储过程GenerateScript()

USE [SOURCE_DATABASE_NAME] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER Procedure [dbo].[GenerateScript] 
(   
    @tableName varchar(100) 
)    
as    
If exists (Select * from Information_Schema.COLUMNS where Table_Name= @tableName)    
Begin    
    declare @sql varchar(8000)    
    declare @table varchar(100)    
    declare @cols table (datatype varchar(50))   
    insert into @cols values('bit')   
    insert into @cols values('binary')   
    insert into @cols values('bigint')   
    insert into @cols values('int')   
    insert into @cols values('float')   
    insert into @cols values('datetime')   
    insert into @cols values('text')   
    insert into @cols values('image')   
    insert into @cols values('uniqueidentifier')   
    insert into @cols values('smalldatetime')   
    insert into @cols values('tinyint')   
    insert into @cols values('smallint')   
    insert into @cols values('sql_variant')   

    set @sql='' 

    Select 
     @[email protected]+    
     case when charindex('(',@sql,1)<=0 then '(' else '' end +Column_Name + ' ' +Data_Type + 
     case when Column_name='id' then ' IDENTITY ' else '' end +    
     case when Data_Type in (Select datatype from @cols) then '' else '(' end+ 
     case when data_type in ('real','money','decimal','numeric') then cast(isnull(numeric_precision,'') as varchar)+','+ 
     case when data_type in ('real','money','decimal','numeric') then cast(isnull(Numeric_Scale,'') as varchar) end 
     when data_type in ('char','nvarchar','nchar') then cast(isnull(Character_Maximum_Length,'') as varchar) else '' end+ 
     case when data_type ='varchar' and Character_Maximum_Length<0 then 'max' else '' end+ 
     case when data_type ='varchar' and Character_Maximum_Length>=0 then cast(isnull(Character_Maximum_Length,'') as varchar) else '' end+ 
     case when Data_Type in (Select datatype from @cols)then '' else ')' end+ 
     case when Is_Nullable='No ' then ' Not null ' else ' null ' end + 
     case when Column_Default is not null then 'DEFAULT ' + Column_Default else '' end + ',' 
    from 
     Information_Schema.COLUMNS where [email protected]    

    select 
     @table= 'Create table ' + table_Name 
    from 
     Information_Schema.COLUMNS 
    where 
     [email protected]    

    select @[email protected] + substring(@sql,1,len(@sql)-1) +')'    

    select @sql as DDL   

End    

Else   
    Select 'The table '[email protected] + ' does not exist'   

PHP

function cloneAccountDatabase($new_id){ 

    $srcDatabaseName = "AccountDatabase_1"; //The Database we are cloning 
    $sourceConn = openDB($srcDatabaseName); 

    $destDatabaseName = "AccountDatabase_".(int)$new_id; 
    $destConn = openDB($destDatabaseName); 

    //ENSURE DATABASE EXISTS, OR CREATE IT  
    if ($destConn==null){ 
     odbc_exec($sourceConn, "CREATE database " . $destDatabaseName); 
     $destConn = openDB($destDatabaseName); 
    } 

    //BUILD ARRAY OF TABLE NAMES 
    $tables = array(); 
    $q = odbc_exec($sourceConn, "SELECT name FROM sys.Tables"); 
    while (odbc_fetch_row($q)) 
     $tables[]=odbc_result($q,"name"); 


    //CREATE TABLES 
    foreach ($tables as $tableName){ 
     $q=odbc_exec($sourceConn, "exec GenerateScript '$tableName';"); 
     odbc_fetch_row($q); 
     $sql = odbc_result($q, 'ddl'); 
     $q=odbc_exec($destConn, $sql); 
    } 

    //BUILD ARRAY OF VIEW NAMES AND CREATE 
    $q = odbc_exec($sourceConn, "SELECT * FROM Information_Schema.Views"); 
    while (odbc_fetch_row($q)){ 
     $view=odbc_result($q,"table_name"); 
     $sql = odbc_result($q, "view_definition"); 
     odbc_exec($destConn, $sql); 
    }   

    return(true); 
} 
1

您可以使用SMO做到这一点,而不会有太多麻烦。 Here's一个网站为其提供了特定的代码。代码是用C#编写的,但希望你可以整合它或将它翻译成PHP。

+0

汤姆,非常感谢这个职位。虽然没有透明的方法将该代码转换为PHP(它使用某种我没有看到代码的Visual Studio DLL),但它确实让我的搜索引擎朝着正确的方向前进。这导致我上面的解决方案。为你的努力+1! – Dutchie432

相关问题