SQL Server Avoid using SELECT * in a View

Slides:



Advertisements
Similar presentations
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
Advertisements

SQL reviews. Stored Procedures Create Procedure Triggers.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Chapter 9 Joining Data from Multiple Tables
Programming using C# Joins SQL Injection Stored Procedures
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
By: Matt Batalon, MCITP  Another form of temporary storage that can be queried or joined against, much like a table variable, temp.
Component 4/Unit 6c Topic III Structured Query Language Background information What can SQL do? How is SQL executed? SQL statement characteristics What.
Views Lesson 7.
Metadata Metadata is information about data or other information.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
Component 4: Introduction to Information and Computer Science Unit 6a Databases and SQL.
EXAM 1 NEXT TUESDAY…. EXAMPLE QUESTIONS 1.Why is the notion of a “state” important in relational database technology? What does it refer to? 2.What do.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
 CONACT UC:  Magnific training   
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
SQL - Training Rajesh Charles. Agenda (Complete Course) Introduction Testing Methodologies Manual Testing Practical Workshop Automation Testing Practical.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CPSC-310 Database Systems
More SQL: Complex Queries,
Relational Model.
Connect to SQL Server and run select statements
Using DML to Modify Data
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
© 2016, Mike Murach & Associates, Inc.
SQL MODELER - OPEN There are Three Ways to open the SQL Modeler
ITEC 313 Database Programming
A Table with a View: Database Queries
Basic Nested Queries and Views
Introduction to Database Systems CSE 444 Lecture 04: SQL
SQL 101.
Structured Query Language (SQL) William Klingelsmith
CSCI 2141 – Intro to Database Systems
Using Table Expressions
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL This presentation will cover: View in database MySQL installation
Introduction To Structured Query Language (SQL)
Writing Basic SQL SELECT Statements
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
Introduction To Structured Query Language (SQL)
A Table with a View: Database Queries
Chapter 8 Advanced SQL.
Excel – VBA TypeMismatch (Error 13) on OpenRecordset Method Call
EXCEL Creating An Array In VBA
HTML How to middle-align text and image
Source: Link Posted By: HeelpBook Permalink: Link
SQL Server How to find duplicate values with T-SQL
Excel Import data from Access to Excel (ADO) using VBA
EXCEL How to Hide Columns Using VBA
SQL Server SELECT * and Adding Column Issue in View
Windows Vista/7 How To Make the Windows Desktop Scrollable
Excel – VBA – How to use Non-Contiguous range of cells
SQL Server Sp_refreshview for all views in a Database
A Table with a View: Database Queries
PERL How to Use the Array Push() Function
Source: Link Posted By: HeelpBook Permalink: Link
Understanding Core Database Concepts
Manipulating Data Lesson 3.
Presentation transcript:

SQL Server Avoid using SELECT * in a View HeelpBook © - 2011 12/05/2019 SQL Server Avoid using SELECT * in a View Source: Link Posted By: HeelpBook Permalink: Link 12/05/2019 How-Tos <<

HeelpBook © - 2011 12/05/2019 Suppose you have a table (tblDemo) and a view (vwDemo) over that table that joins tblDemo to another table to retrieve their results.   The view might resemble the following:   CREATE VIEW vwDemo AS SELECT d.*, o.OtherField01, o.OtherField02 FROM tblDemo d INNER JOIN tblOther o ON d.ID = o.ID 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

>> SQL Server - Avoid using SELECT * in a view How-Tos << HeelpBook © - 2011 12/05/2019 While I’ve never advocated selecting * from anything, it does have its uses; but it turns out there’s potentially a nasty gotcha.  Personally, I like to err on the side of higher specificity, though that usually requires a bit more manual tweaking as time goes on. What happens if the schema of tblDemo changes?  Well, if you don’t update or recreate your view, you may very well get some unexpected side effects. 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 For instance, if a new field were added to tblDemo (e.g., ALTER TABLE tblDemo ADD NewField int), the view, when inspected in SQL shows the following:   SELECT d.ID, d.Field01, d.Field02, d.Field03, d.NewField AS OtherField01, o.OtherField01 AS OtherField02, o.OtherField02 FROM tblDemo d INNER JOIN tblOther o ON d.ID = o.ID 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 Notice that the ‘NewField’ got aliased with the field name at the new fields position! That field, in turn, received the next field’s alias, and so on. Understandably, this can have some very negative side effects in your applications. 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 For this reason I would strongly recommend AGAINST using SELECT * in a view in SQL Server.  Of course, if your view does nothing but SELECT * on a single table or the ‘*’ is the last part of the SELECT clause (such as SELECT o.OtherField01, o.OtherField02, d.* FROM …) then you circumvent the issue.  When used in conjunction with other columns, however, there are indeed repercussions. 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 Use sp_refreshview to fix this “issue”   To refresh the view’s metadata information, run the sp_refreshview stored procedure against a typical view called, for example V1: EXEC sp_refreshview ‘dbo.V1’; 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 A typical example is where a schema change in the underlying objects is not reflected in the views’ metadata information. You might find it to be a good practice to refresh all views’ metadata information after applying schema changes to objects in the database. 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

>> SQL Server - Avoid using SELECT * in a view How-Tos << HeelpBook © - 2011 12/05/2019 To avoid the tedious process of writing the sp_refreshview statements you can use the following query:   SELECT N’EXEC sp_refreshview ‘ + QUOTENAME(VIEW_NAME, ‘’’’) + ‘;’ AS cmd FROM (SELECT QUOTENAME(TABLE_SCHEMA) + N’.’ + QUOTENAME(TABLE_NAME) AS VIEW_NAME FROM INFORMATION_SCHEMA.VIEWS) AS V WHERE OBJECTPROPERTY(OBJECT_ID(VIEW_NAME) , ‘IsSchemaBound’) = 0; The query will generate as its output the lines of code with the sp_refreshview statements against all views in the database that are not schema-bound. 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<

HeelpBook © - 2011 12/05/2019 That’s all Folks Come visit us on www.heelpbook.net to find and read more documents and guides… AND / OR Subscribe to our feed RSS: Link Or see&read us on FeedBurner: Link { 12/05/2019 >> SQL Server - Avoid using SELECT * in a view How-Tos <<