1

I have the following query.

with getstock as 
(
    select 
        a.bomparent, a.bomchild, a.bomqty, a.bompos, a.baltmethod, a.bomissue
    from 
        bom a
    where 
        bomparent = 'QZ10-0262601' and baltmethod = '1'

    union all

    select 
        parent.bomparent, parent.bomchild, parent.bomqty, parent.bompos, parent.baltmethod, parent.bomissue 
    from 
        getstock as a
    inner join 
        bom as parent, stock as s on parent.bomparent = a.bomchild
    where 
        parent.baltmethod = '1' and parent.bomparent = s.stocknum
)
select  *
from getstock

When I run it, I get the following error.

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ','.

Where is the issue?

4 Answers 4

5

You can not use multiple tables in JOIN. It's possible in FROM, but I won't suggest that. That's the old-style JOIN syntax. You should rewrite your query using explicit JOIN:

with getstock as 
(
    select 
        a.bomparent, a.bomchild, a.bomqty, a.bompos, a.baltmethod, a.bomissue
    from 
        bom a
    where 
        bomparent = 'QZ10-0262601' and baltmethod = '1'

    union all

    select 
        parent.bomparent, parent.bomchild, parent.bomqty, parent.bompos, parent.baltmethod, parent.bomissue 
    from 
        getstock as a
    inner join 
        bom as parent on parent.bomparent = a.bomchild
    inner join 
        stock as s on parent.bomparent = s.stocknum
    where 
        parent.baltmethod = '1'
)
select  *
from getstock

For more reading: Avoid using old-style JOIN syntax.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also re-write your query with proper joins for select with union all as below :

SELECT        bomparent, bomchild, bomqty, bompos, baltmethod, bomissue
FROM            bom AS a
WHERE        (bomparent = 'QZ10-0262601') AND (baltmethod = '1')

    union all


SELECT        bom.bomparent, bom.bomchild, bom.bomqty, bom.bompos, bom.baltmethod, bom.bomissue
FROM            getstock INNER JOIN
                         bom ON getstock.bomchild = bom.bomparent INNER JOIN
                         stock ON bom.bomparent = stock.stocknum
WHERE        (bom.baltmethod = N'1')

Comments

1
with getstock as 
(
    select a.bomparent, a.bomchild, a.bomqty, a.bompos,
    a.baltmethod, a.bomissue
    from bom a
    where bomparent = 'QZ10-0262601' and baltmethod = '1'
    union all
    select parent.bomparent, parent.bomchild, parent.bomqty,
    parent.bompos,  parent.baltmethod, parent.bomissue 
    from getstock as a
    inner join bom as parent
     on parent.bomparent = a.bomchild
    inner join stock as s
     parent.bomparent = s.stocknum
   where parent.baltmethod = '1' 
)
select  *
from getstock

Comments

1

Following code should work but its not following best practices. Always use Join condition properly(INNER JOIN ...ON).

with getstock as 
(
    select a.bomparent, a.bomchild, a.bomqty, a.bompos, a.baltmethod, a.bomissue
    from bom a
    where bomparent = 'QZ10-0262601' and baltmethod = '1'
    union all

    select parent.bomparent, parent.bomchild, parent.bomqty, parent.bompos, parent.baltmethod, parent.bomissue 
    from getstock as a,
        bom as parent, 
        stock as s  
    where parent.bomparent = a.bomchild and
        parent.baltmethod = '1' and parent.bomparent = s.stocknum
)
select  * from getstock

2 Comments

And you're NOT using INNER JOIN in your example ..... this is even worse than the OP's question ...
I know, But this is a way of soving the issues. That's all i am trying to convey. I have already mentioned that its not a good way to do in this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.