• DECLARE @dbname VARCHAR(max),

    @fullfilename VARCHAR(max),

    @getfullfileq VARCHAR(max),

    @fullfilebackupq VARCHAR(max),

    @deldate datetime,

    @getdate-2 VARCHAR(30),

    @gettime VARCHAR(8),

    @hh VARCHAR (2),

    @mm VARCHAR(2),

    @ss VARCHAR(2)

    /*This command will get all Producation databases with any recovery model*/

    DECLARE getdbname CURSOR FOR

    SELECT nameFROM sys.databases

    Where name in ('test1','test2','test3','test4','test5','test6',test7')

    OPEN getdbname

    FETCH NEXT FROM getdbname

    INTO @dbname

    WHILE @@FETCH_STATUS =0

    BEGIN

    /* Below section will convert current date and time into varchar so we can put as file name*/

    SET @getdate-2= CONVERT(VARCHAR(30), GETDATE(),110)

    SET @gettime=CONVERT(VARCHAR(8), GETDATE(),108)

    SET @hh = SUBSTRING(@gettime,1,2)

    SET @mm = SUBSTRING(@gettime,4,5)

    SET @ss = SUBSTRING(@gettime,7,8)

    /*Below command will store backup full file query

    Make path modification so we can use from any database*/

    SET @fullfilebackupq ='BACKUP DATABASE ['+@dbname+']

    TO DISK=''E:\Full\'+@dbname+'_full-BACKUP_'+@getdate+'-'+@hh+'-'+@mm+'-'+@ss+'.bak''

    WITH

    RETAINDAYS = 30,

    NOFORMAT,

    NOINIT,

    NAME ='''+@dbname+'fullbackup'+@getdate+'_'+@gettime+''''

    /*Here query executed*/

    --print @fullfilebackupq

    EXEC( @fullfilebackupq)

    FETCH NEXT FROM getdbname

    INTO @dbname

    END

    CLOSE getdbname

    DEALLOCATE getdbname