Results 1 to 6 of 6

Thread: SQL Problem

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    SQL Problem

    I have a table (tblGroupCases) where I am attempting to update a particular field, the problem is that in order to do this I have to join three tables. Two of these tables (Officers and Assoc_Inc_Off) are located in a different MS SQL db. I was thinking about creating a view joining the first two tables then using vb code to join the View and the table that I want to update. My question is can I join three tables in an Update query?
    The following is my failed attempt, tblGroupCases is the table I want to update with the data contained in the Officers table but I need to connect these two (2) table via the Assoc_Inc_Officer Table:


    VB Code:
    1. strSQL = "UPDATE GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases "
    2.             strSQL = strSQL & "SET GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.Subject_Officer = "
    3.             strSQL = strSQL & "IADATA.DBO.OFFICERS.LNAM "
    4.             strSQL = strSQL & "FROM GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases, "
    5.             strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF, IADATA.DBO.OFFICERS "
    6.             strSQL = strSQL & "WHERE GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.INCNUM =  IADATA.DBO.ASSOC_INC_OFF.INCNUM "
    7.             strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF.INCNUM = IADATA.DBO.Officers.INCNUM;"

    If I remove the last line:

    VB Code:
    1. strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF.INCNUM = IADATA.DBO.Officers.INCNUM;"

    The code runs but it doesn't update the field.
    Thank you,
    Last edited by Mark Gambo; Dec 1st, 2003 at 05:43 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Other than missing an And in your Where clause the syntax of your sql statement is correct. Are you getting an error message? The only thing I can think of that might stop the statement from executing is permissions.

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    Bruce,
    Thanks for your reply. I don't beleive that their is a permissions issue because I am able execute other update queries and make table queries. As for the missing AND would I putting it between the two where statements?

    VB Code:
    1. strSQL = strSQL & "WHERE GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.INCNUM =  IADATA.DBO.ASSOC_INC_OFF.INCNUM AND"
    2. strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF.INCNUM = IADATA.DBO.Officers.INCNUM;"

    Thanks again!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Yes, you must separate each criteria in the Where clause with an operator, such as And, Or etc..

    Probably just a typo but make sure you have a space at the end of the first line (after the And) or at the beginning of the second line (before IAData).

  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    Bruce,
    Thanks, that solved my problem. I modified the sql as follows:

    VB Code:
    1. strSQL = "UPDATE GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases "
    2.             strSQL = strSQL & "SET GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.Subject_Officer = "
    3.             strSQL = strSQL & "GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.Subject_Officer  + " & vbCrLf & " + "
    4.             strSQL = strSQL & "IADATA.DBO.OFFICERS.TITLE + ' ' + IADATA.DBO.OFFICERS.FNAM + ' ' + IADATA.DBO.OFFICERS.LNAM, "
    5.             strSQL = strSQL & "GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.Subject_Officer_Cnt =  "
    6.             strSQL = strSQL & "GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.Subject_Officer_Cnt + 1 "
    7.             strSQL = strSQL & "FROM GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases, "
    8.             strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF, IADATA.DBO.OFFICERS "
    9.             strSQL = strSQL & "WHERE GROUP_MANAGEMENT.GROUP_MGMT.tblGroupCases.INCNUM =  IADATA.DBO.ASSOC_INC_OFF.INCNUM AND "
    10.             strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF.OFFNUM = IADATA.DBO.Officers.OFFNUM AND "
    11.             strSQL = strSQL & "IADATA.DBO.ASSOC_INC_OFF.LINK_TYPE = 'OFFICER';"

    The Officer Table may contain one or more related records to the tblGroupCase table and I want to store it in the Subject_Officer Field. When I run the query the first value in the Officers Table is being added to the tblGroupCases Table and the additional data is not appended. The additional data meets the same criteria as the appended data and if I run the query multiple times the same name will be appended again and again.

    I have been able to create an Update Query in Access using the same data and I am able to format the data in the manner I want it:

    VB Code:
    1. UPDATE GROUP_MGMT_tblGroupCases INNER JOIN (dbo_ASSOC_INC_OFF INNER JOIN dbo_OFFICERS ON dbo_ASSOC_INC_OFF.OFFNUM = dbo_OFFICERS.OFFNUM) ON
    2. GROUP_MGMT_tblGroupCases.INCNUM = dbo_ASSOC_INC_OFF.INCNUM SET
    3. GROUP_MGMT_tblGroupCases.Subject_Officer = [GROUP_MGMT_tblGroupCases]![Subject_Officer] & [dbo_OFFICERS]![Title] & " " & [dbo_OFFICERS]![FNAM] & " " &
    4. [dbo_OFFICERS]![LNAM] & Chr(13) & Chr(10), GROUP_MGMT_tblGroupCases.Subject_Officer_Cnt =
    5. [GROUP_MGMT_tblGroupCases]![Subject_Officer_Cnt]=[GROUP_MGMT_tblGroupCases]![Subject_Officer_Cnt]+"1";




    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    Bump
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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