Is there anyway to execute grant and revoke statements to a user for an object based on a condition?

data_explorer
New Contributor II

SELECT if((select count(*) from information_schema.table_privileges where grantee = 'samo@test.com' and table_schema='demo_schema' and table_catalog='demo_catalog')==1, (select count(*) from demo_catalog.demo_schema.demo_table), (select count(*) from demo_catalog.demo_schema.demo_table2));

The above query is working and I'm getting the result.

But what I'm trying to achieve is that,

if

select count(*) from information_schema.table_privileges where grantee = 'samo@test.com' and table_schema='demo_schema' and table_catalog='demo_catalog')==1

If the above condition is true , then I want to run the below subqueries altogether:

revoke select on table demo_catalog.demo_schema.demo_table from `samo@test.com`;revoke usage on catalog demo_catalog from `samo@test.com`;revoke usage on schema demo_catalog.demo_schema from `samo@test.com`;

Basically, for a user, if there is access to only one table, then I want to revoke the access from table, schema and catalog.

Is it possible to achieve this, if yes, How to achieve it?