zoukankan      html  css  js  c++  java
  • 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 ;

  • 相关阅读:
    visio 2019 激活方法
    sftp 多用户安装与配置
    CentOS configuration uses the SFTP server
    esxi命令行强行关闭虚拟机
    esxi 版本升级命令
    存储分析 开源重复数据删除技术崭露头角
    最简单-转换MBR为GPT
    CentOS 7 搭建本地YUM仓库,并定期同步阿里云源
    uml建模工具介绍
    C/C++中extern关键字详解
  • 原文地址:https://www.cnblogs.com/yangdunqin/p/4229409.html
Copyright © 2011-2022 走看看