Query returning unwanted result

0

In my sql statement:

SELECT DISTINCT C.TABLE_NAME,C.CONSTRAINT_NAME,C.COLUMN_NAME 
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS T 
ON T.TABLE_NAME = C.TABLE_NAME
WHERE C.TABLE_NAME = 'VALOR_CAMPO_DATA' 
AND C.COLUMN_NAME IN ('ID','REV') 
AND T.CONSTRAINT_TYPE = 'FOREIGN KEY';

I would like to return only FK's, but PK's are also coming. I have specified in where T.CONSTRAINT_TYPE = 'FOREIGN KEY' , but it is not working, if I only execute INFORMATION_SCHEMA.TABLE_CONSTRAINTS it works,

Does anyone know how I solve this problem?

    
asked by anonymous 04.11.2016 / 18:33

1 answer

4

You have to add the following in the inner join:

AND T.CONSTRAINT_NAME = C.CONSTRAINT_NAME

Otherwise it will duplicate information because TABLE_NAME is for both PK and FK, so it brings both

    
11.11.2016 / 01:56