当我执行以下脚本时,我收到以下错误。我该怎么做才能解决这个问题?
错误 的
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '@path'.
脚本
GO
declare @path varchar(2000)
declare @path1 varchar(2000)
select @path=(SELECT [filename] FROM master..sysdatabases WHERE [name] LIKE 'master')
set @path= REPLACE(@path, 'master', 'test')
set @path1= REPLACE(@path, 'test.mdf', 'test_log.ldf')
select @path
select @path1
CREATE DATABASE [test] ON
( FILENAME = @path ),
( FILENAME =N'D:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\DATA\test_log.ldf' )
FOR ATTACH
GO
答案 0 :(得分:3)
替换
CREATE DATABASE [test] ON
( FILENAME = @path ),
( FILENAME =N'D:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\DATA\test_log.ldf' )
FOR ATTACH
与
DECLARE @sql nvarchar(400);
SET @sql = '
CREATE DATABASE [test] ON
( FILENAME = ' + quotename(@path,'''') + ' ),
( FILENAME =N' + quotename(@path1,'''') + ' )
FOR ATTACH
'
EXECUTE sp_executesql @sql;
答案 1 :(得分:1)
您并非真正读取已配置的默认数据目录 - 您只是在阅读master
数据库所在的目录(并且不必配置数据文件的默认目录。)
如果你真的想知道真正的目录 - 你需要查看注册表。有关所有血腥细节的信息,请参阅此blog post here at SQL Server Central