0

I am looking for a script that I can run on my server to create views with no locking for all of my tables within my database. Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Can you explain why? – Mike Jun 13 '16 at 18:57
  • 3
    Set [Bad Habits to kick - putting NOLOCK everywhere](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) - it is ***NOT RECOMMENDED*** to use this everywhere - quite the contrary! – marc_s Jun 13 '16 at 19:34

1 Answers1

1

Although I agree that this is a bad idea, there still may be something to be gained/learned by showing some examples. But -- yes -- this is potentially a really bad idea, but in some world this may make sense.

If this is a one-time thing, you can do something like this, which will simply create the DDL for the views with (nolock). You simply need to copy-paste and execute. If this needs to be dynamically generated, then a bit more needs to be harnessed.

One more caveat about the below -- it uses select * in the view without schema binding. Be extraordinarily cautious with these types of views as they do not automatically update if the underlying table structure changes. It's advisable and good practice to fully qualify columns in views unless you have other safeguards.

select '
    create view ' + name + 'MayBeABadIdea as select * from ' + name + ' (nolock);
    go'
from sys.objects
where type = 'U'
order by name;
square_particle
  • 526
  • 2
  • 7
  • Thanks. I completely agree that this is not the best idea, however; I need to be able to query my CTI database in real-time without locking the tables. Unfortunately, healthcare requires some live lookups of data but I don't want to hinder my call routing and delivery either. – Matthew Martel Jun 14 '16 at 19:19