0

In the SQL server, how to use a string variable ? for example, I want to write a script to automate database restore, but it complains syntax error near '+' .

how to fix it ?

declare @source varchar(20) = 'Adventureworks2012';
declare @destination varchar(20) = 'Adventureworks2012_copy';

RESTORE DATABASE @source
FROM DISK = @destination
WITH REPLACE, 
MOVE @source+'_Data' TO 'C:\test\Adventureworks2012_20140301_Data.mdf'
1
  • 1
    Try like @SouceStr varchar(500)= @source+'_Data' and use the concatenated field as Source i.e. MOVE @SouceStr TO 'C:\test\Adventureworks2012_20140301_Data.mdf' Commented Feb 19, 2014 at 16:38

1 Answer 1

1

Try something like the following:

DECLARE @Source varchar(50) = 'Adventureworks2012';
DECLARE @Destination varchar(50) = 'Adventureworks2012_copy';
DECLARE @SourceDB varchar(500)
DECLARE @DestinationDB varchar(500)


SET @SourceDB = @Source + '_Data'
SET @DestinationDB = 'C:\test\Adventureworks2012_20140301_Data.mdf'

RESTORE DATABASE @Source
FROM DISK = @Destination
WITH REPLACE, 
MOVE @SourceDB TO @DestinationDB
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.