0

我有一个数据库(在 DB2 9.7 上)A 假设我有表 X、Y、Z...n 现在我在数据库 B 中创建了相同的表 X、Y、Z...n。我想提供相同的 GRANT对于数据库 B 中的用户,就像在数据库 A 中一样。因此,基于 SYSCAT.TABAUTH,我正在尝试生成 GRANT SQL。我为此编写了以下查询:

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'"

但是,我面临的问题是末尾的附加“,”。假设用户只有 Insert auth,上述查询将生成 GRANT sql 为:

GRANT INSERT, ON SCHEMA.TABLE TO GRANTEENAME
or if user has insert and select grants then:
GRANT INSERT,SELECT, ON SCHEMA.TABLE TO GRANTEENAME

我该如何解决这个问题?请帮忙..

4

2 回答 2

0

您始终可以将动态部分推送到子查询中,然后使用 和 的组合LENGTH()SUBSTR()修剪多余的逗号。这是我修改了一下的SQL:

SELECT
'GRANT' ||
SUBSTR(T.AUTHSTRING, 1 LENGTH(T.AUTHSTRING) -1) 
|| T.TABNAME
FROM(
select
    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
    AS AUTHSTRING,

    ' ON ' || RTRIM(TABSCHEMA) || '.' || RTRIM(TABNAME)||' TO ' || RTRIM(GRANTEE) AS TABNAME
from SYSCAT.TABAUTH
where INSERTAUTH='Y'
   OR  ALTERAUTH='Y'
   OR DELETEAUTH='Y'
   OR SELECTAUTH='Y'
   OR UPDATEAUTH='Y'
) AS T

我对此进行了测试,它适用于 LUW 9.7 和 z/OS 9.1。

于 2013-01-17T22:02:28.773 回答
0

这是我最终制作的,并且效果很好:

db2 "SELECT
'GRANT ' ||
SUBSTR(T.AUTHSTRING, 1 ,LENGTH(T.AUTHSTRING) - 1) 
|| T.TABNAME
FROM(
select
    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
    AS AUTHSTRING,
    ' ON ' ||TRIM(TABSCHEMA)||'.'||TRIM(TABNAME)||' TO ' ||GRANTEE AS TABNAME
from SYSCAT.TABAUTH
where INSERTAUTH='Y'
   OR  ALTERAUTH='Y'
   OR DELETEAUTH='Y'
   OR SELECTAUTH='Y'
   OR UPDATEAUTH='Y'
) AS T"
于 2013-01-18T07:34:46.543 回答