Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Scalar Variable

Author  Topic 

_Iron
Starting Member

2 Posts

Posted - 2012-03-26 : 07:43:44
Good Morning.
I am writing a backup script and getting the following error message:
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "@ arquivo_DIF."

It is a script to restore FULL and Differential Backups.
If run the backups work separately, but the script is giving this error.

code position below:

Declare @pasta char(3),
@arquivo_FULL varchar(255),
@arquivo_DIF varchar(255),
@caminho varchar(255)

set @pasta = case datepart( dw, getdate() )
when '1' then 'Dom' when '2' then 'Seg' when '3' then 'Ter' when '4' then 'Qua'
when '5' then 'Qui' when '6' then 'Sex' when '7' then 'Sab'
end

set @caminho = 'T:\Backup\' + @pasta + '\'

set @arquivo_FULL = @caminho + 'bkpFULL_'+ 'db1' + '_' + @pasta + '_00_00.BAK'
set @arquivo_DIF = @caminho + 'bkpDIF_' + 'db1' + '_' + @pasta + '_06_01.BAK'

use master
RESTORE DATABASE [db1_teste]
FROM DISK = @arquivo_FULL
WITH STATS = 5, FILE = 1, MOVE N'DB1_Novo_Data' TO N'D:\Dados\DB1_Teste.MDF', MOVE N'DB1_Novo_Log' TO N'L:\Log\DB1_Teste.LDF'
, NORECOVERY, NOUNLOAD, REPLACE
GO

/* - Restaurando o Diferencial */

use master
RESTORE DATABASE [DB1_Teste]
FROM DISK = @arquivo_DIF
WITH STATS = 3, FILE = 1, MOVE N'DB1_Novo_Data' TO N'D:\Dados\DB1_Teste.MDF',
MOVE N'DB1_Novo_Log' TO N'L:\Log\DB1_Teste.LDF',NORECOVERY
GO


RESTORE DATABASE DB1_Teste WITH RECOVERY
GO


If anyone has a good tip thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-26 : 08:25:21
Scope of a variable ends when the batch ends. So, even though you are declaring @arquivo_DIF at the very top, it goes out of scope at the batch separator (GO statement).
,  NORECOVERY,  NOUNLOAD,  REPLACE
GO

/* - Restaurando o Diferencial */
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-26 : 09:12:52
besides//you need dynamic sql to do this anyway

Why are you restoring the same database twice?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

_Iron
Starting Member

2 Posts

Posted - 2012-03-26 : 09:47:54
Thanks.
remove the GO statements.
a GO statement ends the previous batch,and destroys the declared variables as well.
if you remove the GO statmeents, the variables will still be there, and you are good to go!
SOLVED.
Thanks for all
Go to Top of Page
   

- Advertisement -