There are always some new features in the database server, which has SQL version checking requirements. For example: GRANT VIEW DATABASE PERFORMANCE STATE TO [user] can run, if your server at least SQL Server 2022.
In this case if you don’t want to see error in an older version , you have to check it:
1 2 3 4 5 6 7 8 9 10 11 |
IF (CAST(LEFT( CAST(SERVERPROPERTY('ProductVersion') as varchar(50)), 2) AS INT) >= 16) BEGIN -- Execute the command if the version is at least SQL Server 2022 SELECT @@SERVERNAME AS ServeName,@@VERSION AS Version, 'GREAT! SQL Server version is at least 2022' as Message; END ELSE BEGIN -- Optional: Handle the case where the version is less than SQL Server 2022 SELECT @@SERVERNAME AS ServeName,@@VERSION AS Version, 'SQL Server version is less than 2022. There is no : VIEW DATABASE PERFORMANCE STATE.' as Message; END |