Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

The Evil That is Select *

5.00/5 (5 votes)
2 Aug 2011CPOL 9.2K  
I'm using this way. SELECT * FROM VIEW. Consider the below Table has following fields(Including Non-display columns).Table...
I'm using this way. SELECT * FROM VIEW. Consider the below Table has following fields(Including Non-display columns).

Table Structure
-----------------------------------------------------------------------
tbl_Person
-----------------------------------------------------------------------
ID
PersonCode
PersonName
DateOfBirth
Gender
Address1
Address2
Address3
Zipcode
Phone
Fax
Mobile
Email
Website
CreatedOn
CreatedBy
ModifiedOn
ModifiedBy
Status
-----------------------------------------------------------------------


Now I'm creating View & Stored procedure with only columns which to be displayed(except Audit columns & some other) in UI.
View & Stored Procedure
CREATE VIEW vwPersonDetails
AS
SELECT PersonCode, PersonName, DateOfBirth, Gender, Address1, Address2, Address3, Zipcode, Phone, Fax, Mobile, Email, Website FROM tbl_Person

CREATE PROCEDURE Usp_PersonDetails
AS
SELECT PersonCode, PersonName, DateOfBirth, Gender, Address1, Address2, Address3, Zipcode, Phone, Fax, Mobile, Email, Website FROM tbl_Person

Finally I'm using the below one for execution. If you add new columns in table it won't make any changes in View or stored procedure until the modification. So when ever need to display new columns, I'll change the View or stored procedure.
SELECT * FROM vwPersonDetails
EXEC Usp_PersonDetails

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)