On 3/8/2016 5:54 AM,
chr6...@gmail.com wrote:
> Is there a way to delete a list of users from an as400 at once?
I've been known to do something like this.
create function do_cmd (command varchar(10000)) returns int
external name 'QSYS/QC2SYS(sytem)'
no sql
parameter style general with nulls
Then:
select authorization_name,
do_cmd('dltusrprf ' || authorization_name)
from qsys2.user_info
where authorization_name in
('TRUMP','CARSON','CRUZ','RUBIO','BUSH','CLINTON','SANDERS','TRUMP')
[put in Trump twice just to be sure I really got the bastard ;-)]
You could add some of the other parameters of the DLTUSRPRF command if
you wished. The integer result of the scalar function will come back as
a 0 if the command completes successfully, or a 1 if it fails.
Technically, this isn't deleting them all at once - the command is
executed once for each row meeting the selection criteria. However,
it's "all at once" in the sense that *you* execute a single statement.
You could also write a simple stored procedure to do this, and you could
make it operate on some real world criteria, such as a profile that was
created before some earlier date and that has not signed on in a long
time, or never.
create procedure delete_inactive_profiles () language sql
begin
declare profile char(10);
declare sqlstate char(5);
declare go_on char(1);
declare delete_profile_cmd char(100);
declare inactive_profiles cursor for
select authorization_name
from qsys2.user_info
where date(creation_timestamp) <= current_date - 2 years
and coalesce(date(previous_signon), '0001-01-01')
<= current_date - 1 years;
declare continue handler for sqlexception
set go_on = 'Y';
open inactive_profiles;
if sqlstate = '00000' then
fetch inactive_profiles into profile;
while sqlstate = '00000' do
set delete_profile_cmd = 'dltusrprf ' || profile;
call qsys2.qcmdexc (delete_profile_cmd);
fetch inactive_profiles into profile;
end while;
close inactive_profiles;
end if;
end;
You may want to add some other error handling and possibly insert a row
for each deleted profile into a log table, as well as add some of the
other parameters for the DLTUSRPRF command to handle objects owned by
the profile, etc.