views:

20

answers:

2

Can UDFs be used inside index views or does this break the SCHEMABINDING logic?

For example if in the where clause of the view I had something like this

WHERE
     UserCanSeePost(UserId,PostId)

This UDF would then do a lookup on another table to check some restrictions. I'm guessing this cant be done in indexed views?

+1  A: 

If the function is created WITH SCHEMABINDING, then you can use WITH SCHEMABINDING on a view built on top of it. I'm not 100% sure, but I think functions WITH SCHEMABINDING were introduced in SQL Server 2005...

Edit -> Nope, apparently you could use WITH SCHEMABINDING in 2000 as well.

Matt Whitfield
+2  A: 

Yes, if it's deterministic and itself has SCHEMABINDING

See Indexed views too:

...
User-defined functions referenced in the view must have been created with the SCHEMABINDING option.
...
All functions referenced by expressions in the view must be deterministic. The IsDeterministic property of the OBJECTPROPERTY function reports whether a user-defined function is deterministic. For more information, see Deterministic and Nondeterministic Functions.
...

gbn