DECLARE @ver VARCHAR(MAX)
SELECT @ver = CAST(SERVERPROPERTY('productversion') AS VARCHAR(100))
IF CAST(SUBSTRING(@ver, 0, CHARINDEX('.', @ver)) AS INT) >= 13 -- SQL Server 2016 or newer
BEGIN
  IF EXISTS (SELECT * FROM sys.server_audits WHERE Name = 'Audit_GDPR_Server') 
  BEGIN
    IF NOT EXISTS (SELECT * FROM sys.database_audit_specifications WHERE Name = 'GDPR_Audit')
    BEGIN
      CREATE DATABASE AUDIT SPECIFICATION [GDPR_Audit]
      FOR SERVER AUDIT [Audit_GDPR_Server]
      ADD (SELECT ON OBJECT::[dbo].[Customer] BY [db_owner]),
      ADD (SELECT ON OBJECT::[dbo].[MailAddresses] BY [db_owner]),
      ADD (SELECT ON OBJECT::[dbo].[ReservationAddress] BY [db_owner]),
      ADD (INSERT ON OBJECT::[dbo].[PersonnelLogInfo] BY [db_owner])
      WITH (STATE = OFF)

      ALTER DATABASE AUDIT SPECIFICATION [GDPR_Audit] WITH (STATE = ON)
    END
    ELSE
    BEGIN
      ALTER DATABASE AUDIT SPECIFICATION [GDPR_Audit] WITH (STATE = OFF)

      ALTER DATABASE AUDIT SPECIFICATION [GDPR_Audit]
      FOR SERVER AUDIT [Audit_GDPR_Server]
      ADD (SELECT ON OBJECT::[dbo].[Customer] BY [db_owner]),
      ADD (SELECT ON OBJECT::[dbo].[MailAddresses] BY [db_owner]),
      ADD (SELECT ON OBJECT::[dbo].[ReservationAddress] BY [db_owner]),
      ADD (INSERT ON OBJECT::[dbo].[PersonnelLogInfo] BY [db_owner])

      ALTER DATABASE AUDIT SPECIFICATION [GDPR_Audit] WITH (STATE = ON)
    END
  END
END