Home Database Mysql Tutorial sql server里连接远程服务器,并进行创建和删除新数据库

sql server里连接远程服务器,并进行创建和删除新数据库

Jun 07, 2016 pm 05:47 PM
server sql server Remotely connect

sql server里连接远程服务器,并进行创建和删除新

一、创建新库

/*
测试 在sql2005中通过查询分析器,连接到sql2000,并创建了 tmpdb 库:成功

p_CreateDB 'tmpdb1','data','sa','sa'

*/

-----------------------创建存储过程开始--------------------
if object_id('p_CreateDB') is not null
drop procedure p_CreateDB
go
CREATE PROCEDURE p_CreateDB  
@Des_DB sysname, --目标数据库
@ServerName sysname=N'', --服务器名
@UserName sysname=N'', --用户名,不指定则表示使用 Windows 身份登录
@pwd sysname=N'' --密码 
AS
SET NOCOUNT ON
DECLARE @srvid int,--服务器的对象
 @dbsid int,
 @Dbid int,--新建数据库对象
 @DBFile int,--新建数据库DB文件
 @LogFile int,--新建数据库Log文件
 @CmdStr nvarchar(4000)
declare @err int,@src varchar(255), @desc varchar(255) --错误处理变量


IF ISNULL(@ServerName,N'')=N'' SET @ServerName=@@ServerName --默认为本地数据库

--创建sqldmo对象·
EXEC @err=sp_oacreate 'sqldmo.sqlserver',@srvid OUT
IF @err0 GOTO lb_Err

--连接服务器
IF ISNULL(@UserName,N'')=N'' --使用 Windows 身份登录
BEGIN
  EXEC @err=sp_oasetproperty @srvid,'loginsecure',-1
  IF @err0 GOTO lb_Err

  EXEC @err=sp_oamethod @srvid,'connect',NULL,@ServerName
END
ELSE
  EXEC @err=sp_oamethod @srvid,'connect',NULL,@ServerName,@UserName,@pwd

IF @err0 GOTO lb_Err
--新数据库对象创建
EXEC @err=sp_oacreate 'SQLDMO.Database',@Dbid OUT
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @Dbid, 'Name',@Des_DB
IF @err0 GOTO lb_Err
/*
---这里可以设置数据文件与日志文件的属性,不写就由sql server默认
--新数据库DB文件对象创建,并设置属性
EXEC @err=sp_oacreate 'SQLDMO.DBFile',@DBFile OUT
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @DBFile, 'Name','tmpfile'
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @DBFile, 'PhysicalName','c:tmp.mdf'
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @DBFile, 'PrimaryFile','true'
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @DBFile, 'FileGrowthType',0
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @DBFile, 'FileGrowth',1
IF @err0 GOTO lb_Err
--新数据库对象加入DB文件
EXEC @err = sp_OAMethod @Dbid,'FileGrou.Item("primary").DBFiles.Add',null,@DBFile
IF @err0 GOTO lb_Err

--新数据库LOG文件对象创建,并设置属性
EXEC @err=sp_oacreate 'SQLDMO.LogFile',@LogFile OUT
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @LogFile, 'Name','tmplg'
IF @err0 GOTO lb_Err
EXEC @err=sp_oasetproperty @LogFile, 'PhysicalName','c:tmp.ldf'
--新数据库对象加入DB文件
EXEC @err = sp_OAMethod @Dbid,'TransactionLog.LogFiles.Add',null,@LogFile
IF @err0 GOTO lb_Err
*/
--在服务器上创建 新数据库
EXEC @err = sp_OAMethod @srvid,'Databases.Add',null,@dbid
IF @err0 GOTO lb_Err

/*
EXEC @err= sp_OAGetProperty @srvid, 'Databases', @dbsid OUT
IF @err0 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
GO

二、删除数据库

/*
测试

p_DropDB 'tmpdb1','data','sa','sa'
*/

if object_id('p_DropDB') is not null
drop procedure p_DropDB
go
CREATE PROCEDURE p_DropDB  
@Des_DB sysname, --目标数据库
@ServerName sysname=N'', --服务器名
@UserName sysname=N'', --用户名,不指定则表示使用 Windows 身份登录
@pwd sysname=N'' --密码 
AS
SET NOCOUNT ON
DECLARE @srvid int,--服务器的对象
 @dbsid int,
 @Dbid int,--数据库对象
 @CmdStr nvarchar(4000)
declare @err int,@src varchar(255), @desc varchar(255) --错误处理变量

IF ISNULL(@ServerName,N'')=N'' SET @ServerName=@@ServerName --默认为本地数据库

--创建sqldmo对象·
EXEC @err=sp_oacreate 'sqldmo.sqlserver',@srvid OUT
IF @err0 GOTO lb_Err

--连接服务器
IF ISNULL(@UserName,N'')=N'' --使用 Windows 身份登录
BEGIN
  EXEC @err=sp_oasetproperty @srvid,'loginsecure',-1
  IF @err0 GOTO lb_Err

  EXEC @err=sp_oamethod @srvid,'connect',NULL,@ServerName
END
ELSE
  EXEC @err=sp_oamethod @srvid,'connect',NULL,@ServerName,@UserName,@pwd

IF @err0 GOTO lb_Err

--删除数据库
--这两个都可以删除
EXEC @err = sp_OAMethod @srvid,'KillDatabase',null,@Des_DB
IF @err0 GOTO lb_Err
/*
--
EXEC @err = sp_OAMethod @srvid,'Databases.Remove',null,@Des_DB
IF @err0 GOTO lb_Err
*/

--结束
SET @err=0
GOTO lb_Exit

--错误处理
lb_Err:
  EXEC sp_oageterrorinfo NULL, @src OUT, @desc OUT 
  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 @Dbsid
  EXEC sp_OADestroy @Dbid 
  EXEC sp_OADestroy @srvid 
  EXEC @err=sp_oamethod @srvid,'DisConnect'
  RETURN @err
GO

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

How to connect keep body fat scale How to connect keep body fat scale Mar 07, 2024 pm 04:50 PM

How to connect the keep body fat scale? Keep has a specially designed body fat scale, but most users do not know how to connect the keep body fat scale. Next is the graphic tutorial on the connection method of the keep body fat scale that the editor brings to users. , interested users come and take a look! How to connect the keep body fat scale 1. First open the keep software, go to the main page, click [My] in the lower right corner, and select [Smart Hardware]; 2. Then on the My Smart Devices page, click the [Add Device] button in the middle; 3 , then select the device you want to add interface, select [Smart Body Fat/Weight Scale]; 4. Then on the device model selection page, click the [keep body fat scale] option; 5. Finally, in the interface shown below, finally [Add Now] at the bottom

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

Three Ways to Fix Edge Your Connection Isn't Private Three Ways to Fix Edge Your Connection Isn't Private Mar 13, 2024 pm 01:30 PM

When you use the Edge browser to access web pages, have you ever encountered a prompt that your connection is not a dedicated connection, causing web browsing to fail? How is this going? Many friends don’t know how to deal with this problem. You can take a look at the following three solutions. Method 1 (simple and crude): In the edge browser, you can try to solve the problem of the website being inaccessible by entering the settings and turning off the security function, and then blocking location permissions in the website permissions. It is important to note that the effectiveness and duration of this approach may vary, and specific effects cannot be determined. After restarting your browser, you can try visiting the website to see if the issue is resolved. Method 2: Adjust the keyboard to English input

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

See all articles