64

I have a database with hundreds of awkwardly named tables in it (CG001T, GH066L, etc), and I have views on every one with its "friendly" name (the view "CUSTOMERS" is "SELECT * FROM GG120T", for example). I want to add "WITH SCHEMABINDING" to my views so that I can have some of the advantages associated with it, like being able to index the view, since a handful of views have computed columns that are expensive to compute on the fly.

Are there downsides to SCHEMABINDING these views? I've found some articles that vaguely allude to the downsides, but never go into them in detail. I know that once a view is schemabound, you can't alter anything that would impact the view (for example, a column datatype or collation) without first dropping the view, so that's one, but aside from that? It seems that the ability to index the view itself would far outweigh the downside of planning your schema modifications more carefully.

1
  • 3
    You don't have to drop the view, but you do have to alter the view with the schemabinding removed.
    – JeffO
    Nov 3, 2009 at 3:28

10 Answers 10

50

You wont be able to alter/drop the table, unless you drop the view first.

2
  • 4
    This is a big problem in my view especially if you want to modify the database structure without the original DDL statements handy. In these cases you have to attempt to generate complete DDL statements for the Views/Functions with SCHEMABINDING, drop them and then recreate them. Quite a big task to undergo just to change the size of a column.
    – jpierson
    Aug 19, 2010 at 15:48
  • 22
    You don't need to drop the view per se, just ALTER it so that it is not schema-bound, and ALTER it back after.
    – Paul
    Nov 28, 2011 at 12:02
35

None at all. It's safer. we use it everywhere.

8
  • 9
    If there are no downsides, and it's safer (which was my initial impression), then why wouldn't people use it? It seems like protecting your views from accidental breakage would be a priority, or like it should be the other way around - WITH is the default, and you have to declare your views WITHOUT if you want that behavior.
    – SqlRyan
    Nov 2, 2009 at 14:07
  • 2
    laziness, too much discipline (eg qualified columns etc)
    – gbn
    Nov 2, 2009 at 14:11
  • 1
    Is there a way to make this the default option, or is it always something that needs to be done consciously?
    – SqlRyan
    Nov 2, 2009 at 14:19
  • 3
    I had that happen about a month ago - I changed an underlying table and the view was returned completely whacked out results. It turned out the view used SELECT * FROM and I had to go refresh the view before it realized the underlying schema had changed :)
    – SqlRyan
    Dec 19, 2012 at 21:12
  • 5
    @Triynko: there are good reasons for this. Any changes to the base tables that require the whole indexed view to be affected are disallowed. A SUM is easy to calculate only for the changed rows for example. Also, I don't believe that blind table recreation is a good idea: advanced SQL and this kind of development don't mix
    – gbn
    Mar 6, 2013 at 7:38
35

Oh, there are DEFINITELY DOWNSIDES to using SCHEMABINDING - these come from fact the SCHEMABINDING, especially when coupled with COMPUTED columns "LOCKS" THE RELATIONSHIPS and makes some "trivial changes" darn near impossible.

  1. Create a table.
  2. Create a SCHEMABOUND UDF.
  3. Create a COMPUTED PERSISTED column that references the UDF.
  4. Add an INDEX over said column.
  5. Try to update the UDF.

Good luck with that one!

  1. The UDF can't be dropped or altered because it is SCHEMABOUND.
  2. The COLUMN can't be dropped because it is used in an INDEX.
  3. The COLUMN can't be altered because it is COMPUTED.

Well, frak. Really..!?! My day just became a PITA. (Now, tools like ApexSQL Diff can handle this when provided with a modified schema, but the issue is here that I can't even modify the schema to begin with!)

I'm not against SCHEMABINDING, mind (and it's needed for a UDF in this case), but I'm against there not being a way (that I can find) to "temporarily disable" the SCHEMABINDING.

4
  • 1
    You mean you it's possible to create some circular SCHEMABOUND references? Is there any way to get out of that other than just drop / recreate the database without the SCHEMABINDING OPTION? (dropping the index in your case can unblock you?) Oct 23, 2014 at 13:27
  • 3
    "1. The UDF can't be dropped or altered because it is SCHEMABOUND." No, that's the opposite of what schema binding does. "3. The COLUMN can't be altered because it is COMPUTED." Huh? What do you mean? Feb 9, 2018 at 20:30
  • "1. The UDF can't be dropped or altered because it is SCHEMABOUND." That has nothing to do with SCHEMABINDING, but rather your use of the UDF in the computed column. "3. The COLUMN can't be altered because it is COMPUTED." Erm, yeah? That has nothing to do with SCHEMABINDING, but with the way (MS)SQL works.
    – Tom Lint
    Dec 11, 2019 at 16:09
  • 1
    This answer says that if I create a long chain of schema objects, each of which depends on the previous object, I may have to temporarily alter or drop (rather than just disable) some of them in reverse order of adding them, so that I can do a schema change at the bottom of the tower. It's the same thing the OP meant by "planning [one's] schema modifications more carefully", of course. But the UDF example is still enlightening. Feb 1, 2021 at 21:29
7

One downside is that if you schemabind a view, it can only reference other schemabound views.

I know this because I tried to schemabind a view and was met with an error message telling me it could not be schemabound because one of the other views it references is not also schemabound.

The only consequence of this is that if you suddenly want to update a schemabound view to reference some new or existing view, you might have to schemabind that new or existing view as well. In that case, you won't be able to update the view, and you better hope your database developers know how to work with schemabound views.

4

If these tables are from a third-party app (they're notorious for trying hide their tables), you cause and upgrade to fail if it attempts to alter any of these tables.

You just have to alter the views without the schemabinding before the update/upgrade and then put them back. Like others have mentioned. Just takes some planning, discipline, etc.

4
  • 1
    I suppose that's true, and much less invasive than dropping the view for the duration of your DDL. I recently had to change the collation on some columns, and just doing an ALTER/Change collation/ALTER would have been much easier than dropping the view and breaking the application while I was working.
    – SqlRyan
    Nov 3, 2009 at 16:30
  • 1
    Unfortunately simply removing SCHEMABINDING via an ALTER statement won't work for indexed Views so in these cases I believe the only solution is still to drop and recreate the view.
    – jpierson
    Aug 19, 2010 at 15:51
  • I just tested doing an ALTER VIEW on my indexed view to see what would happen. I was expecting to see some type of error (typical of SQL Server in a good way) but instead it just deleted my indexes. So beware of using ALTER on a view just to change whether it is schema bound or not without knowing whether it has indexes first.
    – jpierson
    Aug 19, 2010 at 16:01
  • 2
    If you remove schemabinding (you have to use alter which completely rebuilds the view) you can't have an index anyway, so yes, if you add schemabinding back you'll have to recreate the index.
    – JeffO
    Aug 19, 2010 at 16:40
2

Another downside is that you need to use schema qualified names for everything: You'll get a load of error messages like this:

Cannot schema bind view 'view' because name 'table' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.

Also to 'switch off' schemabinding you do alter view which requires you to redefine the view's select statement. I think the only thing you dont have to redefine is any grants. This puts me off a lot as overwriting the view seems like an inherently unsafe operation.

Its a bit like the way adding not null constraints forces you to overwrite the column's data type - nasty!

You'll also have to redefine any other views or procedures that depend on the schema bound object you want to change... this means you may have to redefine (and possibly break) a large cascade of functions and views just to add (eg) a not null constraint to one column.

Personally I think this doesnt really represent a solution and its better to have a decent process whereby any database changes are applied automatically so it isnt a nightmare to change the database. That way you can have all your views + functions dropped and recreated from scratch (they get checked on creation anyway) as part of the process when you apply changes to tables.

1

this seems like a downside to me (#'s are mine):

Cannot create index on view "###.dbo.###" because it uses a LEFT, RIGHT, or FULL OUTER join, and no OUTER joins are allowed in indexed views. Consider using an INNER join instead.

I kinda need my LEFT joins. This SO question is relevant.

1

When using tSQLt Unit Test Framework you will come across issues and will need workarounds when using FakeTable method, which won't allow you to fake a table that is linked to a view with schemabinding.

1

The negatives mentioned hardly outweigh this best practice since SQL Svr 2005. It avoids the dreaded table spooling. A major negative for me is that schema bound sprocs, funcs, views, can't include "foreign" databases such as the master db, so you can throw all the great realtime system stuff in the trash unless, for example, your production core database sits inside master. For me, I can't deal with life without the sys stuff. Of course not all processing requires spool-free performance and fast and slow results can be combined simultaneously in higher data class layers.

0

If your tool (ssms etc.) does not handle schema change failures on the base object well / elegantly you could cause yourself some real chaos. That's what I'm sitting with now, and I do realise that this is a fringe case

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.