Hướng dẫn attach và detach database trong sql server 2008 năm 2024
Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật. Move a database using detach and attach (Transact-SQL)
Trong bài viết nàyApplies to: This topic describes how to move a detached database to another location and re-attach it to the same or a different server instance in SQL Server. However, we recommend that you move databases by using the ALTER DATABASE planned relocation procedure, instead of using detach and attach. For more information, see Move User Databases. Important We recommend that you do not attach or restore databases from unknown or untrusted sources. Such databases could contain malicious code that might execute unintended Transact-SQL code or cause errors by modifying the schema or the physical database structure. Before you use a database from an unknown or untrusted source, run DBCC CHECKDB on the database on a nonproduction server and also examine the code, such as stored procedures or other user-defined code, in the database. ProcedureTo move a database by using detach and attach
ExampleThe following example creates a copy of the AdventureWorks2022 database named
Attach the moved database and, optionally, its log by executing the following Transact-SQL statements:
In SQL Server Management Studio, a newly attached database is not immediately visible in Object Explorer. To view the database, in Object Explorer, click View, and then Refresh. When the Databases node is expanded in Object Explorer, the newly attached database now appears in the list of databases. Khi bạn có một Database SQL Server với vài chục account, muốn chuyển nó tới một server khác, vấn đề bạn gặp phải là rất nhiều account sql trước đó đã được gán các quyền nhất định trên từng object như table, store procedure, view, function... và bạn không thể nhớ hết được user nào đang nắm giữ quyền nào... để tiến hành gán lại quyền. Bài viết liên quan
Nhiều người thắc mắc về quá trình Detach và attach Database làm mất hết quyền của user trên đó, nội dung dưới đây sẽ hướng dẫn bạn cách giữ nguyên tất cả các quyền trên Database đó mà không phải tạo account mới và đi gán lại các quyền cho nó. Cách Attach và Detach database không làm mất quyền user trong SQL Server Bước 1: Detach và attach Database bình thường Bước 2: 2.1 Ghi lại tên các User có ở trong Database vừa attach lên 2.2 Vào Sercurit: Logins để tạo lại các user vừa ghi lại ở trên Bước 3. Sau khi đã tạo xong các account trùng tên, tiến hành Ánh xạ lại quyền cho từng user của Database: Chạy lệnh SQL: EXEC sp_change_users_login 'Update_One', 'UserName_Cần_Ánh_Xạ', ' UserName_Cần_Ánh_Xạ ' Ví dụ: EXEC sp_change_users_login 'Update_One', 'New1', 'New1' sp_change_users_login là một thủ tục hệ thống có sẵn trong SQL sẽ giúp chúng ta lấy lại quyền cho các database. mà không phải đi gán đến từng quyền trên mỗi user rất phực tạp. https://thuthuat.taimienphi.vn/ms-sql-server-attach-va-detach-database-khong-lam-mat-quyen-user-26268n.aspx |