SQL远程创建数据库
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 ;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。