Thread: Problem with DROP ROLE

Problem with DROP ROLE

From
Brice André
Date:
Hello everyone,<br /><br />I would want to implement an SQL query where I would be able to suppress all information
froma registered user. I am currenlty able to suppress everything except the user role. The name of the role is present
ina table and so, I would want to perform something like this : <br /> DROP ROLE (SELECT ...)<br />but this is not
consideredas valid, as DROP ROLE is expecting a name and not a text field. So, I tried the following, but with no
success: <br />DROP ROLE CAST((SELECT...) AS name)<br /><br />So, does someone knows how to handle this problem ?<br
/><br/>Regards,<br />Brice<br /> 

Re: Problem with DROP ROLE

From
Guillaume Lelarge
Date:
On Wed, 2011-10-19 at 12:11 +0200, Brice André wrote:
> Hello everyone,
> 
> I would want to implement an SQL query where I would be able to suppress all
> information from a registered user. I am currenlty able to suppress
> everything except the user role. The name of the role is present in a table
> and so, I would want to perform something like this :
> DROP ROLE (SELECT ...)
> but this is not considered as valid, as DROP ROLE is expecting a name and
> not a text field. So, I tried the following, but with no success :
> DROP ROLE CAST((SELECT...) AS name)
> 
> So, does someone knows how to handle this problem ?
> 

You can't do it with only one query. You need to use two queries: the
SELECT, and then the DROP.


-- 
Guillaume http://blog.guillaume.lelarge.info http://www.dalibo.com



Re: Problem with DROP ROLE

From
Brice André
Date:
Thanks very much. I replaced my statement by the following plpgsql code :

statement := 'DROP ROLE "'||(SELECT ...)||'";';
execute(statement);

and it works like a charm.

Regards,
Brice

2011/10/19 Andreas Kretschmer <andreas@a-kretschmer.de>
You can't do that in THAT way, but you can use dynamic SQL:
 
test=# select * from drop_role ;
   t
--------
 foobar
(1 row)

test=*# do $$ declare r text; begin for r in select t from drop_role loop
execute 'drop role ' || quote_ident(r) || ';'; end loop; end; $$language
plpgsql;
 
DO 

 
 

"Brice André" <brice@famille-andre.be> hat am 19. Oktober 2011 um 12:11
geschrieben:


> Hello everyone,
>
> I would want to implement an SQL query where I would be able to suppress all
> information from a registered user. I am currenlty able to suppress everything
> except the user role. The name of the role is present in a table and so, I
> would want to perform something like this :
> DROP ROLE (SELECT ...)
> but this is not considered as valid, as DROP ROLE is expecting a name and not
> a text field. So, I tried the following, but with no success :
> DROP ROLE CAST((SELECT...) AS name)
>
> So, does someone knows how to handle this problem ?
>
> Regards,
> Brice
>

Re: Problem with DROP ROLE

From
Andreas Kretschmer
Date:
You can't do that in THAT way, but you can use dynamic SQL:
 
test=# select * from drop_role ;
   t
--------
 foobar
(1 row)

test=*# do $$ declare r text; begin for r in select t from drop_role loop
execute 'drop role ' || quote_ident(r) || ';'; end loop; end; $$language
plpgsql;
 
DO 

 
 

"Brice André" <brice@famille-andre.be> hat am 19. Oktober 2011 um 12:11
geschrieben:


> Hello everyone,
>
> I would want to implement an SQL query where I would be able to suppress all
> information from a registered user. I am currenlty able to suppress everything
> except the user role. The name of the role is present in a table and so, I
> would want to perform something like this :
> DROP ROLE (SELECT ...)
> but this is not considered as valid, as DROP ROLE is expecting a name and not
> a text field. So, I tried the following, but with no success :
> DROP ROLE CAST((SELECT...) AS name)
>
> So, does someone knows how to handle this problem ?
>
> Regards,
> Brice
>