Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Triggers affect on SQL System VariablesAnother solution for my caching problem with servicestack.text, dapper and sql server. »
    comments

    If ever a DBA walked up a mountain and came back down with two stones that had 10 commandments written on them, “thou shalt not use SELECT *” would be one of them. However, that same DBA would turn around and within 5 minutes use it themselves!

    One place that DBAs use SELECT * is when they create views. The idea is that you do actually want every column available to the view because the query that hits that view should limit which columns it wants returned.

    This works, but there is one big issue with it that most people learn the hard way. If the schema of the source table that the view is selecting from changes, the view will NOT automatically update to include those changes.

    For example, let’s create a simple table with some data and a view over it (because we are using really simple code, I’m not going format visually the way I normally do).

    1. CREATE TABLE tblViewExample (col1 INT NULL);
    2. GO
    3. CREATE VIEW vViewExample AS SELECT * FROM tblViewExample
    4. GO
    5. INSERT INTO vViewExample (col1) VALUES (1);
    6. INSERT INTO vViewExample (col1) VALUES (2);
    7. INSERT INTO vViewExample (col1) VALUES (3);
    8. GO
    9. SELECT * FROM vViewExample;

    I did break the rule of no SELECT * again but it’s to demonstrate the point of this article.
    So far everything is being returned as we expect it. Now let’s change the schema of our table and see what happens.

    1. ALTER TABLE tblViewExample ADD col2 INT NULL;
    2. GO
    3. UPDATE tblViewExample SET col2 = col1 * 5;
    4. GO
    5. SELECT * FROM vViewExample;

    Notice that col2 is missing even though the view is using SELECT *. Let’s see what happens if we try to use the view to insert a new record with data in col2.

    1. INSERT INTO vViewExample (col1, col2) VALUES (5, 5);

    To fix the “broken” view, we have to rebuild it.

    1. DROP VIEW vViewExample;
    2. GO
    3. CREATE VIEW vViewExample AS SELECT * FROM tblViewExample

    Now let’s try the insert statement again and then the select statement.

    1. INSERT INTO vViewExample (col1, col2) VALUES (5, 5);
    2.  
    3. SELECT * FROM vViewExample;

    And of course, let’s clean up after ourselves!

    1. DROP VIEW vViewExample;
    2. DROP TABLE tblViewExample;

    About the Author

    Kevin -=Conan The Canadian=- is a SQL Server geek and has been writing SQL queries for over 10 years. After spending 5 years developing, designing and tuning HighJump systems, he made the jump to become a full fledged DBA. Since that jump, he's created SSIS Packages, transactional replication, cross platform replication with DoubleTake, monitoring with Diagnostic Manager, and so much more!
    Social SitingsTwitterLTD RSS Feed
    2932 views
    sql, views
    InstapaperVote on HN

    3 comments

    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) I was just going to post the same thing as Denis. He beat me by a minute.
    03/12/13 @ 10:21
    Comment from: Kevin Conan [Member]
    Nice!
    03/12/13 @ 12:14

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)