Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

Getting an odd error, SQL Server query using `WITH` clause

The following query:

WITH 
    CteProductLookup(ProductId, oid) 
    AS 
    (
        SELECT p.ProductID, p.oid
        FROM [dbo].[ME_CatalogProducts] p 
    )

SELECT 
    rel.Name as RelationshipName,
    pl.ProductId as FromProductId,
    pl2.ProductId as ToProductId
FROM 
    (
    [dbo].[ME_CatalogRelationships] rel 
    INNER JOIN CteProductLookup pl 
    ON pl.oid = rel.from_oid
    ) 
    INNER JOIN CteProductLookup pl2 
    ON pl2.oid = rel.to_oid
WHERE
    rel.Name = 'BundleItem' AND
    pl.ProductId = 'MX12345';

Is generating this error:

Msg 319, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

On execution only. There are no errors/warnings in the sql statement in the managment studio.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

always use with statement like ;WITH then you'll never get this error. The WITH command required a ; between it and any previous command, by always using ;WITH you'll never have to remember to do this.

see WITH common_table_expression (Transact-SQL), from the section Guidelines for Creating and Using Common Table Expressions:

When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...