[推荐] (SqlServer)分离所有用户数据库
——通过知识共享树立个人品牌。
在实际应用中,有时我们需要一次性分离所有用户数据库,下面给出代码,供大家参考。
USE
[
master
]
GO
IF
EXISTS (
SELECT
*
FROM sys.objects
WHERE
[
object_id
]
=
OBJECT_ID(N
'
[dbo].[spDetachAllUserDatabases]
')
AND type
IN ( N
'
P
', N
'
PC
' ) )
DROP
PROCEDURE
[
dbo
].
[
spDetachAllUserDatabases
]
GO
CREATE
PROCEDURE
[
dbo
].
[
spDetachAllUserDatabases
]
AS
BEGIN
--
Declare Variables
DECLARE
@DatabaseName
VARCHAR(
100)
DECLARE
@MinDatabaseID
INT
DECLARE
@MaxDatabaseID
INT
DECLARE
@SQL
VARCHAR(
4000)
--
Check for temporary table and drop it if it exists
IF
OBJECT_ID(
'
tempDB.dbo.#Database
')
IS
NOT
NULL
DROP
TABLE
[
#Database
];
--
Create temporary table
CREATE
TABLE #
Database
(
ID
INT
IDENTITY(
1,
1),
DatabaseName
VARCHAR(
100)
)
--
Check for existing user databases
IF
EXISTS (
SELECT name
FROM sys.databases
WHERE database_id
>
4
AND name
NOT
IN (
'
SQLDBA
',
'
ReportServer
',
'
ReportServerTempDB
',
'
distribution
' ) )
BEGIN
--
Insert all database names into a temporary table
INSERT
INTO #
Database ( DatabaseName )
SELECT name
FROM sys.databases
WHERE database_id
>
4
AND name
NOT
IN (
'
SQLDBA
',
'
ReportServer
',
'
ReportServerTempDB
',
'
distribution
' )
--
Set Variables for the detach database loop
SELECT
@MinDatabaseID
=
MIN(ID),
@MaxDatabaseID
=
MAX(ID)
FROM #
Database
--
Begin loop to detach databases
WHILE
@MinDatabaseID
作者:EricHu(DB、CS、BS、WebService、WCF、PM等)
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: Spring Boot – spring-boot-starter
spring-boot-starter 当学习Spring Boot时,可以通过一个完整的案例来理解和实践其基本概念和功能。以下是一个简单的Spring Boot Starter完整案例,展示了如何创建一个基本的Web应用程序: 首先,创建一个名为pom.xm…