Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 February SQL Server Interview: How to prevent ‘SELECT *’ using T-SQL Script?

SQL Server Interview: How to prevent ‘SELECT *’ using T-SQL Script?

Anvesh (2)-min

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.

SELECT all columns to be good or bad in database system

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
Feb 19, 2017Anvesh Patel
SQL Server Interview: A Golden question, Can we disable the Clustered Index?SQL Server: Don't Enable Auto Close Database Option
Comments: 9
  1. Alee
    October 5, 2017 at 3:14 pm

    thank you for sharing anvesh, I am reading all your articles and it’s very useful for the community.
    Especially this about the prevent SELECT * which is too good. I am agree with you, we should not use * at all

  2. santosh Dantuluri
    May 9, 2018 at 1:06 pm

    I agree with you.As a DBA im also preventing SELECT * in all SP’s.

  3. Venkatesh
    May 13, 2018 at 8:14 pm

    Hi Anvesh.
    You ara doing great!!
    Can you please give an article on migration of database for any version postgresql in briefly.As i have many doubt on migration.
    Thanks,Venkatesh

    • Anvesh Patel
      Anvesh Patel
      May 14, 2018 at 7:46 pm

      Sure, will write on this.

  4. Shruti patil
    July 4, 2018 at 7:56 am

    Thanks that you are not my DB Manager 🙂

  5. Manish
    July 28, 2018 at 1:36 am

    Great article !

  6. Romy
    August 21, 2018 at 5:00 pm

    great sharing anvesh

  7. http://canadianorderpharmacy.com/
    June 5, 2019 at 5:21 am

    Excellent beat ! I would like to apprentice while you amend your web site, how could i subscribe for a weblog web site? The account aided me a applicable deal. I had been a little bit familiar of this your broadcast offered bright transparent concept

  8. add
    February 3, 2020 at 12:57 pm

    good explanation

Anvesh Patel
Anvesh Patel

Database Engineer

ImageFebruary 19, 2017 SQL Server, SQL Server InterviewAnvesh Patel, database, database research and development, dbrnd, Prevent SELECT *, select, SELECT ALL Column, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, t-sql interview, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....