TSQL to copy backups to a new location

Hello,
I am trying to select the most recent backup of a database and move it over to a new location. However I am unable to assign the top1 max to a variable. Here is my script:
DECLARE @cmd nvarchar(4000)
DECLARE @srcbkpLocation nvarchar(4000)
DECLARE @destbkpLocation nvarchar(4000)
DECLARE @bkpFileName nvarchar(4000)
set @destbkpLocation='\\ServerB\FULL\Backups\NameofDB\'
set @srcbkpLocation='E:\Backups\NameofDB\'
SELECT top 1 @bkpFileName= max(bs.backup_finish_date) as Mostrecent,
bs.database_name AS DatabaseName,
bms.physical_device_name
FROM msdb.dbo.backupset bs
INNER JOIN msdb.dbo.backupmediafamily bms
ON bs.media_set_id = bms.media_set_id
INNER JOIN master.dbo.sysdatabases s
ON bs.database_name = s.name
WHERE
s.dbid IN (6) AND
bs.type = 'D'
GROUP BY bs.database_name,bms.physical_device_name order by Mostrecent desc
SET @cmd = 'xcopy ' +@srcbkpLocation++@bkpFileName + ' ' + @destbkpLocation++@bkpFileName
EXEC master.dbo.xp_cmdshell @cmd, NO_OUTPUT
can someone tell me where am I doing wrong?

Hello,
The error occurs when you are assigning the column values from a SELECT statement into local variables but not all columns are assigned to a corresponding local variable. To avoid this error, you can try to asssign each column to a
local variable. For example:
declare @date datetime
declare @dbname nvarchar(40)
declare @bkpFileName nvarchar(4000)
SELECT top 1 @date=max(bs.backup_finish_date),@dbname=bs.database_name,
@bkpFileName=bms.physical_device_name
FROM msdb.dbo.backupset bs
INNER JOIN msdb.dbo.backupmediafamily bms
ON bs.media_set_id = bms.media_set_id
INNER JOIN master.dbo.sysdatabases s
ON bs.database_name = s.name
WHERE
s.dbid IN (1) AND
bs.type = 'D'
GROUP BY bs.database_name,bms.physical_device_name
order by max(bs.backup_finish_date) desc
As for the Xcopy command, please verify that the source path and destination path of the backup file is correct.
Regards,
Fanny Liu
If you have any feedback on our support, please click here. 
Fanny Liu
TechNet Community Support

Similar Messages

  • Option to "copy photos to a new location" not available

    LR 2.7, Mac
    I regularly use the import from catalog command to merge catalogs. Most of the time I want to move the images, so I usually select the option to “copy photos to a new location” going e.g. from laptop to desktop.
    Occasionally though that option does not appear on the drop-down menu. If it was selected previously upon importing from a catalog, the menu will show a blank entry. If one goes ahead with it looking like that, the computer will hang until one cancels the operation; no images will have been copied.
    It just happened again this morning. I had started up the laptop in target disk mode, but LR woudn't let me select to copy the new images. A copy of this particular catalog on an ext. HD wouldn't work either. A copy on any of the internal HDs did work fine though. (I've ruled out permission problems.)
    As a workaround I then usually export the catalog from the laptop onto one of the internal HDs and import the catalog from there.
    What puzzles me is that most of the time it works fine and there doesn't seem to be any pattern to it.
    Anyone seeing this or having any ideas?

    You can reset the browser.newtab.url pref on the <b>about:config</b> page via the right-click context menu to the default value.
    You can open the <b>about:config</b> page via the location bar and you can accept the warning and click "I'll be careful" to continue
    You can use the SearchReset extension to reset some preferences to the default values.
    *https://addons.mozilla.org/firefox/addon/searchreset/
    Note that the SearchReset extension only runs once and then uninstalls automatically, so it won't show on the "Firefox > Add-ons" page (about:addons).

  • Is it possible to copy a swf to a new location and still use debug

    Hi,
    I'm developing a SWF using Flex. When I generate my SWF file I'd like to copy it to a new location (a folder under my website root folder) so that it runs in my local version of a website I'm working on.
    Before when working with a Flash movie, using Flash Develop I was able to set up a batch file to copy the generate file to the SWF folder under my website, is it possible to do this now in Flex builder and still be able to use the Flex step through debugger?
    Thanks
    Stephen

    Just point your debug settings to that swf file
    in flex Builder > Run > Debug > Other (choose the right project) -- and change the debug path
    that should work I think

  • Move equipment: drag and drop to new location

    Hello,
    We are using CC04 to move/ copy equipment to a new location. When we copy a equipment and put to a new location it does not include the sub equipment.
    Is there a quick and easier was to move or copy equipment to new locations and include everything to go with it: (BOM, Configuration, address, partners, document..etc.). In addition is there a way to copy functional location with all the equipment/subequipment and copy/move to new location.
    Thanks.

    Refer this link
    Link: [Product Structure|http://help.sap.com/saphelp_erp60/helpdata/EN/c0/f0da539ca111d194a000a0c92f024a/frameset.htm]
    Thanks
    S.N

  • Images I move from one folder into another are disappearing, at first it was occurring immediately if I did not copy and paste the images. Today however I found out that images I had been using for days just up and vanished from their new location.

    Images I move from one folder into another are disappearing, at first it was occurring immediately and only if I did not copy and paste the images. Today however I found out that images I had been using for days just up and vanished from their new location. A few of these images had copies in another folder that remained and the ones I couldn't find may or may not have ever had copies. I had backed up my system with time machine recently as well, so I went into the backup to retreive the lost images. When I searched and found the backup copies I got the error "the file alias cannot be opened because the original cannot be located". The crazy thing is that the images that remained on my computer did not give me the same error and opened like they should.
    In short images that I am creating, saving, and using are disapearing so epically that even time machine versions are affected when retrieval is attempted. Any suggestions as to what I could be doing wrong without realizing it? Or perhaps other people have had similar bugs that are software related and have a solution?

    Hi Kevin,
    I understand what you tried to do but it doesn't work that way. Swapping drive names will just mess things up.
    You should be able to reconnect the files though: in the Locate Referenced Files dialog make sure you click the Show Reconnect Options button — this will give you access to all the connected drives. Locate one of the files and hit Reconnect All. Should do the trick.
    Best

  • Restoring from backup to new location + not wanting files renamed

    Hi there - I am a newbie to this forum.
    I have a Win XP machine that recently needed to have the hard drive wiped and the OS re-installed. Fortunately I had a recent backup of all my data so I'm in a good starting position for a restore. However I have some questions as the re-install of the OS has had a change in profile name.
    Before my machine was wiped, I had a "Daniel" profile and so my "iTunes Media folder location" was set to C:\Documents and Settings\Daniel\My Documents\My Music.
    After my PC was rebuilt, a new profile was created called "dnl". iTunes has been re-installed (version 10.1.2.17). The first time I launched iTunes the "iTunes Media folder location" was set to C:\Documents and Settings\dnl\My Documents\My Music. I did NOT choose to have iTunes browse/load my files and so currently iTunes shows no files or playlists.
    I have recreated the C:\Documents and Settings\Daniel\My Documents\My Music folder from my backup so all the music files and the previous libraries are presently in this location. However even if I change the "iTunes Media folder location" within iTunes to point to the "old" location, it doesn’t find the files or libraries.
    I have tried deleting the following files from C:\Documents and Settings\dnl\My Documents\My Music\iTunes:
    iTunes Library Extras.itdb
    iTunes Library Genius.itdb
    iTunes Library.itl
    iTunes Music Library.xml
    I did this assuming that the next time iTunes was launched, it would refer to the old location (as specified as my "iTunes Media folder location"), find the old library files there and use them. However it seems to ignore them and simply re-creates the files above in the location I deleted them from.
    So…I am unclear as to the best strategy to have iTunes point back to the old location to find the music files and retain all the playlists I had.
    I found this thread: http://support.apple.com/kb/ht1364. It mentions a method that will find all the files in the old location and move them to the new location, retaining playlists, artwork etc. Once the data has been copied, it advises to delete the files from the original location.
    However I see that it mentions checking the ‘Keep iTunes Media folder organized’ option. From reading other threads I know this to be a source of one very irritating problem – it renames all the file names. I have specifically ripped my music using software other than iTunes for the express purpose of being able to tag the filenames with more specific info than iTunes would. I like the files to named <artist>-<album>-<track no>-<track title>.<file extension> and am not a fan of the very basic filenames that iTunes assigns.
    So…I want to have iTunes simply point back to the old location and not do any file renaming. I am happy enough to have iTunes simply point to a location that isn’t technically part of my current profile. I don’t have a desperate need for the files to be moved to the dnl profile.
    So…I am guessing there has to be some other file (or registry setting?) that knows where best to point to? Can this modified?
    I would be appreciative of any assistance.
    Thanks!

    Hi Daniel, welcome to Apple Discussions.
    The default location for the iTunes library folder is <Profile><Music>iTunes with the media folder at *<Profile>\<Music>\iTunes\iTunes Media*. For XP the <Music> folder is My Documents\My Music.
    When iTunes starts up it looks for a set of library files in the last location it used. If that location is also the default location and it can't find the library files it assumes it has just been installed and creates a new set.
    Changing the location of the *iTunes Media* folder in preferences does not change which set of library files it opens, nor will it automatically detect all the media in that location.
    Ideally you should restore the folder iTunes and all its subfolders from your backup into *C:\Documents and Settings\dnl\My Documents\My Music* and start iTunes. All should work perfectly and, as far as iTunes is concerned, you will be using the same library as you used to. Any iDevices should sync without problems.
    If you media was not is the usual locations or you are unable to restore the entire iTunes folder then things become more complicated. Post back if you still have trouble and I will try to go into more detail.
    tt2

  • I backup my iphone on my pc and copy backup file into drive D then i setup new OS on my pc. after that i copy the backup file into itunes backup folder but itunes can't find the backup file to restore my iphone. how can i restore my iphone?

    i backup my iphone on my pc and copy backup file into drive D then i setup new OS on my pc. after that i copy the backup file into itunes backup folder but itunes can't find the backup file to restore my iphone. how can i restore my iphone?

    Don't you just love changes in interfaces?
    I found the answer to my question.
    When you attach a device (iPhone, iPad) to your Mac, in iTunes 12 in appears as a tiny icon in the second "row" of the iTunes display. Click the icon and the familiar iTunes display for the device appears. From there, on the Summary screen, you can see the dates of the backups of the device (iCloud and backup to the computer). You can also use the button to backup to the computer.

  • I changed the name of my imac to reflect it's new location, but inadvertently, it unchecked the box to allow the administrative user account to administer the machine.  I am trying to reinstall with a time machine backup and am unable

    I need a little help.  I changed the name of my imac to reflect it's new location, but inadvertently, when I saved the change, the system unchecked the "allow this account to administer the computer" so now I can't make any changes....  I tried to reinstall the OS, but it wants me to go online while installing and use my apple id, but this is not my iMAC, so I don't want to use my account.  Also, I am working in a building that will not allow me network access until I authenticate, which can't be done unless I open the browser first.  So I'm stuck at this point.  I can't even restore a time machine backup that I have because it gives the error message that the system encountered an error.  Any suggestions?

    Ask the admin/owner to sign in. 

  • HT1364 I am trying to change the location of my itunes library and the library doesn't copy to the new location.

    I am trying to change the location of my itunes library and the library doesn't copy to the new location.  I followed the steps in the help message (HT1364) exactly but I am not prompted to consolidate in the new location.  Any suggestions?

    Hi ed2345 and diesel vdub,  when I consolidate, the files start copying to the new location and part way through I get a message that the copying failed (-50) even though all of the files are already in place in the new location.  Should I just delete the itunes in the new location and manually copy the files again?  But in itunes, I have indicated that the new location is on the external hard drive but itunes seems to ignore that and still looks for itunes in the original location on my internal hard drive.  I disabled the location of itunes in the old location (by labelling itunes as "itunes old") and it tried to create a new itunes at the old location and prompted me to import music and videos from a CD or other source.  I hope I am making sense.

  • In PC I have changed the default app location from default to other and copied the previous content to new location and changed the Default location in preference. Now in iTunes, my previous apps are not visible. Is there any solution to this?

    In PC I have changed the default app location from (c:/users/username/My Music/iTunes) to other (d:iTunes)  and copied the previous content to new location and changed the Default location in Edit preference. Now in iTunes, my previous apps are not visible. Is there any solution to this?

    wjosten - thanks very much for the detailed instructions. I'll try this as soon as I get my replacement and it looks like you'll probably end up getting the green tick

  • HT1414 is it possible to copy backup taken from my old iphone to the new one?

         is it possible to copy backup taken from my old iphone to the new one?

    Sure you can, as long as you still have the old backup.
    I have restored my iDevices (iPod Touches, iPads and iPhone) from backups a number of times. I've done this long after the devices were setup. Sometimes I restore from a backup because I have tweaked a setting and it screwed something up on my device so I restored from a backup in order to restore the previous "non-tweaked" setting back to the device.

  • How to Copy backup to new ipad

    How to Copy backup to new ipad?

    Do you mean this via icloud. Today i had my ipad3 exchanged under warranty (dust under the screen)(Thankyou apple for easy replacement). I had to set up ipad as new. Then check what ios version i had on this new ipad (it was 5) the icloud version from my previous ipad was 6 which was preventing my new ipad accepting the upload from the icloud. First i updated the software on new ipad to ios6 , after that had completed , i had to reset and erase settings again.  Then new ipad could see my old icloud account and downloaded it. All is back on. Happy days.

  • HT1229 When i copy my iPhoto file (261 GB) in a new location i see only 30 photos

    Problems moving iPhoto library

    Sorry - do you want help? If so you must give more than "something does not work"
    What version of iPhoto? Is OS X 10.5.2 as you state correct for your OS? What format is the drive that you moved the iphoto library to? How exactly did you move it?
    the general answer is
    Moving the iPhoto library is safe and simple - quit iPhoto and drag the iPhoto library intact as a single entity to the external drive - depress the option key and launch iPhoto using the "select library" option to point to the new location on the external drive - fully test it and then trash the old library on the internal drive (test one more time prior to emptying the trash)
    And be sure that the External drive is formatted Mac OS extended (journaled) (iPhoto does not work with drives with other formats) and that it is always available prior to launching iPhoto
    And backup soon and often - having your iPhoto library on an external drive is not a backup and if you are using Time Machine you need to check and be sure that TM is backing up your external drive
    LN

  • Unknown Error When Collecting Files to Copy to a New Location

    Going crazy!! I'm trying to shift over and start working on a different computer but although I created my project on an external hard drive so that this move would be easy, I realised too late that my Auto-save folders were saving to the computer's Documents folder. I copied these onto the external hard drive but it still doesn't work when I open Premiere. Instead of five sequences in my project, opening it on my new computer, there only appears to be one sequence and all my bins are not the latest updated state in which they appear on my old computer.
    I decided to go to the Project Manager and Collect the Files to Copy to a New Location, making sure the Project destination is on my external hard drive, however three times the copying process has stopped due to an Unknown Error. Does anyone know what this error could be?
    I only have possession of the old computer for another 24 hours so if I can't find a solution I will have to lose four solid months of work.
    Thank you in advance!!

    Hi Kylie,
    Which version of Premiere Pro are you using? Can we get more details about this case? FAQ: What information should I provide when asking a question on this forum?
    Thanks,
    Kevin

  • After refomatting HDD, reinstalling Vista plus service packs, recovering files from Microsoft backup and installing new copy of Firefox 3.6, Firefox will not run. Have deleted all old M0zilla profile folders and reinstalled Firefox again to no avail.

    After computer would not boot and could not be repaired, I reformatted the HDD, reinstalled Vista plus all service packs and updates, recovered files from Microsoft backup and installed new copy of Firefox 3.6. Firefox will not run.
    I have deleted all old M0zilla profile folders and reinstalled Firefox again to no avail.
    After intallation was completed, the automatic startup just placed a small box on the screen "Import wiz". This just sat there with no activity. After I closed this box, none of the Firefox start buttons would open the program -- just the blue rotating icon for about 2 seconds then nothing.

    Hi,
    Please try to rebuil the WMI repository.
    For detailed information, please refer to the blog below,
    http://blogs.technet.com/b/askperf/archive/2009/04/13/wmi-rebuilding-the-wmi-repository.aspx
    If issue persists, due to sfc doesn't work, could you try to repair your system with the installation disk?
    Best Regards.
    Steven Lee
    TechNet Community Support

Maybe you are looking for

  • Is this a bug in Installer Source Files Display

    When building an installer in a LabVIEW project I added a directory under [SystemFolder] in the Destination View and it appeared normally in the Destination Window, with the - sign at the left. After exiting and reopening the Installer Builder there

  • Payment Terms Change

    Dear All, While Doing the MIRO Imn't able to change the Payment terms.I m getting the Following Meaasge Terms of payment changed; Check      Message no. F5231 Effects on Customizing      You can define when this system message is to be issued in acco

  • Http Reciver Adapter Header fields

    Hi all, I have a requirement where the receiver is http adapter. I have some fields/structure to be added to the http request. One way i know is to generate entire envelope in the map and then send as request having header and body both. Is there any

  • Ardour crashes with some LV2 plugins

    I don't know why, but many of my LV2 plugins stopped working on Ardour. When I try to start a previous session done on Ardour - which has LV2 plugins - it freezes or just force-closes. Here's the output in Ardour of a session that just forced-closed

  • Extreme difficulty with simple program.. Help appreciated

    Ok..I'm currently attempting to make a menu driven program that will end up calling on an earlier developed method in order to find the smallest, or largest number from an array. Unfortunatly my teacher rarely explained anything and i am finding this