MS SQLServer如何批量附加数据库
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了MS SQLServer如何批量附加数据库,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
USE MASTER
GO
IF OBJECT_ID('[sp_AttchDataBase]') IS NOT NULL
DROP PROCEDURE [sp_AttchDataBase] GO
CREATE PROCEDURE sp_AttchDataBase(
@Path NVARCHAR(1024),
@DataFiles NVARCHAR(MAX) = NULL,
@SplitStr NVARCHAR(50) = ',' )
AS
SET NOCOUNT ON
--检查文件路径是否正确
DECLARE @Dir NVARCHAR(1024),
@i INT,
@x XML
IF RIGHT(@Path, 1) <> '\'
SET @Path = @Path + '\'
IF CHARINDEX('\\', @Path) > 0
BEGIN
--RAISERROR 50001 N'文件路径中不能包含有"\\",@Path设置错误.'
RETURN(1)
END
SET @Dir = 'Dir ' + @Path
EXEC @i = xp_cmdshell @Dir,
no_output
IF @i <> 0
BEGIN
--RAISERROR 50001 N'无效的文件路径,@Path设置错误.'
RETURN(1)
END
SET @Path = REPLACE(@Path, '"', '')
DECLARE @Files TABLE(NAME NVARCHAR(512))
DECLARE @filetmpfin TABLE(
NAME NVARCHAR(255) NOT NULL,
depth INT NULL,
IsFile BIT NULL
)
DECLARE @SmoPrimayChildren TABLE(
STATUS INT,
fileid INT,
NAME SYSNAME,
FILENAME NVARCHAR(512)
)
DECLARE @smoPrimaryFileProp TABLE(PROPERTY SQL_VARIANT NULL, VALUE SQL_VARIANT NULL)
SET @DataFiles = REPLACE(
REPLACE(REPLACE(@DataFiles, CHAR(13) + CHAR(10), ''), CHAR(13), ''),
CHAR(10),
''
)
SET @x = N'<Root><File>' + REPLACE(@DataFiles, @SplitStr, N'</File><File>') +
N'</File></Root>'
INSERT INTO @Files
SELECT t.v.value('.[1]', 'nvarchar(512)') AS NAME
FROM @x.nodes('Root/File') t(v)
WHERE t.v.value('.[1]', 'nvarchar(512)') > ''
INSERT INTO @filetmpfin
EXEC MASTER.dbo.xp_dirtree @Path,
1,
1
DECLARE @File NVARCHAR(255),
@sql NVARCHAR(4000),
@DataBase SYSNAME
DECLARE cur_File CURSOR
FOR
SELECT NAME
FROM @filetmpfin AS a
WHERE IsFile = 1
AND NAME LIKE '%.mdf'
AND (
EXISTS(
SELECT 1
FROM @Files
WHERE NAME = a.Name
)
OR @DataFiles IS NULL
)
AND NOT EXISTS(
SELECT 1
FROM MASTER.sys.master_files
WHERE physical_name = @Path + a.Name
)
OPEN cur_File
BEGIN TRY
FETCH NEXT FROM cur_File INTO @File
WHILE @@Fetch_Status = 0
BEGIN
SET @sql = 'dbcc checkprimaryfile (N''' + @Path + @File + ''' , 2) With No_Infomsgs'
INSERT INTO @smoPrimaryFileProp
EXEC (@sql)
SET @sql = 'dbcc checkprimaryfile (N''' + @Path + @File + ''' , 3) With No_Infomsgs'
INSERT INTO @SmoPrimayChildren
EXEC (@sql)
SELECT @DataBase = QUOTENAME(CONVERT(NVARCHAR(255), VALUE)),
@sql = NULL
FROM @smoPrimaryFileProp
WHERE CONVERT(NVARCHAR(255), PROPERTY) = 'Database name'
SELECT @sql = ISNULL(
@sql + ',' + CHAR(13) + CHAR(10),
'Create DataBase ' + @DataBase + ' On' + CHAR(13) + CHAR(10)
) +
'(FileName=N''' + @Path + RIGHT(
RTRIM(FILENAME),
CHARINDEX('\', REVERSE(RTRIM(FILENAME))) -1
) + ''')'
FROM @SmoPrimayChildren
EXEC (@sql + ' For Attach')
PRINT N'成功附加数据库: ' + @DataBase
DELETE
FROM @SmoPrimayChildren
DELETE
FROM @smoPrimaryFileProp
FETCH NEXT FROM cur_File INTO @File
END
END TRY
BEGIN CATCH
DECLARE @Error NVARCHAR(2047)
SET @Error = ERROR_MESSAGE()
--RAISERROR 50001 @Error
END CATCH
CLOSE cur_File
DEALLOCATE cur_File
GO
--use master --Go
--Exec sp_AttchDataBase -- @Path = 'E:\100.其他\测试', -- nvarchar(1024) -- @DataFiles = NULL, -- nvarchar(max) -- @SplitStr = NULL -- nvarchar(50)
感谢你能够认真阅读完这篇文章,希望小编分享的“MS SQLServer如何批量附加数据库”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341