2016-04-08 14 views
-1

Ich habe Abfrage SQL Server-Datenbank: -Konvertieren von SQL Server Query zu Oracle Abfrage

  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; 

Wie kann ich diese SQL Server-Abfrage in eine Oracle-Abfrage-Format konvertieren?

+1

Haben Sie es versucht? Welches Problem hast du? – Aleksej

+0

@Aleksej: - Ya ich habe es versucht. Problem ist, dass Zeilen mehrmals kommen. – Sanjiv

+0

kommen sie mehrmals in SQL Server? Sind deine Daten gleich? – Matt

Antwort

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; 
+0

: - Danke .. es funktioniert, was ich ausgenommen bin .. – Sanjiv

0

Verwenden Sie "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; 

oder bei Verwendung von LEFT OUTER JOIN ‚s IS NOT NULL auf verknüpfte Werte verwenden.

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;