I am deploying a stored procedure that checks the backup age of every server I support. However, with a little of programming experience that I have, I know I can add or delete part of my code with an IF statement that checks the versions it runs on.

Here's the code :

```
SELECT DISTINCT 
    SUBSTRING(s.name,1,50) AS 'DATABASE Name',
    b.backup_start_date AS 'Full DB Backup Status',
    c.backup_start_date AS 'Differential DB Backup Status',
    d.backup_start_date AS 'Transaction Log Backup Status',
    DATEDIFF(HOUR, b.backup_start_date, GETDATE()) AS 'Age Full Backup',
    --Get number hours since last full backup
    ISNULL(DATEDIFF(HOUR, c.backup_start_date, GETDATE()), 0) AS 'Age Diff Backup',
    --Get number hours since last diff backup
    ISNULL(DATEDIFF(HOUR, d.backup_start_date, GETDATE()), 0) AS 'Age Tlog Backup',
    --Get number hours since last tlog backup
    s2.state_desc AS 'Status',
    s2.recovery_model
FROM 
    MASTER..sysdatabases s 
LEFT OUTER JOIN 
    msdb..backupset b ON s.name = b.database_name 
                      AND b.backup_start_date = (SELECT MAX(backup_start_date) AS 'Full DB Backup Status'
                                                 FROM msdb..backupset
                                                 WHERE database_name = b.database_name
                                                   AND TYPE = 'D') -- full database backups only, not log backups
LEFT OUTER JOIN 
    master.sys.databases s2 ON s2.name = b.database_name
LEFT OUTER JOIN 
    msdb..backupset c ON s.name = c.database_name 
                      AND c.backup_start_date = (SELECT MAX(backup_start_date) 'Differential DB Backup Status'
                                                 FROM msdb..backupset
                                                 WHERE database_name = c.database_name 
                                                   AND TYPE = 'I')
LEFT OUTER JOIN 
    msdb..backupset d ON s.name = d.database_name 
                      AND d.backup_start_date = (SELECT MAX(backup_start_date) 'Transaction Log Backup Status'
                                                 FROM msdb..backupset
                                                 WHERE database_name = d.database_name  
                                                   AND TYPE = 'L')
WHERE 
    s.name != 'tempdb' 
    AND s.name NOT IN ('master', 'model', 'msdb') 
    AND s.name NOT LIKE '%TMP[_]%'

```

I was wondering if I can add this part to the WHERE clause at the very end : 

```
    AND dbid NOT IN (SELECT database_id
                     FROM master.sys.dm_hadr_database_replica_states AS DRS
                     INNER JOIN sys.dm_hadr_availability_replica_states AS ARS ON ARS.replica_id = DRS.replica_id
                     WHERE ARS.is_local = 1 AND role_desc = 'SECONDARY')

```

but only for SQL Server higher or equals to 2012? Since AlwaysOn was introduced in 2012, this code generates an error on 2008 R2 and lower.

So far, I have this IF statement that checks the version which works pretty well :

```
IF CONVERT(int, SUBSTRING(@@VERSION, 22, 4)) >= 2012
    PRINT 'true'
ELSE
    PRINT 'false'

```

I know that I can just delete the code and create my Stored Procedure, but I want to know if it's possible to place an IF statement like we can do in C# or even in JavaScript to adjust our code from the versions it runs on.

Thanks!