Solved

UNION operator for TSQL code

Posted on 2012-03-13
5
313 Views
Last Modified: 2012-03-13
Hi All,

Please guide me how to combine below two scripts using UNION operator.

Script 1:
=========
SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 7
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'D'
       AND K.Backup_Start_Date >= getdate()-7
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
ORDER BY K.[Type] DESC

Script 2:
===========
SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 2
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'I'
       AND K.Backup_Start_Date >= getdate()-2
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
ORDER BY K.[Type] DESC
0
Comment
Question by:sg05121983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 125 total points
ID: 37713813
SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 7
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END,
              K.[Type]
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'D'
       AND K.Backup_Start_Date >= getdate()-7
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
union all
SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 2
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END,
              K.[Type]
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'I'
       AND K.Backup_Start_Date >= getdate()-2
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
ORDER BY K.[Type] DESC
0
 
LVL 8

Expert Comment

by:rushShah
ID: 37713949
you dont need union operator for this, try this,


SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                               WHEN max(k.backup_start_date) < GETDATE() - 7
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     WHEN max(k.backup_start_date) < GETDATE() - 2
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And ((K.[Type] = 'I'
       AND K.Backup_Start_Date >= getdate()-2) OR (K.[Type] = 'D'
       AND K.Backup_Start_Date >= getdate()-7))
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
ORDER BY K.[Type] DESC
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 37713966
My post should have been

SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 7
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END,
              K.[Type]
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'D'
       AND K.Backup_Start_Date >= getdate()-7
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
union
SELECT
(SELECT RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as Instance_Name,
              M.Name As Database_Name,
              K.Backup_Start_Date,
              K.Backup_Finish_Date,
              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential'
              END,
              Note = CASE
                     WHEN max(k.backup_start_date) < GETDATE() - 2
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/      
                     WHEN ISNULL(max(k.backup_start_date),0) = 0
                     THEN 'ALERT'
                     ELSE '-----'
              END,
              K.[Type]
FROM Sys.SysDatabases M (NOLOCK)
       INNER JOIN Sys.SysAltFiles SF (NOLOCK)
       ON M.[DBId] = SF.[DBId]
       LEFT OUTER JOIN MSDB..BackupSET K (NOLOCK)
       ON M.Name = K.Database_Name And K.[Type] = 'I'
       AND K.Backup_Start_Date >= getdate()-2
WHERE M.[Dbid] <> 2
GROUP BY M.Name, M.crdate, K.Backup_Start_Date, K.Backup_Finish_Date, K.[Type], K.User_Name
ORDER BY K.[Type] DESC
0
 
LVL 25

Accepted Solution

by:
jogos earned 125 total points
ID: 37714282
Im all for the non-union sollution  (when you start changing then you don't have to change things twice) so you only must identify cleart the things that are different.

So in addition to that sollution

See that there are 2 problems in the case-when
- in that sollution the check for type was 'forgotten' for 7 or 2 days
- already in original ' WHEN ISNULL(max(k.backup_start_date),0) = 0 ' when it is not null then you are comaparing a date to 0 (an int)


              Backup_Type = CASE Upper(K.[Type])
                     WHEN 'D' THEN 'Full Backup'
                     WHEN 'I' THEN 'Differential' 
              END,
              Note = CASE
                               WHEN max(k.backup_start_date) <  GETDATE() - 7 AND 
                                                            Upper(K.[Type]) = 'D'
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 7 days*/ 
                     WHEN max(k.backup_start_date) < GETDATE() - 2 AND 
                                                            Upper(K.[Type]) = 'I'
                     THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 2 days*/
                     --WHEN max(k.backup_start_date) < DATEADD(HH, - 168, GETDATE()) THEN 'ALERT' /*raise an ALERT, if a backup hadn't occurred in last 168 hours*/       
                     WHEN max(k.backup_start_date)  is null 
                     THEN 'ALERT'
                     ELSE '-----'
              END

Open in new window

Didn't check the whole sql for the rest
0
 

Author Closing Comment

by:sg05121983
ID: 37718416
--
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can we attach PDF to table 2 46
SQL Server Agent Job Error 13 79
SQL / Table Lock? 7 40
How do i delete the last node in an xml in T-SQL 7 26
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question