Wednesday, June 4, 2014

find who owns what objects

How to you find out who owns what in the Netezza databases.  You need to know this because sometimes you want to drop a particular object, NPS returns error like below:

DROP USER: user "BIADM" owns objects, cannot be removed

So how do you find out which objects belongs to BIADM?  

Here's how:

SELECT OWNER, THE_OBJECT_TYPE, OBJNAME
FROM (
SELECT
                usename AS OWNER,
                (CASE   WHEN objclass = 4902 then 'database'
                        WHEN objclass = 4903 then 'group'
                        WHEN objclass = 4904 then 'user'       END ) AS the_object_type,
                objname
        FROM
                _t_object,
                _t_user
        WHERE   objowner = usesysid
         AND    objclass IN (4902, 4903, 4904)
         AND    objdb = 0
         AND    objowner <> (SELECT usesysid FROM _t_user WHERE UPPER(usename) = 'ADMIN')
UNION ALL
        SELECT
                _t_user.usename AS OWNER,
                lower(the_class.objname) AS the_object_type,
                 the_object.objname || ' in database ' ||  the_database.objname AS objname
        FROM
                _t_object as the_object
                        LEFT OUTER JOIN _t_object the_class ON
                                (the_object.objclass = the_class.objid),
                _t_object the_database,
                _t_user
        WHERE   the_object.objowner = _t_user.usesysid
         AND    the_object.objclass NOT IN (4902, 4903, 4904)
         AND    the_object.objdb = the_database.objid
         AND    the_object.objowner <> (SELECT usesysid FROM _t_user WHERE UPPER(usename) = 'ADMIN')
) tab
WHERE OWNER='BIADM'
ORDER BY 1,2,3;

No comments:

Post a Comment