Results 1 to 10 of 10

Thread: Use SQL Views to Update, Insert, Select, Delete

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Use SQL Views to Update, Insert, Select, Delete

    HI Guys,

    Is it possible to Update, Insert and Delete using Views? Where can I find the best tutorials for Views? Thanks in advance.

  2. #2
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Use SQL Views to Update, Insert, Select, Delete


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: Use SQL Views to Update, Insert, Select, Delete

    Thanks but how can I create an updatable views? I am not yet familiar with Views? What are the rules to consider?

  4. #4
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Use SQL Views to Update, Insert, Select, Delete

    that is based on your database and googling a little would do a lot
    HTH

  5. #5
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Use SQL Views to Update, Insert, Select, Delete

    Can i ask why you would want to ?

    I believe that in SQL Server at least you can update (and probably delete, not sure about insert) from a view, but only if the view is querying a single table. If you view returns fields from more than 1 table then No !!

    Still don't know why you would want to do this though, why not just Update/Insert/Delete normally directly into the tables ??
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Use SQL Views to Update, Insert, Select, Delete

    Presumably you are using SQL Server, although that has not definately been established.

    Assuming it is so, however, what version of SQL Server are you using?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Use SQL Views to Update, Insert, Select, Delete

    It doesn't matter how many tables the VIEW relates to - it's based on how many tables you refer to in the INSERT or UPDATE statement

    Code:
    Create Table Table1 (PKCol int, DataCol varchar(10))
    Go
    Create Table Table2 (PKCol int, DataCol varchar(10))
    Go
    Create View BothTables 
    as
    Select T1.PKCol "PKCol1", T1.DataCol "DataCol1"
         , T2.PKCol "PKCol2", T2.DataCol "DataCol2"
         From Table1 T1
         Left Join Table2 T2 on T2.PKCol=T1.PKCol
    Go
    Insert into BothTables values (1,'A',1,'B')
    --this gives error "View or function 'BothTables' is not updatable because the
    --modification affects multiple base tables."
    Go
    Insert into BothTables (PKCol1,DataCol1) values (1,'A')
    --this works - "(1 row(s) affected)"
    Go
    Insert into BothTables (PKCol2,DataCol2) values (1,'B')
    --this works - "(1 row(s) affected)"
    Go
    Select * from BothTables
    /*
    this returns:
    PKCol1      DataCol1   PKCol2      DataCol2
    ----------- ---------- ----------- ----------
    1           A          1           B
    
    (1 row(s) affected)
    */
    I've created two tables above - and a VIEW that JOIN's them

    Then I try to insert into the VIEW - all 4 columns - that's not allowed.

    But I can insert into each "segregated" segment of the VIEW.

    There is much power to this - UPDATEABLE VIEW's create an interesting method of serving data to a user that can in fact be insert'd and update'd

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Use SQL Views to Update, Insert, Select, Delete

    Right that's really interesting to know i was under the impression that you couldn't, can you also Delete from a View too ?

    So you can Insert/update a View as long as you are only updating data to 1 of the tables in the View ?

    What are the advantages of doing this then ?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Use SQL Views to Update, Insert, Select, Delete

    From what I know you cannot delete - as DELETE is not column specific - thus you cannot hit just the one BASE TABLE.

    The interesting thing about UPDATING and INSERTING into a VIEW is that you can give a user access to a VIEW that doesn't show all columns of the BASE TABLES. The "limiting" of access is the interesting and powerful feature.

    So you give a user or an application a limited view of data - but that VIEW is still updateable - still insertable...

    Not like I've ever done it myself - but I can appreciate places where you might use it...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Use SQL Views to Update, Insert, Select, Delete

    Right i see what you mean,

    yes that could be useful, and an interesting thing to know Thanks!!
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width