在上下文中指定的非布尔类型的表达式,其中预期条件接近 ')'

Expression of non boolean type specified on context where condition is expected near ')'

我使用 SQL Server 2012 我收到此错误:

Expression of non boolean type specified on context where condition is expected near ')'

我认为这是因为它无法处理 'N/A' OR Null 或 Text。

如何修改动态 SQL 以接受 N/A AND NULL?

请问如何解决这个问题?

我的示例数据

CREATE TABLE #Allfeatures
(
    ZPLID INT,
    ZFeatureKey nvarchar(20),
    IsNumericValue int
) 

INSERT INTO #Allfeatures (ZPLID, ZFeatureKey, IsNumericValue)
VALUES (75533, '1505730036', 0)
                    
CREATE TABLE #Condition
(
    Code nvarchar(20),
    ZFeatureKey nvarchar(20),
    ZfeatureType nvarchar(20),
    EStrat  nvarchar(20),
    EEnd NVARCHAR(10)
)

INSERT INTO #Condition (Code, ZFeatureKey, ZfeatureType, EStrat, EEnd)
VALUES ('8535400000', '1505730036', NULL, '>1000', ' '),
       ('8535400000', '1505730036', NULL, '>280AV', ' '),
       ('8535400000', '1505730036', NULL, 'N/A', ' '),
       ('8535400000', '1505730036', NULL, NULL, ' ')
                    
CREATE TABLE #PartAttributes
(
    PartID INT,
    ZFeaturekEY NVARCHAR(20),
    AcceptedValuesOption_Value INT,
    Name nvarchar(20)
)

INSERT INTO #PartAttributes (PartID, ZFeaturekEY, AcceptedValuesOption_Value, Name)
VALUES (4977941, 1505730036, 280, '280VDC'),
       (4977936, 1505730036, 280, '280VDC'),
       (4977935, 1505730036, 280, '280VDC')
                      
DECLARE @Sql nvarchar(max)
DECLARE @ConStr nvarchar(max)
                                
SET @ConStr = STUFF((SELECT CONCAT(' Or (PM.ZfeatureKey= ', CC.ZfeatureKey , IIF(CC.ZfeatureType='Qualifications',' And AcceptedValuesOption_Value ' , ' And translate(Name, ''VDCA'', space(4)) ' ) , CAST(
                        
                           cast(LEFT(SUBSTRING(EStrat, PATINDEX('%[<>0-9.-]%', EStrat), 2500),
             PATINDEX('%[^<>0-9.-]%', SUBSTRING(EStrat, PATINDEX('%[<>0-9.-]%', EStrat), 2500) + 'X') -1) as nvarchar(2500))
                  --EStrat 
                  AS NVARCHAR(2500)), ')')   
                        
                  FROM #Condition CC INNER JOIN #Allfeatures AL ON AL.ZfeatureKey = CC.ZfeatureKey AND AL.IsNumericValue =0
                  where EStrat <> 'N/A'
                  FOR XML PATH(''), TYPE).value('(./text())[1]','varchar(max)'),1,3,'')
     
  ----------------                    
  SET @Sql= CONCAT(' SELECT PartID, Code, Count(1) as ConCount
  FROM #PartAttributes PM 
  INNER JOIN #Condition Co ON Co.ZfeatureKey = PM.ZfeatureKey ',
  'Where 1=1 and (', @ConStr, ' ) Group By PartID,Code ' ,
  ' Having Count(1)> = ',(SELECT COUNT(1) FROM #Condition))
  EXEC (@SQL)

TRANSLATE 是在 SQL Server 2017

中引入的

here 所解释的那样创建一个替代函数或类似这样的函数:

USE master
CREATE FUNCTION dbo.TRANSLATE
(
    @inputString NVARCHAR(MAX),
    @characters NVARCHAR(MAX),
    @translations NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @pos INT = 0;
    DECLARE @len INT = LEN(@characters)
    DECLARE @result NVARCHAR(MAX) = @inputString
    WHILE (@pos < @len)
    BEGIN
        SET @pos += 1
        SET @result = REPLACE(@result,SUBSTRING(@characters,@pos,1),SUBSTRING(@translations,@pos,1))
    END
    RETURN @result

END

要处理 'N/A' 或 NULL 文本,展开

translate(Name, ''VDCA'', space(4))

part with(已更改为该用户定义的函数)

CAST(LTRIM(RTRIM(ISNULL(master.dbo.translate(CASE WHEN Name = ''N/A'' THEN ''0'' ELSE Name END, ''VDCA'', space(4)),''0''))) AS INT)

以下看起来也不太正确(很确定您可以自己解决):

' Having Count(1)> = '

要进一步调试您的代码,请将 EXEC (@SQL) 更改为 PRINT @SQL 以查看实际执行的内容。