标签:
CREATE PROCEDURE [dbo].[p_CreateDB] @Des_DB sysname, @ServerName sysname=N‘‘, @UserName sysname=N‘‘, @pwd sysname=N‘‘, @fliename sysname AS SET NOCOUNT ON DECLARE @srvid int, @dbsid int, @Dbid int, @DBFile int, @LogFile int, @CmdStr nvarchar(4000) declare @err int,@src varchar(255), @desc varchar(255),@dname varchar(255) IF ISNULL(@ServerName,N‘‘)=N‘‘ SET @ServerName=@@ServerName EXEC @err=sp_oacreate ‘sqldmo.sqlserver‘,@srvid OUT IF @err<>0 GOTO lb_Err IF ISNULL(@UserName,N‘‘)=N‘‘ BEGIN EXEC @err=sp_oasetproperty @srvid,‘loginsecure‘,-1 IF @err<>0 GOTO lb_Err EXEC @err=sp_oamethod @srvid,‘connect‘,NULL,@ServerName END ELSE EXEC @err=sp_oamethod @srvid,‘connect‘,NULL,@ServerName,@UserName,@pwd IF @err<>0 GOTO lb_Err EXEC @err=sp_oacreate ‘SQLDMO.Database‘,@Dbid OUT IF @err<>0 GOTO lb_Err EXEC @err=sp_oasetproperty @Dbid, ‘Name‘,@Des_DB IF @err<>0 GOTO lb_Err EXEC @err=sp_oacreate ‘SQLDMO.DBFile‘,@DBFile OUT IF @err<>0 GOTO lb_Err EXEC @err=sp_oasetproperty @DBFile, ‘Name‘,@Des_DB IF @err<>0 GOTO lb_Err set @dname=@fliename+@Des_DB+‘.mdf‘ EXEC @err=sp_oasetproperty @DBFile, ‘PhysicalName‘,@dname IF @err<>0 GOTO lb_Err EXEC @err=sp_oasetproperty @DBFile, ‘PrimaryFile‘,‘true‘ IF @err<>0 GOTO lb_Err EXEC @err=sp_oasetproperty @DBFile, ‘FileGrowthType‘,10 IF @err<>0 GOTO lb_Err EXEC @err=sp_oasetproperty @DBFile, ‘FileGrowth‘,50 IF @err<>0 GOTO lb_Err EXEC @err = sp_OAMethod @Dbid,‘FileGroups.Item("primary").DBFiles.Add‘,null,@DBFile IF @err<>0 GOTO lb_Err EXEC @err=sp_oacreate ‘SQLDMO.LogFile‘,@LogFile OUT IF @err<>0 GOTO lb_Err set @dname=@Des_DB+‘_log‘ EXEC @err=sp_oasetproperty @LogFile, ‘Name‘,@dname IF @err<>0 GOTO lb_Err set @dname=@fliename+@dname+‘.ldf‘ EXEC @err=sp_oasetproperty @LogFile, ‘PhysicalName‘,@dname EXEC @err = sp_OAMethod @Dbid,‘TransactionLog.LogFiles.Add‘,null,@LogFile IF @err<>0 GOTO lb_Err EXEC @err = sp_OAMethod @srvid,‘Databases.Add‘,null,@dbid IF @err<>0 GOTO lb_Err EXEC @err= sp_OAGetProperty @srvid, ‘Databases‘, @dbsid OUT IF @err<>0 GOTO lb_Err SET @CmdStr = ‘Add‘ EXEC @err = sp_OAMethod @dbsid,@CmdStr,null,@dbid SET @err=0 GOTO lb_Exit lb_Err: EXEC sp_oageterrorinfo NULL, @src OUT, @desc OUT EXEC sp_OADestroy @LogFile EXEC sp_OADestroy @DBFile EXEC sp_OADestroy @Dbsid EXEC sp_OADestroy @Dbid EXEC sp_OADestroy @srvid EXEC @err=sp_oamethod @srvid,‘DisConnect‘ RAISERROR(N‘错误编号 %#x, 错误源"%s", 错误描述 "%s"‘,16,1,@err,@src,@desc) RETURN -1 lb_Exit: EXEC sp_OADestroy @LogFile EXEC sp_OADestroy @DBFile EXEC sp_OADestroy @Dbsid EXEC sp_OADestroy @Dbid EXEC sp_OADestroy @srvid EXEC @err=sp_oamethod @srvid,‘DisConnect‘ RETURN @err ;
标签:
原文地址:http://www.cnblogs.com/yangdunqin/p/4229409.html