A file activation error occurred. The physical file name 'N@filename
Below t-sql code compiles fine. But when I run it like exec [SP_ATTACH_NW] 开发者_StackOverflow社区N'C:\myfolder' I get
Msg 5105, Level 16, State 2, Procedure SP_ATTACH_NW, Line 14 A file activation error occurred. The physical file name 'N@mdfFileName' may be incorrect. Diagnose and correct additional errors, and retry the operation.
USE master
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[SP_ATTACH_NW] (
@DestFolder varchar(255)
)
AS
BEGIN
SET NOCOUNT ON;
Declare @mdfFileName varchar(255)
Declare @ldfFileName varchar(255)
set @mdfFileName = @DestFolder + '\northwnd.mdf'
set @ldfFileName = @DestFolder + '\northwnd.ldf'
CREATE DATABASE [Northwind] ON
( FILENAME = N@mdfFileName ),
( FILENAME = N@ldfFileName )
FOR ATTACH
END
Pls advise. thanks
You can't have variables in the filename arguments of CREATE DATABASE (MSDN doesn't show @vars in the syntax)
The code above is literally looking for the constant "N@mdfFileName" as a filename.
You'd need dynamic SQL to build a string in, say, @MyBuiltSQL then run EXEC(@MyBuiltSQL)
Note: The "N" prefix here would not make @mdfFileName nvarchar anyway
精彩评论