Как вы разрабатываете запросы или находите ошибки в SQL Server? - PullRequest
2 голосов
/ 25 марта 2011

Интересно, как другие люди конструируют SQL-запросы в SQL Server.Я использую только «редактор дизайна запросов», но иногда мне кажется, что я что-то упустил.

Например, следующий странный запрос, который просто возвращает некоторые идентификаторы, которые соответствуют заданным критериям (это обязательная матрица запасных частей, если sp1 изменяется, то sp2 также должен быть изменен).Я открыт для улучшений, но моя главная проблема заключается в том, что я установил слишком много или слишком мало скобок, а SQL Server комментирует его только с «неправильным синтаксисом рядом») в строке 359 (последняя строка) ».

Как вы находите / избегаете таких ошибок в своих запросах?

SELECT  idData FROM tabData
WHERE     (fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/402-22720-0002', 
                                   'A/402-22710-0003', 'A/402-22720-0001', 'A/402-22710-0001'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0004', 
                                   'A/415-22710-0011', 'A/415-22710-0031')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/402-22720-0002', 
                                   'A/402-22710-0003', 'A/402-22720-0001', 'A/402-22710-0001'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/403-22710-0001'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                   'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0009'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                   'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                 'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/321-0000-00292')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/321-0000-00292')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/404-22710-0009', 
                                   'A/404-22710-0003', 'A/404-22710-0008', 'A/404-22710-0007', 'A/404-22710-0075', 'A/404-22710-0077', 'A/404-22710-0079'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0005')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND
       (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND
       (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028'))))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0003','A/401-22710-0005','A/401-22710-0004','A/401-22710-0001','A/401-22710-0013','A/401-22710-0014','A/401-22710-0015'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0003','A/401-22710-0005','A/401-22710-0004','A/401-22710-0001','A/401-22710-0013','A/401-22710-0014','A/401-22710-0015'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1204-8457')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1204-8457')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0027')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/412-00000-0010')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0009')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0009')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0007')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0003')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1230-9478')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1222-5301')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR (fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0015')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005')))

Заранее благодарим вас за любые предложения.

Ответы [ 2 ]

2 голосов
/ 25 марта 2011

Получите SSMS 2008, затем всякий раз, когда он выдает ошибку, дважды щелкните по нему, чтобы перейти к строке.Продолжайте добавлять ) (закрытие) до тех пор, пока он больше не будет выделен (что означает совпадение с предыдущим несопоставленным ().

Что касается How do you find/avoid such bugs in your queries?, сохраняйте хорошую структуру форматирования, так что скобки сразу появляются, когда они не совпадают.

SELECT  idData FROM tabData
WHERE     (fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/402-22720-0002', 
                                   'A/402-22710-0003', 'A/402-22720-0001', 'A/402-22710-0001'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0004', 
                                   'A/415-22710-0011', 'A/415-22710-0031')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/402-22720-0002', 
                                   'A/402-22710-0003', 'A/402-22720-0001', 'A/402-22710-0001'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/403-22710-0001'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                   'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0009'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                   'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0006', 
                                 'A/401-22710-0002'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/321-0000-00292')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/321-0000-00292')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/404-22710-0009', 
                                   'A/404-22710-0003', 'A/404-22710-0008', 'A/404-22710-0007', 'A/404-22710-0075', 'A/404-22710-0077', 'A/404-22710-0079'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0005')) AND 
      (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028')))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND
       (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))) 
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND
       (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028'))))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0003','A/401-22710-0005','A/401-22710-0004','A/401-22710-0001','A/401-22710-0013','A/401-22710-0014','A/401-22710-0015'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013', 
                                   'A/415-22710-0010', 'A/415-22710-0028'))))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/8CS-22710-0004')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName IN ('A/401-22710-0003','A/401-22710-0005','A/401-22710-0004','A/401-22710-0001','A/401-22710-0013','A/401-22710-0014','A/401-22710-0015'))) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1204-8457')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1204-8457')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0027')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028'))))))))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/412-00000-0010')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0009')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName IN ('A/415-22710-0013','A/415-22710-0010','A/415-22710-0028')))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0009')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0007')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0003')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1230-9478')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR(fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = '1222-5301')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))
OR (fiModel = 775) AND EXISTS(SELECT     tabDataDetail_5.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_5 INNER JOIN
                                   tabSparePart AS tabSparePart_5 ON tabDataDetail_5.fiSparePart = tabSparePart_5.idSparePart
            WHERE      (tabData.idData = tabDataDetail_5.fiData) AND (tabDataDetail_5.Material_Quantity >= 1) AND (tabSparePart_5.SparePartName = 'A/415-22710-0015')) AND (NOT EXISTS
          (SELECT     tabDataDetail_3.idDataDetail
            FROM          tabDataDetail AS tabDataDetail_3 INNER JOIN
                                   tabSparePart AS tabSparePart_3 ON tabDataDetail_3.fiSparePart = tabSparePart_3.idSparePart
            WHERE      (tabData.idData = tabDataDetail_3.fiData) AND (tabDataDetail_3.Material_Quantity >= 1) AND (tabSparePart_3.SparePartName = 'A/415-22710-0005'))))))))))
1 голос
/ 25 марта 2011

При написании таких сложных запросов я придерживаюсь трех правил:

  1. при открытии фигурной скобки немедленно напишите закрывающую фигурную скобку и то, что вы хотели написать между ними
  2. по возможности создавайте представление, чтобы избежать бесконечных запросов и вместо этого использовать представление с правильными параметрами (в вашем случае это будет представление для многократно повторяющихся подвыборов)
  3. напишите запрос шаг за шагом, выполняйте его каждый раз и проверяйте, соответствуют ли результаты ожидаемым
...