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.
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 21Must declare the scalar variable "@newdb".Msg 137, Level 16, State 1, Line 21Must declare the scalar variable "@LOC2".http://www.sqlservercentral.com/articles/Administration/trackingdownnewlycreateddatabases/1582/any help is appreciated....UsemasterGoDECLARE@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_databasesIf(@retval > 0)insertinto@newdbSelect new_db_namefrom Last_DB_Track;select * from @newdb;insert into @LOC2select '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 itselfquote: Msg 137, Level 16, State 1, Line 21Must declare the scalar variable "@newdb"
you did not declare the variable @newdbquote: Msg 137, Level 16, State 1, Line 21Must declare the scalar variable "@LOC2".
You declare the variable @LOC2 as table variable and not as scalar variable. The Backup Database expects scalar variableActually 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 intselect @id = min(id) from @LOC2while @id is not nullbegin select @cmd = loc2 from @LOC2 where id = @id exec (@cmd) select @id = min(id) from @LOC2 where id > @idend KH[spoiler]Time is always against us[/spoiler] |
|
|
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 itselfquote: Msg 137, Level 16, State 1, Line 21Must declare the scalar variable "@newdb"
you did not declare the variable @newdbquote: Msg 137, Level 16, State 1, Line 21Must declare the scalar variable "@LOC2".
You declare the variable @LOC2 as table variable and not as scalar variable. The Backup Database expects scalar variableActually 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 intselect @id = min(id) from @LOC2while @id is not nullbegin select @cmd = loc2 from @LOC2 where id = @id exec (@cmd) select @id = min(id) from @LOC2 where id > @idend KH[spoiler]Time is always against us[/spoiler] |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-10-31 : 23:20:08
|
welcome KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|