April 16, 2007 at 1:32 pm
In terms of good practice, should you always bracket field, tables, etc.? if so, why?
Example: SELECT @CountryName = Descrip FROM [mydb].[dbo].[syCountry]
April 16, 2007 at 1:51 pm
It's a good practice, but many people don't do it. The reason why is so that you don't confuse the parser by using reserved words, spaces in column names, etc. To see this in action, create the following table:
CREATE TABLE tmpReservedTest
(
[SELECT] tinyint
)
INSERT INTO tmpReservedTest VALUES (123)
Now try the following:
SELECT SELECT FROM tmpReservedTest
Won't work so well. But this, on the other hand, works fine:
SELECT [SELECT] FROM tmpReservedTest
In reality, many DBAs would much rather not use the brackets, and are careful with avoiding reserved words as column names. If there is even a slight chance that you'll run into a reserved word, say you've created a process that dynamically creates code based on a passed column name (or user, or database name, etc.) over which you have no control, then definitely use brackets.
April 16, 2007 at 1:55 pm
This is only required if the names contain special characters that would otherwise render them unusable. The practice guidelines for our shop state that such names should not be used and that the brackets would be used only for names from vendors supplied systems over which we had no control which required them.
April 16, 2007 at 2:10 pm
I know about the need for it with reserver words, but, for example our dba always puts it around everything. Also, thew new SQL Prompt by RedGate does also.
April 16, 2007 at 3:07 pm
While some people might debate whether best practices require the brackets, it's certainly not bad programming practice to do so, so they're just covering their bases.
April 16, 2007 at 3:12 pm
And old QA does it every time.
Automatic tools cannot predict hat kind of names you will use for your objects. So, when it generates scripts it must not give any chance of generating SELECT * FROM dbo.USER
_____________
Code for TallyGenerator
April 16, 2007 at 3:32 pm
Thanks all! I tend to go with what is recommended industry wide. On with the brackets!
April 16, 2007 at 9:55 pm
Hi
Another point to consider is that the use of square bracket is MSSQL specific whereas the use of double quotes is quite standard. I have an idea (but may not be correct) that double quotes are ANSI standard.
And while you may not be planning to migrate your App, neither were the developers of any of those that i have migrated over the years. As a cross platform DBA I recommend that you use the highest possible selection of
ANSI standard
Industry Standard
Vendor Standard
Site Standard
Seemed like a good idea at the time
Regards
Karl
April 17, 2007 at 1:29 am
I would always put readability and maintainability first - any rules are a means to an end, not the end in themselves. As a general principle though, avoid reserved words!
April 17, 2007 at 3:04 am
I think the question shouldn't be if brackets are good practice but rather why would you need them? I think it's best practice to use only 7-bit ASCII alpha and numeric characters, no spaces, no hyphens and so on. One should not forget that only because SQL Server can cope with bracketed names another application might not be able to understand spaces in column names f.e. Remember Win95 and long file names? Even today there are still applications that have troubles with spaces in file names.
April 18, 2007 at 10:45 am
I think that the brackets are a pain in the butt that just bandaids poor naming conventions. Why would you name a column SELECT or any reserved words? You are just degrading the readability of your code. You should never name any columns any reserved words or special characters.
April 18, 2007 at 11:12 am
>>why would you need them
well, yea, that was included really in that I would like to know for sure!
April 18, 2007 at 11:13 am
Thanks TheReligion2000
April 18, 2007 at 11:13 am
My question was
>>In terms of good practice, should you always bracket field, tables, etc.? if so, why?
April 18, 2007 at 11:14 am
TheReligion2000, by my own standards, I have never put a space in for lets say an alias, column name, etc. So I guess the question may be still the "why" then, if you never use spaces...any input on that?
Viewing 15 posts - 1 through 15 (of 30 total)
You must be logged in to reply to this topic. Login to reply