0

Question is in the title :). I need to know if it is possible or not.

Here is how to do it for table

SELECT
    OBJECT_NAME(f.parent_object_id) as tbl, 
    OBJECT_NAME(fc.referenced_object_id) as refTable, 
    COL_NAME(f.parent_object_id, fc.parent_column_id) as foreignKeyColumnName 
FROM sys.foreign_keys f 
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
WHERE f.parent_object_id = OBJECT_ID('dbo.TableName')
0

1 Answer 1

1

Views are typically just "pre-stored" SELECT statements based on tables.

Views themselves don't have any physical representation in your database, and therefore, you don't have foreign keys on views - you have them on the underlying base tables

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Marc, maybe it is possible to get it from assoc tables through view.
@Danil: sure - you just need to know what tables the view relies on and then use your code you've posted..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.