将SQL Server查询转换为Oracle查询

时间:2016-04-08 07:23:39

标签: oracle oracle11g oracle-sqldeveloper oracle-xe

我有SQL Server数据库查询: -

          SELECT DISTINCT
                 info.COLUMN_NAME AS NAME,
                 info.TABLE_SCHEMA AS TABLESCHEMA,
                 info.TABLE_NAME AS TABLENAME,
                 info.ORDINAL_POSITION AS POSITION, 
                 info.IS_NULLABLE AS ISNULLABLE,
                 info.DATA_TYPE AS DATATYPE,
                 tc.CONSTRAINT_TYPE AS CONSTRAINTTYPE,
                 kcufk.TABLE_SCHEMA AS REFRENCESCHEMA, 
                 kcufk.TABLE_NAME AS REFRENCETABLE,
                 kcufk.COLUMN_NAME AS REFRENCECOLUMN
            FROM INFORMATION_SCHEMA.COLUMNS info
 LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
              ON kcu.COLUMN_NAME = info.COLUMN_NAME
             AND kcu.TABLE_NAME = info.TABLE_NAME
 LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc 
              ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
             AND tc.TABLE_NAME = kcu.TABLE_NAME
 LEFT OUTER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rk 
              ON rk.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
 LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcufk 
              ON kcufk.CONSTRAINT_NAME = rk.UNIQUE_CONSTRAINT_NAME
           WHERE info.TABLE_NAME = 'Category'
             AND info.TABLE_SCHEMA = 'core' 
        ORDER BY info.ORDINAL_POSITION ASC;

如何将此SQL Server查询转换为Oracle查询?

2 个答案:

答案 0 :(得分:1)

SELECT tc.column_name,
       tc.owner,
       tc.table_name,
       tc.column_id,
       tc.nullable,
       tc.data_type,
       c.constraint_type,
       c.r_owner AS reference_owner,
       rcc.table_name AS reference_table,
       rcc.column_name AS reference_column_name
FROM   ALL_TAB_COLUMNS tc
       LEFT OUTER JOIN ALL_CONS_COLUMNS cc
       ON ( tc.owner = cc.owner AND tc.table_name = cc.table_name AND tc.column_name = cc.COLUMN_NAME )
       LEFT OUTER JOIN ALL_CONSTRAINTS c
       ON ( tc.owner = c.owner AND tc.table_name = c.table_name AND c.constraint_name = cc.constraint_name )
       LEFT OUTER JOIN ALL_CONS_COLUMNS rcc
       ON ( c.r_owner = rcc.owner AND c.r_constraint_name = rcc.constraint_name )
WHERE  tc.table_name = 'CATEGORY'
AND    tc.OWNER      = 'CORE'
ORDER BY tc.column_id;

答案 1 :(得分:0)

尝试使用'INNER JOIN`。

SELECT DISTINCT
info.COLUMN_NAME AS NAME,
info.TABLE_SCHEMA AS TABLESCHEMA,
info.TABLE_NAME AS TABLENAME,
info.ORDINAL_POSITION AS POSITION, 
info.IS_NULLABLE AS ISNULLABLE,
info.DATA_TYPE AS DATATYPE,
tc.CONSTRAINT_TYPE AS CONSTRAINTTYPE,
kcufk.TABLE_SCHEMA AS REFRENCESCHEMA, 
kcufk.TABLE_NAME AS REFRENCETABLE,
kcufk.COLUMN_NAME AS REFRENCECOLUMN
FROM INFORMATION_SCHEMA.COLUMNS info
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON kcu.COLUMN_NAME = info.COLUMN_NAME AND kcu.TABLE_NAME = info.TABLE_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND tc.TABLE_NAME = kcu.TABLE_NAME
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rk ON rk.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcufk ON kcufk.CONSTRAINT_NAME = rk.UNIQUE_CONSTRAINT_NAME
WHERE info.TABLE_NAME ='Category'
AND info.TABLE_SCHEMA ='core' 
ORDER BY info.ORDINAL_POSITION ASC;

或者,如果使用LEFT OUTER JOIN对链接值使用IS NOT NULL

SELECT DISTINCT
info.COLUMN_NAME AS NAME,
info.TABLE_SCHEMA AS TABLESCHEMA,
info.TABLE_NAME AS TABLENAME,
info.ORDINAL_POSITION AS POSITION, 
info.IS_NULLABLE AS ISNULLABLE,
info.DATA_TYPE AS DATATYPE,
tc.CONSTRAINT_TYPE AS CONSTRAINTTYPE,
kcufk.TABLE_SCHEMA AS REFRENCESCHEMA, 
kcufk.TABLE_NAME AS REFRENCETABLE,
kcufk.COLUMN_NAME AS REFRENCECOLUMN
FROM INFORMATION_SCHEMA.COLUMNS info
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON kcu.COLUMN_NAME = info.COLUMN_NAME AND kcu.TABLE_NAME = info.TABLE_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND tc.TABLE_NAME = kcu.TABLE_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rk ON rk.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcufk ON kcufk.CONSTRAINT_NAME = rk.UNIQUE_CONSTRAINT_NAME
WHERE info.TABLE_NAME ='Category'
AND info.TABLE_SCHEMA ='core'
AND info.COLUMN_NAME IS NOT NULL
AND info.TABLE_NAME IS NOT NULL
AND kcu.CONSTRAINT_NAME IS NOT NULL
AND kcu.TABLE_NAME IS NOT NULL
AND rk.UNIQUE_CONSTRAINT_NAME IS NOT NULL
ORDER BY info.ORDINAL_POSITION ASC;