[英]How to solve this SQL Server error I'm getting?
As you can see below I am trying to execute a stored procedure by passing the two parameters, I get error like正如您在下面看到的,我正在尝试通过传递两个参数来执行存储过程,但出现类似错误
Msg 102, Level 15, State 1, Line 19
消息 102,第 15 级,State 1,第 19 行
Incorrect syntax near 'C:\SQL_Backup\Test.bak'.'C:\SQL_Backup\Test.bak' 附近的语法不正确。
Code:代码:
EXEC Proc_RestoreSQL_Database ('C:\SQL_Backup\Test.bak','DMTest')
Stored procedure being executed:正在执行的存储过程:
ALTER PROCEDURE [dbo].[Proc_RestoreSQL_Database]
@DISK nvarchar(1000),
@DBName varchar(1000)
AS
BEGIN
-- Create DB with script(DMUtilityTest)
CREATE DATABASE [@DBName]
-- restore
RESTORE DATABASE [@DBName]
FROM DISK = N'@DISK'
END
There is no need to CREATE
the database if you are immediately going to restore it.如果您要立即恢复数据库,则无需
CREATE
数据库。
Just execute the restore directly.直接执行restore就好了。 Do not wrap the database name parameter in
[]
, nor the filename parameter in quotes ''
, you only need this if you are embedding the actual name, not parameters.不要将数据库名称参数包含在
[]
中,也不要将文件名参数包含在引号''
中,只有在嵌入实际名称而不是参数时才需要它。
You should also use more sensible parameter types您还应该使用更合理的参数类型
CREATE OR ALTER PROCEDURE [dbo].[Proc_RestoreSQL_Database]
@DISK nvarchar(255),
@DBName sysname
AS
RESTORE DATABASE @DBName
FROM DISK = @DISK;
GO
This obviously assumes you are going to restore to the same location.这显然假设您要恢复到同一位置。 You may want to pass new locations using
MOVE TO @newFile
您可能希望使用
MOVE TO @newFile
传递新位置
To execute it, again do not wrap in parenthesis ()
, just specify the parameter names要执行它,再次不要用括号
()
换行,只需指定参数名称
EXEC Proc_RestoreSQL_Database @DISK = 'C:\SQL_Backup\Test.bak', @DBName = 'DMTest';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.