This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Guys, Don’t surprise with this question. If you are in front of me, surely I will ask this question in an interview. I know, most of you are using ‘SELECT *’ in T-SQL code.
I already shared an article on ‘SELECT *’ to be good or bad.
Regarding database coding standard, I am always strict and even I never allow single extra space in T-SQL Code.
Let me share my experience,
In my group, I already set guidelines like ‘do not use SELECT *’, but then also I found total 320 ‘SELECT *’ statements out of 2125 statements in our project.
The Developers are always running with their practice, but as DB Professionals we must prevent SELECT *.
When I found these bad queries, and I didn’t react immediately. I had been waiting for tight deadlines. Before a month ago I scheduled an internal demonstration.
On this occasion, I planned the penalty for those developers who wrote ‘SELECT *’ in their stored procedures.
In the morning, I added one extra overnight computed column on those tables. The computed column was (BadSelect AS (1 / 0)), that means if anyone is selecting data using ‘SELECT *’, they get error like ‘Divide by zero error encountered’ and fails the execution of a query.
Now after this alteration, developers had removed all ‘SELECT *’ and we started our demonstration late by 3 hours.
It is OK for me, but next time they will never put ‘SELECT *’ in T-SQL Code.
Use below demonstration, to test this exercise and prepare yourself for this kind of interview questions as well.
Generate a script to add computed column in all the table:
You can copy script for requiring a table and can create computed column.
1 2 3 |
SELECT 'ALTER TABLE ' + name + ' ADD BadSelect AS (1 / 0) GO' FROM sys.tables |
If developer execute ‘SELECT *’, they will get a bellow error:
1 2 |
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered. |
Generate a script to remove computed column from all the table:
1 2 3 |
SELECT 'ALTER TABLE ' + name + ' DROP COLUMN BadSelect GO' FROM sys.tables |