T-SQL query for date of last full backup, size and locationrestore db from .bak file that contains multi...

How does residential electricity work?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Is a roofing delivery truck likely to crack my driveway slab?

Is there a good way to store credentials outside of a password manager?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Failed to fetch jessie backports repository

Is there any reason not to eat food that's been dropped on the surface of the moon?

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?

The plural of 'stomach"

Coordinate position not precise

Products and sum of cubes in Fibonacci

Why is delta-v is the most useful quantity for planning space travel?

Is HostGator storing my password in plaintext?

Can criminal fraud exist without damages?

Why are on-board computers allowed to change controls without notifying the pilots?

Go Pregnant or Go Home

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Why is `const int& k = i; ++i; ` possible?

Why does John Bercow say “unlock” after reading out the results of a vote?

Stereotypical names

How can I replace every global instance of "x[2]" with "x_2"

Is this Spell Mimic feat balanced?

At which point does a character regain all their Hit Dice?

is this a spam?



T-SQL query for date of last full backup, size and location


restore db from .bak file that contains multi backupQuery that displays backup information (successful and failed) SQL ServerGenerate Backup listHow to know that , Is it Maintenance Cleanup Task is working properly from maintenance Plan or not through T-SQL?Query to list “last backup time” listing wrong times ( SQL Server 2008 R2 )MSDB log file sizeIs an MDF “Last Modified Date” affected by transaction log backup?Ola Hallengren backup failedQuery to Find Out How Many Days Until Database Is FullHow to order databases by size? (For use in full backups)













4















I'm using the below T-SQL query to determine the date of the last full database backup and also return the size and location of the backup file. My problem is that it won't return any data at all for databases that have had no backups or for which there is no backup history for. Ideally, I'd want to modify the query so that all databases are returned, regardless of whether or not there's any backup history for them. Can anyone advise how the below query could be modified to accommodate this?



WITH LastBackUp AS
(
SELECT bs.database_name,
bs.backup_size,
bs.backup_start_date,
bmf.physical_device_name,
Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0
)
SELECT
database_name AS [Database],
CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
backup_start_date AS [Last Full DB Backup Date],
physical_device_name AS [Backup File Location]
FROM LastBackUp
WHERE Position = 1
ORDER BY [Database];









share|improve this question



























    4















    I'm using the below T-SQL query to determine the date of the last full database backup and also return the size and location of the backup file. My problem is that it won't return any data at all for databases that have had no backups or for which there is no backup history for. Ideally, I'd want to modify the query so that all databases are returned, regardless of whether or not there's any backup history for them. Can anyone advise how the below query could be modified to accommodate this?



    WITH LastBackUp AS
    (
    SELECT bs.database_name,
    bs.backup_size,
    bs.backup_start_date,
    bmf.physical_device_name,
    Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
    FROM msdb.dbo.backupmediafamily bmf
    JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
    JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
    WHERE bs.[type] = 'D'
    AND bs.is_copy_only = 0
    )
    SELECT
    database_name AS [Database],
    CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
    backup_start_date AS [Last Full DB Backup Date],
    physical_device_name AS [Backup File Location]
    FROM LastBackUp
    WHERE Position = 1
    ORDER BY [Database];









    share|improve this question

























      4












      4








      4








      I'm using the below T-SQL query to determine the date of the last full database backup and also return the size and location of the backup file. My problem is that it won't return any data at all for databases that have had no backups or for which there is no backup history for. Ideally, I'd want to modify the query so that all databases are returned, regardless of whether or not there's any backup history for them. Can anyone advise how the below query could be modified to accommodate this?



      WITH LastBackUp AS
      (
      SELECT bs.database_name,
      bs.backup_size,
      bs.backup_start_date,
      bmf.physical_device_name,
      Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
      FROM msdb.dbo.backupmediafamily bmf
      JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
      JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
      WHERE bs.[type] = 'D'
      AND bs.is_copy_only = 0
      )
      SELECT
      database_name AS [Database],
      CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
      backup_start_date AS [Last Full DB Backup Date],
      physical_device_name AS [Backup File Location]
      FROM LastBackUp
      WHERE Position = 1
      ORDER BY [Database];









      share|improve this question














      I'm using the below T-SQL query to determine the date of the last full database backup and also return the size and location of the backup file. My problem is that it won't return any data at all for databases that have had no backups or for which there is no backup history for. Ideally, I'd want to modify the query so that all databases are returned, regardless of whether or not there's any backup history for them. Can anyone advise how the below query could be modified to accommodate this?



      WITH LastBackUp AS
      (
      SELECT bs.database_name,
      bs.backup_size,
      bs.backup_start_date,
      bmf.physical_device_name,
      Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
      FROM msdb.dbo.backupmediafamily bmf
      JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
      JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
      WHERE bs.[type] = 'D'
      AND bs.is_copy_only = 0
      )
      SELECT
      database_name AS [Database],
      CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
      backup_start_date AS [Last Full DB Backup Date],
      physical_device_name AS [Backup File Location]
      FROM LastBackUp
      WHERE Position = 1
      ORDER BY [Database];






      sql-server t-sql backup msdb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 21 '16 at 7:18









      FzaFza

      3661414




      3661414






















          2 Answers
          2






          active

          oldest

          votes


















          8














          You need to query the list of databases first (it's in sys.databases), then join it to your query:



          WITH LastBackUp AS
          (
          SELECT bs.database_name,
          bs.backup_size,
          bs.backup_start_date,
          bmf.physical_device_name,
          Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
          FROM msdb.dbo.backupmediafamily bmf
          JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
          JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
          WHERE bs.[type] = 'D'
          AND bs.is_copy_only = 0
          )
          SELECT
          sd.name AS [Database],
          CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
          backup_start_date AS [Last Full DB Backup Date],
          physical_device_name AS [Backup File Location]
          FROM sys.databases AS sd
          LEFT JOIN LastBackUp AS lb
          ON sd.name = lb.database_name
          AND Position = 1
          ORDER BY [Database];





          share|improve this answer































            0














            Just to add to the beautiful answer by Spaghettidba:



            what if you backup to more than one file?
            this script will show you it all.



                    declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
            ;with Radhe as (
            SELECT @@Servername as [Server_Name],
            B.name as Database_Name,
            ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
            ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
            ,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
            ,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
            ,media_set_id = MAX(A.media_set_id)
            ,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
            ,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
            ,A.type
            FROM sys.databases B

            LEFT OUTER JOIN msdb.dbo.backupset A
            ON A.database_name = B.name
            AND A.is_copy_only = 0
            AND (@backup_type IS NULL OR A.type = @backup_type )

            GROUP BY B.Name, A.type

            )

            SELECT r.[Server_Name]
            ,r.Database_Name
            ,[Backup Type] = r.type
            ,r.DaysSinceLastBackup
            ,r.LastBackupDate
            ,r.BackupSize_GB
            ,r.BackupSize_MB
            ,F.physical_device_name
            ,r.[AVG Backup Duration]
            ,r.[Longest Backup Duration]

            FROM Radhe r

            LEFT OUTER JOIN msdb.dbo.backupmediafamily F
            ON R.media_set_id = F.media_set_id

            ORDER BY r.Server_Name, r.Database_Name





            share|improve this answer

























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "182"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f150241%2ft-sql-query-for-date-of-last-full-backup-size-and-location%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              8














              You need to query the list of databases first (it's in sys.databases), then join it to your query:



              WITH LastBackUp AS
              (
              SELECT bs.database_name,
              bs.backup_size,
              bs.backup_start_date,
              bmf.physical_device_name,
              Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
              FROM msdb.dbo.backupmediafamily bmf
              JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
              JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
              WHERE bs.[type] = 'D'
              AND bs.is_copy_only = 0
              )
              SELECT
              sd.name AS [Database],
              CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
              backup_start_date AS [Last Full DB Backup Date],
              physical_device_name AS [Backup File Location]
              FROM sys.databases AS sd
              LEFT JOIN LastBackUp AS lb
              ON sd.name = lb.database_name
              AND Position = 1
              ORDER BY [Database];





              share|improve this answer




























                8














                You need to query the list of databases first (it's in sys.databases), then join it to your query:



                WITH LastBackUp AS
                (
                SELECT bs.database_name,
                bs.backup_size,
                bs.backup_start_date,
                bmf.physical_device_name,
                Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
                FROM msdb.dbo.backupmediafamily bmf
                JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
                JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
                WHERE bs.[type] = 'D'
                AND bs.is_copy_only = 0
                )
                SELECT
                sd.name AS [Database],
                CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
                backup_start_date AS [Last Full DB Backup Date],
                physical_device_name AS [Backup File Location]
                FROM sys.databases AS sd
                LEFT JOIN LastBackUp AS lb
                ON sd.name = lb.database_name
                AND Position = 1
                ORDER BY [Database];





                share|improve this answer


























                  8












                  8








                  8







                  You need to query the list of databases first (it's in sys.databases), then join it to your query:



                  WITH LastBackUp AS
                  (
                  SELECT bs.database_name,
                  bs.backup_size,
                  bs.backup_start_date,
                  bmf.physical_device_name,
                  Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
                  FROM msdb.dbo.backupmediafamily bmf
                  JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
                  JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
                  WHERE bs.[type] = 'D'
                  AND bs.is_copy_only = 0
                  )
                  SELECT
                  sd.name AS [Database],
                  CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
                  backup_start_date AS [Last Full DB Backup Date],
                  physical_device_name AS [Backup File Location]
                  FROM sys.databases AS sd
                  LEFT JOIN LastBackUp AS lb
                  ON sd.name = lb.database_name
                  AND Position = 1
                  ORDER BY [Database];





                  share|improve this answer













                  You need to query the list of databases first (it's in sys.databases), then join it to your query:



                  WITH LastBackUp AS
                  (
                  SELECT bs.database_name,
                  bs.backup_size,
                  bs.backup_start_date,
                  bmf.physical_device_name,
                  Position = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
                  FROM msdb.dbo.backupmediafamily bmf
                  JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
                  JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
                  WHERE bs.[type] = 'D'
                  AND bs.is_copy_only = 0
                  )
                  SELECT
                  sd.name AS [Database],
                  CAST(backup_size / 1048576 AS DECIMAL(10, 2) ) AS [BackupSizeMB],
                  backup_start_date AS [Last Full DB Backup Date],
                  physical_device_name AS [Backup File Location]
                  FROM sys.databases AS sd
                  LEFT JOIN LastBackUp AS lb
                  ON sd.name = lb.database_name
                  AND Position = 1
                  ORDER BY [Database];






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 21 '16 at 7:34









                  spaghettidbaspaghettidba

                  9,9771835




                  9,9771835

























                      0














                      Just to add to the beautiful answer by Spaghettidba:



                      what if you backup to more than one file?
                      this script will show you it all.



                              declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
                      ;with Radhe as (
                      SELECT @@Servername as [Server_Name],
                      B.name as Database_Name,
                      ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
                      ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
                      ,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
                      ,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
                      ,media_set_id = MAX(A.media_set_id)
                      ,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                      ,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                      ,A.type
                      FROM sys.databases B

                      LEFT OUTER JOIN msdb.dbo.backupset A
                      ON A.database_name = B.name
                      AND A.is_copy_only = 0
                      AND (@backup_type IS NULL OR A.type = @backup_type )

                      GROUP BY B.Name, A.type

                      )

                      SELECT r.[Server_Name]
                      ,r.Database_Name
                      ,[Backup Type] = r.type
                      ,r.DaysSinceLastBackup
                      ,r.LastBackupDate
                      ,r.BackupSize_GB
                      ,r.BackupSize_MB
                      ,F.physical_device_name
                      ,r.[AVG Backup Duration]
                      ,r.[Longest Backup Duration]

                      FROM Radhe r

                      LEFT OUTER JOIN msdb.dbo.backupmediafamily F
                      ON R.media_set_id = F.media_set_id

                      ORDER BY r.Server_Name, r.Database_Name





                      share|improve this answer






























                        0














                        Just to add to the beautiful answer by Spaghettidba:



                        what if you backup to more than one file?
                        this script will show you it all.



                                declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
                        ;with Radhe as (
                        SELECT @@Servername as [Server_Name],
                        B.name as Database_Name,
                        ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
                        ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
                        ,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
                        ,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
                        ,media_set_id = MAX(A.media_set_id)
                        ,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                        ,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                        ,A.type
                        FROM sys.databases B

                        LEFT OUTER JOIN msdb.dbo.backupset A
                        ON A.database_name = B.name
                        AND A.is_copy_only = 0
                        AND (@backup_type IS NULL OR A.type = @backup_type )

                        GROUP BY B.Name, A.type

                        )

                        SELECT r.[Server_Name]
                        ,r.Database_Name
                        ,[Backup Type] = r.type
                        ,r.DaysSinceLastBackup
                        ,r.LastBackupDate
                        ,r.BackupSize_GB
                        ,r.BackupSize_MB
                        ,F.physical_device_name
                        ,r.[AVG Backup Duration]
                        ,r.[Longest Backup Duration]

                        FROM Radhe r

                        LEFT OUTER JOIN msdb.dbo.backupmediafamily F
                        ON R.media_set_id = F.media_set_id

                        ORDER BY r.Server_Name, r.Database_Name





                        share|improve this answer




























                          0












                          0








                          0







                          Just to add to the beautiful answer by Spaghettidba:



                          what if you backup to more than one file?
                          this script will show you it all.



                                  declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
                          ;with Radhe as (
                          SELECT @@Servername as [Server_Name],
                          B.name as Database_Name,
                          ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
                          ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
                          ,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
                          ,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
                          ,media_set_id = MAX(A.media_set_id)
                          ,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                          ,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                          ,A.type
                          FROM sys.databases B

                          LEFT OUTER JOIN msdb.dbo.backupset A
                          ON A.database_name = B.name
                          AND A.is_copy_only = 0
                          AND (@backup_type IS NULL OR A.type = @backup_type )

                          GROUP BY B.Name, A.type

                          )

                          SELECT r.[Server_Name]
                          ,r.Database_Name
                          ,[Backup Type] = r.type
                          ,r.DaysSinceLastBackup
                          ,r.LastBackupDate
                          ,r.BackupSize_GB
                          ,r.BackupSize_MB
                          ,F.physical_device_name
                          ,r.[AVG Backup Duration]
                          ,r.[Longest Backup Duration]

                          FROM Radhe r

                          LEFT OUTER JOIN msdb.dbo.backupmediafamily F
                          ON R.media_set_id = F.media_set_id

                          ORDER BY r.Server_Name, r.Database_Name





                          share|improve this answer















                          Just to add to the beautiful answer by Spaghettidba:



                          what if you backup to more than one file?
                          this script will show you it all.



                                  declare @backup_type CHAR(1) = 'D' --'D' full, 'L' log
                          ;with Radhe as (
                          SELECT @@Servername as [Server_Name],
                          B.name as Database_Name,
                          ISNULL(STR(ABS(DATEDIFF(day, GetDate(), MAX(Backup_finish_date)))), 'NEVER') as DaysSinceLastBackup,
                          ISNULL(Convert(char(11), MAX(backup_finish_date), 113)+ ' ' + CONVERT(VARCHAR(8),MAX(backup_finish_date),108), 'NEVER') as LastBackupDate
                          ,BackupSize_GB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00/1024.00 AS NUMERIC(18,2))
                          ,BackupSize_MB=CAST(COALESCE(MAX(A.BACKUP_SIZE),0)/1024.00/1024.00 AS NUMERIC(18,2))
                          ,media_set_id = MAX(A.media_set_id)
                          ,[AVG Backup Duration]= AVG(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                          ,[Longest Backup Duration]= MAX(CAST(DATEDIFF(s, A.backup_start_date, A.backup_finish_date) AS int))
                          ,A.type
                          FROM sys.databases B

                          LEFT OUTER JOIN msdb.dbo.backupset A
                          ON A.database_name = B.name
                          AND A.is_copy_only = 0
                          AND (@backup_type IS NULL OR A.type = @backup_type )

                          GROUP BY B.Name, A.type

                          )

                          SELECT r.[Server_Name]
                          ,r.Database_Name
                          ,[Backup Type] = r.type
                          ,r.DaysSinceLastBackup
                          ,r.LastBackupDate
                          ,r.BackupSize_GB
                          ,r.BackupSize_MB
                          ,F.physical_device_name
                          ,r.[AVG Backup Duration]
                          ,r.[Longest Backup Duration]

                          FROM Radhe r

                          LEFT OUTER JOIN msdb.dbo.backupmediafamily F
                          ON R.media_set_id = F.media_set_id

                          ORDER BY r.Server_Name, r.Database_Name






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 4 mins ago

























                          answered Sep 22 '16 at 14:29









                          marcello miorellimarcello miorelli

                          5,9562162143




                          5,9562162143






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Database Administrators Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f150241%2ft-sql-query-for-date-of-last-full-backup-size-and-location%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

                              Always On Availability groups resolving state after failover - Remote harden of transaction...

                              update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...