开发者

How do you design queries or find errors in SQL Server?

开发者 https://www.devze.com 2023-02-19 22:07 出处:网络
I wonder how other people design SQL queries in SQL Server. I\'m using only the \"design-query editor\" but sometimes I think I\'m missing something.

I wonder how other people design SQL queries in SQL Server. I'm using only the "design-query editor" but sometimes I think I'm missing something.

For example following weird query that simply returns some ID's that match given criteria (it's a mandatory spare part matrix, if sp1 is changed then sp2 must be changed also). I'm open for improvements, but my main problem is that I've set a brace too much or too little and SQL Server only comments it with a "Wrong Syntax near ')' in line 359 (last line)".

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 = 7开发者_如何学Python75) 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')))

Thank you in advance for any suggestions.


Get SSMS 2008, then whenever it gives you an error, double click on it to go to the line. Keep adding ) (closing) until it no longer highlights it (which signifies a match to prior unmatched ().

As for How do you find/avoid such bugs in your queries?, maintain a good formatting structure, so that the brackets immediately show up when unmatched.

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'))))))))))


I follow three rules when writing such complex queries:

  1. when opening a brace immediatly write the closing brace and what you wanted to write in between them
  2. whenever possible create a view so you avoid having endless queries and instead use the view with the right parameters (in your case it would be a view for the over and over repeating subselects)
  3. write the query step by step and execute it everytime and check if the results are what you expect them to be
0

精彩评论

暂无评论...
验证码 换一张
取 消