在where子句中不存在时使用

时间:2019-01-11 07:56:58

标签: sql sql-server-2008

我得到Incorrect syntax near 'ctm' 以下是查询...

SELECT * FROM fit.dbo.CityTemplate ctm WHERE
NOT EXISTS (ctm.TemplateCode in (SELECT pit.TourCode
FROM sales97..City cy
Inner join sales97.dbo.Region rg ON rg.Region_Code = cy.Region_Code
Inner join presets.dbo.ProductItinerary pit ON pit.ProductCode =             
rg.Region_Code AND pit.ProductType = 'REGN'
WHERE isnull(pit.FrzInd,0) = 0  and isnull(rg.FrzInd,0) = 0  and     
isnull(rg.Showonwebind,0) = 1 
and (cy.Region_Code in ( SELECT * FROM Sales97.dbo.SplitString(@Where, ','))      
or cy.City_code = replace(replace(isnull(@Where,''),',',''),'cy_',''))))
OR (SELECT pit.TourCode FROM Sales97..Region rg
Inner join presets.dbo.ProductItinerary pit ON pit.ProductCode = 
rg.Region_Code AND pit.ProductType = 'REGN'
WHERE rg.Region_Code in ( SELECT * FROM Sales97.dbo.SplitString(@Where, 
',')))

如果查询1即'sales97..City未返回任何行,则我必须执行查询2即Sales97..Region

1 个答案:

答案 0 :(得分:1)

这是您发布的SQL,但格式有点:

SELECT 
    * 
FROM 
    fit.dbo.CityTemplate ctm 
WHERE
    NOT EXISTS (
        ctm.TemplateCode in (
            SELECT 
                pit.TourCode
            FROM 
                sales97..City cy
                Inner join sales97.dbo.Region rg ON rg.Region_Code = cy.Region_Code
                Inner join presets.dbo.ProductItinerary pit ON 
                    pit.ProductCode = rg.Region_Code AND 
                    pit.ProductType = 'REGN'
            WHERE 
                isnull(pit.FrzInd,0) = 0 and 
                isnull(rg.FrzInd,0) = 0  and     
                isnull(rg.Showonwebind,0) = 1 and 
                (
                    cy.Region_Code in (
                        SELECT 
                            * 
                        FROM 
                            Sales97.dbo.SplitString(@Where, ',')) or 
                    cy.City_code = replace(replace(isnull(@Where,''),',',''),'cy_','')))) OR 
    (
        SELECT 
            pit.TourCode 
        FROM 
            Sales97..Region rg
            Inner join presets.dbo.ProductItinerary pit ON 
                pit.ProductCode = rg.Region_Code AND 
                pit.ProductType = 'REGN'
        WHERE 
            rg.Region_Code in (SELECT * FROM Sales97.dbo.SplitString(@Where, ',')))

这里有几个问题:

  • 您正在使用NOT EXISTS,而没有将ctm表与相关查询链接(我假设您希望它与之相关)。因此,您想在子查询中的任何地方通过ctm.TemplateCode进行过滤。

    SELECT 
        pit.TourCode
    FROM 
        sales97..City cy
        Inner join sales97.dbo.Region rg ON rg.Region_Code = cy.Region_Code
        Inner join presets.dbo.ProductItinerary pit ON 
            pit.ProductCode = rg.Region_Code AND 
            pit.ProductType = 'REGN'
    WHERE
    
        pit.TourCode = ctm.TemplateCode AND -- Here
    
        isnull(pit.FrzInd,0) = 0 and 
        isnull(rg.FrzInd,0) = 0  and     
        isnull(rg.Showonwebind,0) = 1 and 
        (
            cy.Region_Code in (
                SELECT 
                    * 
                FROM 
                    Sales97.dbo.SplitString(@Where, ',')) or 
            cy.City_code = replace(replace(isnull(@Where,''),',',''),'cy_','')))
    
  • NOT EXISTS的开头存在语法错误。该运算符需要一个结果集(也称为SELECT),不能使用ctm.TemplateCode IN (...)。此错误也在第二个子查询上重复。正确的方法如下:

    SELECT 
        * 
    FROM 
        fit.dbo.CityTemplate ctm 
    WHERE
        NOT EXISTS (
            SELECT 
                ...
            FROM 
                SomeTable T
            WHERE
                T.Field = ctm.Field AND /*Link outmost table with inner tables so it's correlated!*/
                ...)
    

请尝试使用此固定的SQL,尽管我假设您可能需要检查一些内容以使其适合您在此查询中查找的内容:

SELECT 
    * 
FROM 
    fit.dbo.CityTemplate ctm 
WHERE
    NOT EXISTS (
        SELECT 
            pit.TourCode
        FROM 
            sales97..City cy
            Inner join sales97.dbo.Region rg ON rg.Region_Code = cy.Region_Code
            Inner join presets.dbo.ProductItinerary pit ON 
                pit.ProductCode = rg.Region_Code AND 
                pit.ProductType = 'REGN'
        WHERE
            ctm.TemplateCode = pit.TourCode AND -- Link ctm to pit
            isnull(pit.FrzInd,0) = 0 and 
            isnull(rg.FrzInd,0) = 0  and     
            isnull(rg.Showonwebind,0) = 1 and 
            (
                cy.Region_Code in (
                    SELECT 
                        * 
                    FROM 
                        Sales97.dbo.SplitString(@Where, ',')) or 
                cy.City_code = replace(replace(isnull(@Where,''),',',''),'cy_',''))) OR 

    NOT EXISTS ( -- Repeat the NOT EXISTS operator
        SELECT 
            pit.TourCode 
        FROM 
            Sales97..Region rg
            Inner join presets.dbo.ProductItinerary pit ON 
                pit.ProductCode = rg.Region_Code AND 
                pit.ProductType = 'REGN'
        WHERE
            ctm.TemplateCode = pit.TourCode AND  -- Link ctm to pit
            rg.Region_Code in (SELECT * FROM Sales97.dbo.SplitString(@Where, ',')))