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

前端 未结 4 1104
情歌与酒
情歌与酒 2020-11-29 08:10

The following query:

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


        
相关标签:
4条回答
  • 2020-11-29 08:25

    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.

    0 讨论(0)
  • 2020-11-29 08:36

    In some cases this also occurs if you have table hints and you have spaces between WITH clause and your hint, so best to type it like:

    SELECT Column1 FROM Table1 t1 WITH(NOLOCK)
    INNER JOIN Table2 t2 WITH(NOLOCK) ON t1.Column1 = t2.Column1
    

    And not:

    SELECT Column1 FROM Table1 t1 WITH (NOLOCK)
    INNER JOIN Table2 t2 WITH (NOLOCK) ON t1.Column1 = t2.Column1
    
    0 讨论(0)
  • 2020-11-29 08:38

    It should be legal to put a semicolon directly before the WITH keyword.

    0 讨论(0)
  • 2020-11-29 08:41
    ;WITH 
        CteProductLookup(ProductId, oid) 
        AS 
    ...
    
    0 讨论(0)
提交回复
热议问题