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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Why shouldn't you use Reserved Keywords

Author  Topic 

TallOne
Starting Member

49 Posts

Posted - 2011-07-14 : 11:36:09
Hi Everyone!

I've always been in the habit of placing reserved keywords in brakets when they are used as an object. For a field named Description, I would normally place in brackets [Description]. But my question is why? I've worked at shops that do not do this and I'm not aware of any problems that ever arose from it. Now I'm faced with enforcing this policy at a new shop and I would like to provide the team a good explanation of why. Can anyone clear this up for me?

Thanks,
Jerry

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-14 : 11:41:36
It is because most of them will throw error

select 1 as add

the above throws error whereas the following is not

select 1 as [add]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-14 : 11:59:49
Well, tell me if you think this kind of table is sensible:
CREATE TABLE [FROM]([SELECT] CHAR(1), [WHERE] VARCHAR(2), [=] VARCHAR(2))
INSERT [FROM] VALUES('A','B','C')
INSERT [FROM] VALUES('A','B','B')
INSERT [FROM] VALUES('A','=','B')
That's the kind of headache I don't need as a DBA, and it's harder to argue against if I let people get away with things like GROUP, USER, LOG, TABLE, etc. You also encounter problems depending on your QUOTED_IDENTIFIER setting:
SET QUOTED_IDENTIFIER ON
SELECT * FROM [FROM] WHERE [WHERE] = [=]
SELECT * FROM [FROM] WHERE [WHERE] = '='
SELECT * FROM [FROM] WHERE [WHERE] = "="
SET QUOTED_IDENTIFIER OFF
SELECT * FROM [FROM] WHERE [WHERE] = [=] --same as above
SELECT * FROM [FROM] WHERE [WHERE] = '=' --same as above
SELECT * FROM [FROM] WHERE [WHERE] = "=" --different row
SET QUOTED_IDENTIFIER ON

DROP TABLE [FROM]
If you're positive you can enforce the square brackets then you shouldn't have any problem. But if your developers are not familiar with QUOTED_IDENTIFIER and they still enclose strings in double quotes, you can get the wrong data just because of a system setting.

Now, if you're a DBA, and you want to use this technique AGAINST your developers:

http://www.sqlsaturday.com/viewsession.aspx?sat=77&sessionid=4262

That's perfectly fine.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-07-14 : 13:02:02
quote:
Originally posted by robvolk

Now, if you're a DBA, and you want to use this technique AGAINST your developers:

http://www.sqlsaturday.com/viewsession.aspx?sat=77&sessionid=4262

That's perfectly fine.



You're an evil man lol. I especially like the duplicates script. Sneaky.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-14 : 13:20:37
Rob, that is a great idea for a presentation! I wish I could've sat in your session.

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 - 2011-07-14 : 13:24:19
quote:
Originally posted by TallOne

Hi Everyone!

I've always been in the habit of placing reserved keywords in brakets when they are used as an object. For a field named Description, I would normally place in brackets [Description]. But my question is why? I've worked at shops that do not do this and I'm not aware of any problems that ever arose from it. Now I'm faced with enforcing this policy at a new shop and I would like to provide the team a good explanation of why. Can anyone clear this up for me?

Since you are asking a general question about reserved words,I don't want to be captain obvious. But, Description is not a reserved word in MS SQL Server. And the, perhaps not so obvious, answer is to not allow reserved words to be used as an object name. That's how we handle that issue. As for the square-brackets, I personally hate them, but if you use Visual Studio you seem to pretty much be stuck with them.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-14 : 13:37:55
quote:
Originally posted by tkizer

Rob, that is a great idea for a presentation! I wish I could've sat in your session.

Tara Kizer
I'm presenting it at SQL Saturday #81 in Birmingham, Alabama and #89 in Atlanta:

http://www.sqlsaturday.com/81/eventhome.aspx
http://www.sqlsaturday.com/89/eventhome.aspx

I also submitted it for #85 in Orlando:

http://www.sqlsaturday.com/85/schedule.aspx
Go to Top of Page

TallOne
Starting Member

49 Posts

Posted - 2011-07-15 : 12:34:49
Thanks for the replies everyone!

@Lamprey - You are correct, I should have used a better word example...or maybe I should have included the phrase ReservedKeywords/Propeties Out of habit, I always include words like that in brackets as well. The word "Description" appears in blue in query analyzer just like a reserved word and I'm assuming it must be a property of some library. Do you guys do the same thing and are there any reprecussions if not?

@robvolk Nice bag of tricks! :) I was lucky to catch the Charotte SQL Saturday a few months back and appreciate your time and effort.

Thanks,
Jerry


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-15 : 12:47:30
Anytime it turns blue for objects, I put them in square brackets. I can't stand to see the blue and also want to play it safe. We unfortunately have a table called load and you have to put that one in square brackets.

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 - 2011-07-15 : 12:56:32
As I mentioned, I just use the Reserved Keyword list (http://msdn.microsoft.com/en-us/library/ms189822.aspx) and if it appears in that list it cannot be used for an object name. That way there is no need to ever use delimited identifiers.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-15 : 13:19:51
That's what I do as well. Doesn't mean my developers are going to follow it, and by the time they get me involved, it's too late. They've written too much code around it and can't change things due to critical timelines.

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

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-15 : 13:20:30
So if the object makes it into production, and I've got to query adhoc, when I see it turn blue, I always put square brackets around it.

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

Subscribe to my blog
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-15 : 14:04:48
When I was playing with Policy Based Management I created a rule to prevent reserved-word object and column names. I can't remember the details but I don't think it could prevent, only log, which was kinda useless to my mind.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2011-07-15 : 16:25:06
Nice Rob!
Reminds me of this oldie:
Naming convention for Objects in MS SQL and .net


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -