Results 1 to 8 of 8

Thread: insert and update data in two different tables

Hybrid View

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

    Re: insert and update data in two different tables

    What is the database - ACCESS or MS SQL or MySQL - I really need to know that also - thanks.

    *** 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

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Colombia
    Posts
    9

    Re: insert and update data in two different tables

    Quote Originally Posted by szlamany
    What is the database - ACCESS or MS SQL or MySQL - I really need to know that also - thanks.
    It is a Microsoft SQL Server 2000 database.

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

    Re: insert and update data in two different tables

    Well I've always wanted to try this - seeing if a VIEW can be updated.

    So I did this in QUERY ANALYZER - but it doesn't work.

    Code:
    --Drop Table Test1 Drop Table Test2
    Create Table Test1 (keycol int, datacol1 varchar(1))
    Insert into Test1 values (1,'A')
    Create Table Test2 (keycol int, datacol2 varchar(1))
    Insert into Test2 values (1,'Z')
    Select * from Test1
    Select * from Test2
    Go
    Create View View1 as Select T1.KeyCol,T1.DataCol1,T2.DataCol2 From Test1 T1 Left Join Test2 T2 on T2.KeyCol=T1.KeyCol
    Go
    Select * from View1
    Update View1 Set DataCol1='B',DataCol2='B' Where KeyCol=1
    Select * from View1
    Gets this error:

    View or function 'View1' is not updatable because the modification affects multiple base tables.

    That's a shame.

    Since you are using MS SQL Server - have you considered using a STORED PROCEDURE to do the update - so you can encapsulate both table UPDATES in a single TRANSACTION?

    *** 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

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Colombia
    Posts
    9

    Re: insert and update data in two different tables

    Quote Originally Posted by szlamany
    Well I've always wanted to try this - seeing if a VIEW can be updated.

    So I did this in QUERY ANALYZER - but it doesn't work.

    Code:
    --Drop Table Test1 Drop Table Test2
    Create Table Test1 (keycol int, datacol1 varchar(1))
    Insert into Test1 values (1,'A')
    Create Table Test2 (keycol int, datacol2 varchar(1))
    Insert into Test2 values (1,'Z')
    Select * from Test1
    Select * from Test2
    Go
    Create View View1 as Select T1.KeyCol,T1.DataCol1,T2.DataCol2 From Test1 T1 Left Join Test2 T2 on T2.KeyCol=T1.KeyCol
    Go
    Select * from View1
    Update View1 Set DataCol1='B',DataCol2='B' Where KeyCol=1
    Select * from View1
    Gets this error:

    View or function 'View1' is not updatable because the modification affects multiple base tables.

    That's a shame.

    Since you are using MS SQL Server - have you considered using a STORED PROCEDURE to do the update - so you can encapsulate both table UPDATES in a single TRANSACTION?
    Yes I used a stored procedure but it produces the same error.

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

    Re: insert and update data in two different tables

    Post the code for the SPROC - I should be able to correct it for you...

    *** 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

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