当Optionals被注释掉时,SQL返回更多结果

时间:2018-03-23 11:22:57

标签: sql tsql

这里有一个sql的情况,我正在为一个存储过程更改一段sql,我已经写了下面到目前为止的内容,我现在正在打一堵墙,

当我运行查询AS时,它将返回19个结果,注释掉作为可选模块显示的所有内容,并返回31个结果, 这背后的想法是基本上从xml文件读取,确定文件中的值,然后匹配我们在这个sql中的值,所以它会说a如果是,则为1,如果b为否则为0, 所以它是许可的,或者没有许可的情况,

但我不认为它的功能应该如此。

   -- Grab our license
 DECLARE @xml XML
 SELECT TOP 1 @xml = CAST(LicenceKey AS xml) FROM Organisation

 -- Calculate our licensed modules
 DECLARE @LicensedModules TABLE (moduleid INT)

 Declare @optionalmodules table (moduleid INT,description varchar (100))

 INSERT INTO @optionalmodules (moduleid, description) values (1,'R9')
 INSERT INTO @optionalmodules (moduleid, description) values (2,'S8')
 INSERT INTO @optionalmodules (moduleid, description) values (6,'S7')
 INSERT INTO @optionalmodules (moduleid, description) values (8,'A6')
 INSERT INTO @optionalmodules (moduleid, description) values (9,'C5')
 INSERT INTO @optionalmodules (moduleid, description) values (10,'S4')
 INSERT INTO @optionalmodules (moduleid, description) values (11,'A2')
 INSERT INTO @optionalmodules (moduleid, description) values (12,'P4')
 INSERT INTO @optionalmodules (moduleid, description) values (13,'PSL')
 INSERT INTO @optionalmodules (moduleid, description) values (14,'H4')
 INSERT INTO @optionalmodules (moduleid, description) values (15,'F2')
 INSERT INTO @optionalmodules (moduleid, description) values (16,'C1')
 INSERT INTO @optionalmodules (moduleid, description) values (17,'H2')
 INSERT INTO @optionalmodules (moduleid, description) values (18,'P3')
 INSERT INTO @optionalmodules (moduleid, description) values (20,'G1')
 INSERT INTO @optionalmodules (moduleid, description) values (21,'E2')
 INSERT INTO @optionalmodules (moduleid, description) values (22,'R1')
 INSERT INTO @optionalmodules (moduleid, description) values (23,'H1')
 INSERT INTO @optionalmodules (moduleid, description) values (24,'S2')
 INSERT INTO @optionalmodules (moduleid, description) values (25,'i1')
 INSERT INTO @optionalmodules (moduleid, description) values (26,'G1')
 INSERT INTO @optionalmodules (moduleid, description) values (27,'S1')
 INSERT INTO @optionalmodules (moduleid, description) values (28,'E1')
 INSERT INTO @optionalmodules (moduleid, description) values (29,'F1')
 INSERT INTO @optionalmodules (moduleid, description) values (31,'T2')
 INSERT INTO @optionalmodules (moduleid, description) values (32,'A1')
 INSERT INTO @optionalmodules (moduleid, description) values (33,'P2')
 INSERT INTO @optionalmodules (moduleid, description) values (16016,'P1')


INSERT INTO @LicensedModules

    SELECT CAST(sl.LookupReference AS INT)--,
        --sl.LookupReference, 
        --sl.Description, 
        --m.*,
        --cast(case license.value('.', 'varchar(3)') when 'Yes' then 1 else 0 end as bit) as optModLicenced,
        --case WHEN m.moduleid IS NULL THEN 1 ELSE cast(case license.value('.', 'varchar(3)') when 'Yes' then 1 else 0 end as bit) END AS isLicenced
    FROM SystemLookup sl
        LEFT JOIN @optionalmodules m ON sl.LookupReference = m.moduleid 
        Left Join @xml.nodes('//Modules/*') as organisation(license) on m.description = organisation.license.value('local-name(.)', 'varchar(50)')
    WHERE sl.LookupTypeId = 1 
        AND case WHEN m.moduleid IS NULL THEN 1 ELSE cast(case license.value('.', 'varchar(3)') when 'Yes' then 1 else 0 end as bit) END = 1

Select * FROM @LicensedModules

1 个答案:

答案 0 :(得分:0)

似乎在WHERE子句中,如果@optionalmodules不存在或不匹配,那么它将始终包含该行(CASE语句中的m.moduleid IS NULL)。

如果@optionalmodules表的值与sl.LookupReference匹配,那么它只包含许可证为“是”的项目,这可能会导致返回的行数减少。