How to batch attach databases to MS SQLServer
/************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释使用此脚本 * 时间: 2015/7/13 11:16:41 ************************************************************/ USE MASTER GO IF OBJECT_ID('[sp_AttchDataBase]') IS NOT NULL DROP PROCEDURE [sp_AttchDataBase] GO /*附加数据库(V2.0) Andy 2011-7-8 */ CREATE PROCEDURE sp_AttchDataBase( @Path NVARCHAR(1024), @DataFiles NVARCHAR(MAX) = NULL, @SplitStr NVARCHAR(50) = ',' ) AS SET NOCOUNT ON /* V2.0 版本,在V1.0基础上,处理文件路径不规范原則,e.g. @DataFiles='E:\"my data DB"\"Hello RT"' @Path 文件路径 @DataFiles 文件名列表 @SplitStr 文件名列表中的文件分隔符 1.必须把要附加的数据库文件(*.mdf和*.ldf)放到@Path下, 2.当@DataFiles Is Null 会附加@Path文件夹下的所有数据库文件. e.g: Exec sp_AttchDataBase 'D:\db2' */ --检查文件路径是否正确 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)
The above is the detailed content of How to batch attach databases to MS SQLServer. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

The syntax differences between MySQL and SQL Server are mainly reflected in database objects, data types, SQL statements and other aspects. Database object differences include the storage engine and how filegroups are specified, and the creation of indexes and constraints. Data type differences involve differences in numeric types, character types, and date and time types. SQL statement differences are reflected in result set limitations, data insertion, update and delete operations, etc. Other differences include how identity columns, views, and stored procedures are created. Understanding these differences is important to avoid errors when using different database systems.

Mobile phone film has become one of the indispensable accessories with the popularity of smartphones. To extend its service life, choose a suitable mobile phone film to protect the mobile phone screen. To help readers choose the most suitable mobile phone film for themselves, this article will introduce several key points and techniques for purchasing mobile phone film. Understand the materials and types of mobile phone films: PET film, TPU, etc. Mobile phone films are made of a variety of materials, including tempered glass. PET film is relatively soft, tempered glass film has good scratch resistance, and TPU has good shock-proof performance. It can be decided based on personal preference and needs when choosing. Consider the degree of screen protection. Different types of mobile phone films have different degrees of screen protection. PET film mainly plays an anti-scratch role, while tempered glass film has better drop resistance. You can choose to have better

The location where the Navicat database configuration files are stored varies by operating system: Windows: The user-specific path is %APPDATA%\PremiumSoft\Navicat\macOS: The user-specific path is ~/Library/Application Support/Navicat\Linux: The user-specific path is ~/ .config/navicat\The configuration file name contains the connection type, such as navicat_mysql.ini. These configuration files store database connection information, query history, and SSH settings.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())
