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
 t-sql to backup new database

Author  Topic 

crazy_max
Starting Member

2 Posts

Posted - 2014-10-31 : 21:29:25

hello all,

i was able to get most part working...i took the below article and used it to come up with my strategy of backing up newly created DB that will be scheduled by agent to run every day or so....But when i run the below code i get this error...

fails at this part "backup database @newdb to disk = @loc2;"
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@newdb".
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@LOC2".

http://www.sqlservercentral.com/articles/Administration/trackingdownnewlycreateddatabases/1582/


any help is appreciated....


Use
master
Go
DECLARE
@LOC nvarchar(512) = N'H:\Backup\SQLTEST02\';
DECLARE
@BKP_EXT nvarchar(5);
DECLARE
@retval int;
SET
@BKP_EXT = '.bak';
DECLARE
@newdb table
(
newdb nvarchar(512)
);
DECLARE
@LOC2 table
(
loc2 nvarchar(512)
);
Exec
@retval = sp_track_new_databases
If
(@retval > 0)
insert
into
@newdb
Select new_db_name
from Last_DB_Track;
select * from @newdb;
insert into @LOC2
select 'backup database ' + new_db_name + ' to disk = ''' + @LOC + new_db_name + '.bak''' from last_db_track;
select * from @LOC2;
backup database @newdb to disk = @loc2;
go

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-10-31 : 22:29:36
Firstly on the error message itself

quote:
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@newdb"

you did not declare the variable @newdb

quote:
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@LOC2".

You declare the variable @LOC2 as table variable and not as scalar variable. The Backup Database expects scalar variable

Actually you already form the backup command in the @LOC2 table. You only need to grab the command from @LOC2 table and execute it. You can use cursor or while loop to do that.

example below uses while loop.

declare @cmd nvarchar(100)
declare @LOC2 table (id int identity, loc2 nvarchar(512))
insert into @LOC2 (loc2)
select 'backup database ' + new_db_name + ' to disk = ''' + @LOC + new_db_name + '.bak''' from last_db_track;

declare @id int
select @id = min(id) from @LOC2
while @id is not null
begin
select @cmd = loc2 from @LOC2 where id = @id
exec (@cmd)
select @id = min(id) from @LOC2 where id > @id
end



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

crazy_max
Starting Member

2 Posts

Posted - 2014-10-31 : 22:54:17
Thank you very much...that worked for me...Appreciate your help...

quote:
Originally posted by khtan

Firstly on the error message itself

quote:
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@newdb"

you did not declare the variable @newdb

quote:
Msg 137, Level 16, State 1, Line 21
Must declare the scalar variable "@LOC2".

You declare the variable @LOC2 as table variable and not as scalar variable. The Backup Database expects scalar variable

Actually you already form the backup command in the @LOC2 table. You only need to grab the command from @LOC2 table and execute it. You can use cursor or while loop to do that.

example below uses while loop.

declare @cmd nvarchar(100)
declare @LOC2 table (id int identity, loc2 nvarchar(512))
insert into @LOC2 (loc2)
select 'backup database ' + new_db_name + ' to disk = ''' + @LOC + new_db_name + '.bak''' from last_db_track;

declare @id int
select @id = min(id) from @LOC2
while @id is not null
begin
select @cmd = loc2 from @LOC2 where id = @id
exec (@cmd)
select @id = min(id) from @LOC2 where id > @id
end



KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-10-31 : 23:20:08
welcome


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -