SQL相关 - 代码修改请求和建议

时间:2017-06-15 07:36:10

标签: sql sql-server sql-like

这里有新手!我有一些SQL代码,其中有许多子选择充当规则。现在我使用LIKE运算符有许多嵌套的WHERE子句,但我想知道是否有一种方法来合并代码或合并条件。

SELECT   TOP 1 [ID]
         FROM [forecast].[dbo].[forecast_tool_product_identifiers]
         WHERE [macro_products_id] = '3' -- Macro Prouduct Dropdown
         AND ISNULL([source_attribute_1],' ') LIKE                    

         (SELECT 
         [col1]-- First RULE value
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001' -- Current rule Template
         AND [rule_sequence] = '1') -- RULE sequence number (will be looped)
AND ISNULL([source_attribute_2],' ') LIKE 
         (SELECT 
         [col2]-- Second RULE value - etc.
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_3],' ') LIKE 
         (SELECT 
         [col3]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_4],' ') LIKE 
         (SELECT 
         [col4]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_5],' ') LIKE 
         (SELECT 
         [col5]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_6],' ') LIKE 
         (SELECT 
         [col6]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_7],' ') LIKE 
         (SELECT DISTINCT
         [col7]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_8],' ') LIKE 
         (SELECT 
         [col8]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_9],' ') LIKE 
         (SELECT 
         [col9]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_10],' ') LIKE 
         (SELECT 
         [col10]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_11],' ') LIKE 
         (SELECT 
         [col11]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1')
AND ISNULL([source_attribute_12],' ') LIKE 
         (SELECT 
         [col12]
         FROM [forecast].[dbo].[forecast_tool_rule_templates]
         WHERE [template_name] = 'testADI001'
         AND [rule_sequence] = '1') AND [ID] -- Application Drop Down values below
      IN 
      (SELECT [ID]
      FROM [forecast].[dbo].[forecast_tool_product_identifiers]
      WHERE [macro_products_id] = '3' AND
      ISNULL([source_attribute_1],' ') LIKE '%' AND
      ISNULL([source_attribute_2],' ') LIKE '%' AND
      ISNULL([source_attribute_3],' ') LIKE '%' AND
      ISNULL([source_attribute_4],' ') LIKE '%' AND
      ISNULL([source_attribute_5],' ') LIKE '%' AND --  ETH-10GE (TEST VALUE)
      ISNULL([source_attribute_6],' ') LIKE '%' AND
      ISNULL([source_attribute_7],' ') LIKE 'BLTMMDCH' AND --  NYCMNY54 (TEST VALUE)
      ISNULL([source_attribute_8],' ') LIKE '%' AND
      ISNULL([source_attribute_9],' ') LIKE '%' AND --  AGSTMEST (TEST VALUE)
      ISNULL([source_attribute_10],' ') LIKE '%' AND
      ISNULL([source_attribute_11],' ') LIKE '%' AND
      ISNULL([source_attribute_12],' ') LIKE '%');--  KT (TEST VALUE)

提前谢谢!

1 个答案:

答案 0 :(得分:0)

看起来可以简化

$scope.myFunction = function(name) {
    if(typeof $scope[name] === 'function') {
        $scope[name]();
    }
}