Attach database in SQL Server 2005 using script

Here is the script through which you can easily attach database in sql server 2005.


USE [master]
GO

--ConfigDb
CREATE DATABASE [$(dbname)] ON
( FILENAME = N'$(MDFFileLocationPath)' ),
( FILENAME = N'$(LDFFileLocationPath)' )
FOR ATTACH
GO
if exists (select name from master.sys.databases sd where name = N'$(dbname)' and SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) EXEC [$(dbname)].dbo.sp_changedbowner @loginame=N'$(username)', @map=false


Happy Coding...

No comments:

Post a Comment