Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are delimited identifiers considered a "best-practice" in Transact-SQL?

I'm working on some legacy SQL and the author delimited every column name and data type declaration. See the following:

CREATE TABLE SomeTable (
    [SomeDate]   [datetime] NOT NULL,
    [SomeInt]    [int]      NOT NULL,
    [SomeString] [nvarchar] NOT NULL
) ON [PRIMARY]
GO

Is this considered a best-practice when writing T-SQL for SQL Server? Since I'm now maintaining this code, should I continue the practice?

like image 823
Travis Heseman Avatar asked Dec 02 '09 22:12

Travis Heseman


2 Answers

I personally would only write that if you're using reserved keywords as column/table names, which you shouldn't be anyway. Personally I think otherwise, it makes the SQL code less 'clean' and a bit more difficult to read.

This style is usually what is generated by SQL tools, as it guarantees that there won't be any issues with reserved word conflicts.

like image 56
LJW Avatar answered Oct 21 '22 08:10

LJW


If the name of the table or column is "harmless" like "SomeInt", then the square brackets [...] aren't required - you can specify them, if you want to, don't have to.

On the other hand - always using those will make sure that even "dangerous" column names like '[Message]' and others, or column names with spaces in them like [Product Name], will always be handled correctly.

So - you don't have to continue doing it, but I would think it's a good practice and if it's already been used, I'd recommend to keep using it.

like image 45
marc_s Avatar answered Oct 21 '22 07:10

marc_s