Results 1 to 13 of 13

Thread: Complex update query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Complex update query

    Hi

    I have the code below but have problems trying to convert it into one clean update query. Any help would be greatly appreciated. Thanks.

    Code:
        Set MyTable = New ADODB.Recordset
        strSQL = "SELECT " & colHdg & ".ID FROM " & _
            "(" & colHdg & " INNER JOIN VoicePPC ON " & colHdg & ".Customer_Index = " & _
            "VoicePPC.CustID) INNER JOIN CustomerVCLI ON (VoicePPC.CLI = CustomerVCLI.CLI) AND " & _
            "(VoicePPC.CustID = CustomerVCLI.CustID) AND (" & colHdg & ".Dialling_Code_Calling = " & _
            ConcatenateFields("CustomerVCLI.NationalDiallingCode", "CustomerVCLI.RegionalDiallingCode") & _
            ") AND (" & colHdg & ".Number_Calling = CustomerVCLI.CLIDetails) WHERE " & _
            "((VoicePPC.CustID = " & custIdx & ") AND (" & colHdg & ".Date_Time_Called >= #" & FormatDateForDB(fromDate, False) & _
            "#) AND (" & colHdg & ".Date_Time_Called <= #" & FormatDateForDB(toDate, False) & "#) AND (" & colHdg & ".Customer_Index = " & custIdx & ") AND " & _
            "(VoicePPC.VoicePricePlan = '" & pPlan & "'))"
        MyTable.Open ModifySQLStatement1(1, strSQL), myMainDbCon, adOpenDynamic, adLockBatchOptimistic, adCmdText
        'debug.print MyTable.RecordCount
        Do Until MyTable.EOF
            DoEvents
            strSQL = "UPDATE " & colHdg & " SET Valid_CLI_For_Plan = 1 WHERE ID = " & MyTable![id]
            myMainDbCon.Execute ModifySQLStatement1(1, strSQL), , adCmdText
            If MyTable.EOF = False Then
                MyTable.MoveNext
            End If
        Loop
        MyTable.Close
        Set MyTable = Nothing

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Complex update query

    I cannot test it but one method could be like the following:
    Code:
    strSQL = "SELECT " & colHdg & ".ID FROM " & _
            "(" & colHdg & " INNER JOIN VoicePPC ON " & colHdg & ".Customer_Index = " & _
            "VoicePPC.CustID) INNER JOIN CustomerVCLI ON (VoicePPC.CLI = CustomerVCLI.CLI) AND " & _
            "(VoicePPC.CustID = CustomerVCLI.CustID) AND (" & colHdg & ".Dialling_Code_Calling = " & _
            ConcatenateFields("CustomerVCLI.NationalDiallingCode", "CustomerVCLI.RegionalDiallingCode") & _
            ") AND (" & colHdg & ".Number_Calling = CustomerVCLI.CLIDetails) WHERE " & _
            "((VoicePPC.CustID = " & custIdx & ") AND (" & colHdg & ".Date_Time_Called >= #" & FormatDateForDB(fromDate, False) & _
            "#) AND (" & colHdg & ".Date_Time_Called <= #" & FormatDateForDB(toDate, False) & "#) AND (" & colHdg & ".Customer_Index = " & custIdx & ") AND " & _
            "(VoicePPC.VoicePricePlan = '" & pPlan & "'))"
    strSQL = "UPDATE " & colHdg & " SET Valid_CLI_For_Plan = 1 WHERE ID IN (" & strSQL & ")"
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    I don't see how what you wrote is different to what I posted. I am looking for a single update query to replace the select and update I posted. Thanks.

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Complex update query

    Didn't you notice the following modification I made?

    Code:
    strSQL = "UPDATE " & colHdg & " SET Valid_CLI_For_Plan = 1 WHERE ID IN (" & strSQL & ")"
    You can try executing it and see if it works the same, I suppose they should.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    Sorry, will try this.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    I get an error: Operation must use an updateable query. Any ideas?

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Complex update query

    How did you use the sql?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    And sorry again. It worked very well. I had missed a bit of your sql out. Thanks for your help with this.

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Complex update query

    No problem. Since your problem is solved you can now mark your thread as Resolved using the Thread Tools menu.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Complex update query

    Although UPDATE'ing with a WHERE clause that uses a sub-query is fine - I do it often.

    If this is MS SQL - you should know that there is a syntax trick that allows a more natural way of doing this.

    If you can say:

    Code:
    Select SomeFld
       From SomeTbl
       Left Join yadda yadda
       Left Join more and more
       Where x.aa = ...
    Then you can also say

    [code]
    Code:
    Update SomeTbl Set SomeColumn=DDD
       From SomeTbl
       Left Join yadda yadda
       Left Join more and more
       Where x.aa = ...
    Notice that the UPDATE has a FROM clause that exactly matches the SELECT's FROM clause.

    The real power of this syntax though is that the DDD value that you are using to UPDATE into SomeTbl can come from one of the JOIN's as long as you specify the ALIAS properly.

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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    Hi

    Saw your query does not work under mysql. I get the following error:

    You can't specify target table 'story_category' for update in FROM clause.

    story_category is the table name in colHdg.

    Any idea? Thanks.

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

    Re: Complex update query

    Sorry - I don't use MySQL.

    The syntax I showed was for MS SQL.

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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Complex update query

    Thanks. I tried on Access and MS SQL and it works fine. MySQL does not though. Thanks anyway.

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