+ Reply to Thread
Results 1 to 3 of 3

Syntax Error when trying to SubQuery in MSQUERY

  1. #1
    Registered User
    Join Date
    01-22-2009
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2003
    Posts
    4

    Syntax Error when trying to SubQuery in MSQUERY

    I have table JobCost_Hdr and ShopOrd_Hdr with Job# and Shop# the matching fields. JobCost has one record per Job but ShopOrd can have multiple records per Job. I am trying to pull records from Job and one field from the first matching record from Shop. I have tried putting a Select Top 1 sub query but continue to get a syntax error regardless of what I try.
    This is the latest SQL try from Microsoft Query (Excel 2008).
    SELECT JOBCST_HDR.ID_JOB_ODBC, JOBCST_HDR.NAME_CUST,
    (SELECT TOP 1 SHPORD_HDR.ID_ITEM_PAR
    FROM PUBLIC.SHPORD_HDR SHPORD_HDR
    WHERE JOBCST_HDR.ID_JOB_ODBC=SUBSTRING(SHPORD_HDR.ID_SO_ODBC,4,6))
    FROM PUBLIC.JOBCST_HDR JOBCST_HDR,
    WHERE JOBCST_HDR.STAT_JOB='O'
    Thak you in advance for any help you can offer.

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Syntax Error when trying to SubQuery in MSQUERY

    I can't test tis at the moment but I would have expected you to need an "AS" in the "FROM" clauses ie:

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Syntax Error when trying to SubQuery in MSQUERY

    Howdy

    You appear to have a comma after the FROM clause and before the WHERE clause, so try removing it:

    Please Login or Register  to view this content.
    Are you targeting SQL Server with the code (as Substring isn't supported in the Access driver AFAIK)? I don't know if SQL Server supports SELECT TOP 1 syntax so this may be another source of the problem.

    Richard
    Richard Schollar
    Microsoft MVP - Excel

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1