Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 T-SQL standards

Author  Topic 

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-10-29 : 19:34:20
Hi,

I had been looking for a good web link that has T-SQL best practices. I searched msdn and it gives a whole bunch of white papers related to sql server best practices. I was wondering if there is something that the members of sqlteam.com uses.

Thanks

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-10-30 : 07:18:27
which best practices are you looking for?
BOL provides you with great documentation and examples on T-SQL.
Go to Top of Page

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-11-01 : 12:24:35
I am looking for some sql coding standards.

For example using WITH NOLOCK in the joins is better because no shared locks are issued against the table that would prohibit other transactions from modifying data in the table.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-11-01 : 13:21:34
Using NOLOCK (or any query hint) is certainly NOT a best practice. They should only be used when all other tuning options have been exhausted, i.e. as a last resort.
Go to Top of Page

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-11-01 : 14:18:14
I thought that NOLOCK was a good practice. Thank you robvolk for clarifying.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-11-01 : 14:57:46
"I thought that NOLOCK was a good practice."

No. Just to be clear: it is terrible practice.

You will get some rows twice, some rows not at all, some rows will show data for rows that do exist in the database, but which were not committed, and thus the data you see will be data that was never actually in the database.

These "side effects" happen very INfrequently. They are hard / impossible to reproduce, so when users say things like "I saw XXX" it is very unlikely you will be able to reproduce it. If management use reports containing NOLOCK there is a serious business risk that they will take decisions based on data that was incorrect / inaccurate / inappropriate!!

Don't use NOLOCK ever. If you think you need to use NOLOCK then ask me first
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-01 : 15:46:34
Instead of NOLOCK, use READ_COMMITTED_SNAPSHOT. But first be sure you understand that isolation level. You won't get dirty reads like you would with NOLOCK, but you can get data that is no longer accurate.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-01 : 18:06:54
Before you use or don't use a particular isolation level, you should understand the implications of that choice. I dislike blanket "Always" or "Never" do X, Y or Z statements. So, I'll leave it up to you to educate yourself.

As to standards or best practices... That's harder to nail down because most of the best practices depend on your situation. Some are more universal, like it's not a good idea to name objects the same as SQL reserved words, etc.. Unfortunately, there are no universal coding standards; every shop has their own (if they even have any). So, if you come up with some, just be consistent in your use of that standard.

Here is link to the 11179 ISO standards:
http://metadata-stds.org/11179/

And here is a link to the Wikia for more information on said standard:
http://en.wikipedia.org/wiki/ISO/IEC_11179
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-01 : 18:32:34
Just to be clear, I wasn't saying it's a best practice to use READ_COMMITTED_SNAPSHOT. I was saying if you are already using NOLOCK without fully understanding it, then you'd probably be better off with READ_COMMITTED_SNAPSHOT.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-11-01 : 20:07:31
Thanks everybody for clearing up my queries on NOLOCK. There are a lot of scripts that i had used NOLOCKS. Probably i need to change them.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-01 : 20:16:01
I'm surprised Opey's head didn't explode

Just say that it's bad...

Do you know how many SQL "Experts" out there use NOLOCK?

Ignorance reign supreme

If I write really bad code...NOLOCK should fix that

Or perceived to fix it

Oh and pvccaz, if you remove NOLOCK and ALL of your report counts change...you WILL get blamed




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-11-02 : 03:30:56
"Just say that it's bad..."

Its not that it is bad, it is that it is really bad. It is made bad because the people using it don't know the consequences. It fixes the problem at hand (blocking, say) and seems the panacea solution. I do understand why it is widely used, I am sure that 99% of people using it don't realise the potential consequences, and why should they? So its only when they hear from someone else that there are issues that they will become aware. I like to speak up on this particular issue because I suspect in most/many cases there is a very real possibility that it is doing harm - but only very VERY infrequently, but quite possibly with disastrous consequences.

Sorry O/P didn't mean my reply to sound harsh. But, yes, you do need to change your code and as Tara said: most likely READ_COMMITTED_SNAPSHOT is what you need - plus, you just turn that on once for the database and get the benefit (of what I think you are using NOLOCK for) on every query in your application - but you do have to go take out the NOLOCK's, and there may be side effects of doing that and, as Brett said!, you will get the blame for those! (Maybe some queries WILL now block - where Write is blocking Write, for example, but NOLOCK was not the solution for them and you'll expose a real problem that you do need to fix properly.)

Hope it goes well
Go to Top of Page
   

- Advertisement -