0

I came across this term in a reading. Here is the context:

"It is a best practice to make your T-SQL code self-documenting. Generally speaking, a view will be more self-documenting if the column names of the view are specified in the SELECT statement and not listed separately in the view."

I am just curious as to what this means. Any response is greatly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mitchell Walker
  • 211
  • 3
  • 8
  • 18

2 Answers2

2

It is the same as any other piece of code.

Meaningful names:

  • Give you table names that describe their contents.
  • Give the columns names and types that describe their contents and are appropriate for them.
  • Give your views, stored procedures, functions and other database objects descriptive names.

The specific quote seems to recommend using column names in views - I take this to mean that use of SELECT * is discouraged (it is a bad practice for various reasons).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Specifically, the quote is saying:

CREATE VIEW my_view AS
  SELECT col1,col2,col3 FROM my_table

is better than:

CREATE VIEW my_view(col1,col2,col3) AS
  SELECT * FROM my_table

The quote is recommending two separate things:

1) Don't use the my_view(col1,col2,col3) syntax. It's redundant, creates an additional maintenance burden, and provides a source for silent bugs (like swapped columns) if the order of the SELECT columns is changed but the view definition is not.

2) Don't use the * in any code you are saving.

Anon
  • 10,660
  • 1
  • 29
  • 31