I have a database (on DB2 9.7) A in which suppose I have tables X,Y,Z...n Now I have created same tables X,Y,Z...n in database B. I want to provide same GRANTs to users in database B as it was in database A. So based on SYSCAT.TABAUTH I am trying to generate GRANT SQLs. I have written the following query for it:
db2 "select 'GRANT '||
case INSERTAUTH
WHEN 'Y' THEN 'INSERT,'
WHEN 'N' THEN ' '
END||
case ALTERAUTH
WHEN 'Y' THEN 'ALTER,'
WHEN 'N' THEN ' '
END||
case DELETEAUTH
WHEN 'Y' THEN 'DELETE,'
WHEN 'N' THEN ' '
END||
case SELECTAUTH
WHEN 'Y' THEN 'SELECT,'
WHEN 'N' THEN ' '
END||
case UPDATEAUTH
WHEN 'Y' THEN 'UPDATE,'
WHEN 'N' THEN ' '
END||
' ON '||TABSCHEMA||'.'||TABNAME||' TO '||GRANTEE from SYSCAT.TABAUTH
where INSERTAUTH='Y' OR ALTERAUTH='Y' OR DELETEAUTH='Y' OR SELECTAUTH='Y' OR UPDATEAUTH='Y'"
However, the problem I am facing is of the additional ',' at end. Suppose a user has only Insert auth, the above query will generate GRANT sql as:
GRANT INSERT, ON SCHEMA.TABLE TO GRANTEENAME
or if user has insert and select grants then:
GRANT INSERT,SELECT, ON SCHEMA.TABLE TO GRANTEENAME
How can I solve this? Please help..