how to attach database with variable path by storedprocedure??

  • please help me, how to attach database with variable path??

  • It requires a bit of dynamic sql:

    USE master;

    DECLARE @dbname SYSNAME = N'AdventureWorks2008R2',

    @filename NVARCHAR(500) = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2DEV\MSSQL\DATA\AdventureWorks2008R2_Data.mdf',

    @logfilename NVARCHAR(500) = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2DEV\MSSQL\DATA\AdventureWorks2008R2_log.LDF',

    @sql NVARCHAR(MAX);

    SET @sql = N'CREATE DATABASE ' + QUOTENAME(@dbname) + N' ON

    ( FILENAME = N' + QUOTENAME(@filename, '''') + N'),

    ( FILENAME = N' + QUOTENAME(@logfilename, '''') + N')

    FOR ATTACH;'

    PRINT @sql

    --uncomment when ready

    --EXEC(@sql);

    GO

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • ok thanks very much 😀

    its resolved

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply