web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics GP (Archived)

SQL code help

(0) ShareShare
ReportReport
Posted on by

Need SQL help - the code below works fine

SELECT S.ORDOCAMT,
S.SALSTERR,
S.SLPRSNID,
S.SOPNUMBE,
S.DOCDATE,
S.SOPTYPE,
S.CUSTNMBR,
T.CUSTCLAS,
T.CUSTNAME,
T.USERDEF1,
S.SUBTOTAL,
S.DOCAMNT,
S.MRKDNAMT

FROM SOP10100 S


LEFT OUTER JOIN RM00101 T
ON T.CUSTNMBR = S.CUSTNMBR

UNION ALL

SELECT S.ORDOCAMT,
S.SALSTERR,
S.SLPRSNID,
S.SOPNUMBE,
S.DOCDATE,
S.SOPTYPE,
S.CUSTNMBR,
T.CUSTCLAS,
T.CUSTNAME,
T.USERDEF1,
S.SUBTOTAL,
S.DOCAMNT,
S.MRKDNAMT

FROM SOP30200 S

LEFT JOIN RM00101 T
ON T.CUSTNMBR = S.CUSTNMBR

ORDER BY S.SOPNUMBE

GO

 

but when I try to add a WHERE stmt

SELECT S.ORDOCAMT,
S.SALSTERR,
S.SLPRSNID,
S.SOPNUMBE,
S.DOCDATE,
S.SOPTYPE,
S.CUSTNMBR,
T.CUSTCLAS,
T.CUSTNAME,
T.USERDEF1,
S.SUBTOTAL,
S.DOCAMNT,
S.MRKDNAMT

FROM SOP10100 S
WHERE S.SALESTERR = 'JA'


LEFT OUTER JOIN RM00101 T
ON T.CUSTNMBR = S.CUSTNMBR

UNION ALL

SELECT S.ORDOCAMT,
S.SALSTERR,
S.SLPRSNID,
S.SOPNUMBE,
S.DOCDATE,
S.SOPTYPE,
S.CUSTNMBR,
T.CUSTCLAS,
T.CUSTNAME,
T.USERDEF1,
S.SUBTOTAL,
S.DOCAMNT,
S.MRKDNAMT

FROM SOP30200 S
WHERE S.SALESTERR = 'JA'

LEFT JOIN RM00101 T
ON T.CUSTNMBR = S.CUSTNMBR

ORDER BY S.SOPNUMBE

GO

i get this error message:  

Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'LEFT'.
Msg 156, Level 15, State 1, Line 41
Incorrect syntax near the keyword 'LEFT'.

It doesn't make sense - probably something stupid but I don't see it

*This post is locked for comments

I have the same question (0)
  • MattPaulen Profile Picture
    6,912 on at

    Try moving your WHERE statements below your joins:

    SELECT S.ORDOCAMT,
    S.SALSTERR,
    S.SLPRSNID,
    S.SOPNUMBE,
    S.DOCDATE,
    S.SOPTYPE,
    S.CUSTNMBR,
    T.CUSTCLAS,
    T.CUSTNAME,
    T.USERDEF1,
    S.SUBTOTAL,
    S.DOCAMNT,
    S.MRKDNAMT

    FROM SOP10100 S


    LEFT OUTER JOIN RM00101 T
    ON T.CUSTNMBR = S.CUSTNMBR

    WHERE S.SALESTERR = 'JA'

    UNION ALL

    SELECT S.ORDOCAMT,
    S.SALSTERR,
    S.SLPRSNID,
    S.SOPNUMBE,
    S.DOCDATE,
    S.SOPTYPE,
    S.CUSTNMBR,
    T.CUSTCLAS,
    T.CUSTNAME,
    T.USERDEF1,
    S.SUBTOTAL,
    S.DOCAMNT,
    S.MRKDNAMT

    FROM SOP30200 S

    LEFT JOIN RM00101 T
    ON T.CUSTNMBR = S.CUSTNMBR

    WHERE S.SALESTERR = 'JA'

    ORDER BY S.SOPNUMBE

    GO

  • Verified answer
    MattPaulen Profile Picture
    6,912 on at

    You'll also want to change the where clause to be SALSTERR instead of SALESTERR

  • Verified answer
    Community Member Profile Picture
    on at

    NOPE - now I get this message:  

    Msg 207, Level 16, State 1, Line 21

    Invalid column name 'SALESTERR'.

    Msg 207, Level 16, State 1, Line 45

    Invalid column name 'SALESTERR'.

    I've tried it with S.SALESTERR, T.SALESTERR and just SALESTERR

  • Suggested answer
    Community Member Profile Picture
    on at

    ugh - you saw it!!!!  I thank you

  • MattPaulen Profile Picture
    6,912 on at

    Not a problem.  Sometimes you just need another set of eyes (I know I do all the time)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics GP (Archived)

#1
mtabor Profile Picture

mtabor 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans