'How to find out the IsSchemaBound property of a view of another database

I need to find out whether a specific view is created by using SCHEMABINDING or not in another database (In a JOIN statement). Currently I'm using OBJECTPROPERTY(SV.object_id, 'IsSchemaBound'). SV.object_id gets the correct object id of the view of another database. But the problem is, the built in function OBJECTPROPERTY() is only executable in current database context as per this ms doc.

Is there a way to execute OBJECTPROPERTY() function in another database context? Or is there any way to get the IsSchemaBound property of another database.



Solution 1:[1]

You can use dynamic sql in the context of the target database, for example:

exec databasename.sys.sp_executesql N'select ObjectProperty(<objectId>, ''IsSchemaBound'')';

Solution 2:[2]

Thanks to @Charlieface for view schemabinding check.

you can create a common table expression and then use the same in your join condition, as you have mentioned in your comments.

;with cte_schemaboundViews(Name) AS
(select v.name,
  from YourDB.sys.views v
join YourDB.sys.sql_modules m on m.object_id = v.object_id
WHERE is_schema_bound = 1
)
SELECT * -- Your columns
FROM YourSchemaName.YourTableName AS IV  -- yourTableName
JOIN cte_schemaboundViews AS SV
ON CONVERT(VARCHAR(MAX), IV.TABLE_NAME) = CONVERT(VARCHAR(MAX), SV.NAME)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stu
Solution 2 Venkataraman R