Not in production environment, anyway!
Recently, I have been involved in implementing functionality that requires a modification to the structure of a SQL Server table. In fact, the nature of the change was a simple addition of a new column to the table in question.
Logic would dictate that that wouldn’t be too problematic. Right? Wrong!
The fact is, that when you use ‘*’ to reference all columns on a table, you’ll make it impossible to perform any reliable impact analysis on your code base. Columns will be used without being explicitly referenced.
If you add more columns to the table in future, who can guarantee that your existing views or stored procedures will actually need them? If you have code in them that performs a ‘Select *’ then they will, regardless. The obvious problem with this is the overhead of returning unwanted data from the database, and the potential degradation of performance inherent therein. However, there is another pitfall surrounding table joins, which I will now attempt to demonstrate:
Imagine you start out with two tables:
create table Test (Id int, code char(1)) create table Test2 (Id int, TestId int, name varchar(10))
And you would like a query that joins these two, so you create a view
create view vwJoinTests as select t.code, t2.* from test t inner join test2 t2 on t.id = t2.testid
You can select from this, no problem
select code from vwJoinTests
A year later, another developer comes along and adds a new column to Test2:
alter table Test2 add code char(1)
Now, the column ‘Code’ exists on both tables.
Selecting from the view is still fine:
select code from vwJoinTests
And, because our original ‘select’ remains intact (it doesn’t break anything), this fault will not be picked up by testing.
But, if I want to reapply the view code, I get the following error:
Column names in each view or function must be unique. Column name ‘code’ in view or function ‘vwJoinTests’ is specified more than once.
Why would I want to reapply the view? Well:
- You may want to modify it
- You may want to rebuild a database
- You may want to install a brand new database on a client site
Technically, 2 and 3 are the same, but I thought I’d emphasise the potential for embarrasment when it comes to your clients.
It also emphasises the point that structural changes to the database, should be accompanied by a full database build test to ensure its integrity has been maintained.