|
-
May 3rd, 2009, 04:25 PM
#1
Thread Starter
Hyperactive Member
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
-
May 3rd, 2009, 04:38 PM
#2
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 & ")"
-
May 4th, 2009, 03:54 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 4th, 2009, 07:33 PM
#4
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.
-
May 5th, 2009, 02:55 AM
#5
Thread Starter
Hyperactive Member
-
May 5th, 2009, 03:23 AM
#6
Thread Starter
Hyperactive Member
Re: Complex update query
I get an error: Operation must use an updateable query. Any ideas?
-
May 5th, 2009, 03:34 AM
#7
-
May 5th, 2009, 04:18 AM
#8
Thread Starter
Hyperactive Member
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.
-
May 5th, 2009, 04:07 PM
#9
Re: Complex update query
No problem. Since your problem is solved you can now mark your thread as Resolved using the Thread Tools menu.
-
May 5th, 2009, 05:01 PM
#10
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.
-
May 7th, 2009, 04:37 PM
#11
Thread Starter
Hyperactive Member
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.
-
May 7th, 2009, 04:52 PM
#12
Re: Complex update query
Sorry - I don't use MySQL.
The syntax I showed was for MS SQL.
-
May 7th, 2009, 05:00 PM
#13
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|