我想编写sql脚本以插入多个记录

时间:2019-06-19 16:04:49

标签: sql postgresql

我已经写了要插入一个用例的sql代码,但是我想重用它插入多个用例。我知道在sql中查询json很麻烦,但是我在这里别无选择。由于要基于showErrorsTab true过滤数组元素,因此我正在读取searchTableColumns属性并构造临时表。

样本json:

{
          "canPlaceOnHold": true,
          "displayMyUploads": true,
          "canSelectMultipleTask": true,
          "displayErrorTasks": false,
          "displaySummaryTasks": true,
          "searchTableColumns": [{
                "columnName": "",
                "isProcessVariable": false,
                "propertyName": ["checkBox"],
                "dataType": "select",
                "sortable": false
          }, {
                "columnName": "",
                "isProcessVariable": true,
                "propertyName": ["returnStatus"],
                "dataType": "icon",
                "sortable": false
          }, {
                "columnName": "Name",
                "isProcessVariable": true,
                "propertyName": ["fileName"],
                "dataType": "Link",
                "sortable": true
          }, {
                "columnName": "Assignee",
                "isProcessVariable": false,
                "propertyName": ["assignee"],
                "dataType": "String",
                "sortable": true
          }, {
                "columnName": "Type",
                "isProcessVariable": false,
                "propertyName": ["processType"],
                "dataType": "String",
                "sortable": true
          }, {
                "columnName": "Status",
                "isProcessVariable": false,
                "propertyName": ["name"],
                "dataType": "String",
                "sortable": true
          }, {
                "columnName": "Upload Date",
                "isProcessVariable": false,
                "propertyName": ["startTime"],
                "dataType": "Date",
                "sortable": true
          }, {
                "columnName": "Status Date",
                "isProcessVariable": false,
                "propertyName": ["createTime"],
                "dataType": "Date",
                "sortable": true
          }]
      }

sql脚本:

DO $$
    DECLARE
      _featureIdentifier  INTEGER;
      _featureId          INTEGER;
      _searchColumnLabelsPropertyValue      CHARACTER VARYING;
      _errorColumnLabelsPropertyValue      CHARACTER VARYING;
    BEGIN
      FOR _featureIdentifier IN SELECT feature_id FROM features WHERE feature_name IN ('Lease Default Feature', 'Mortgage Default Feature', 'HUD Default Feature')
      LOOP
        with feature as (SELECT feature_id INTO _featureId FROM features WHERE feature_id = _featureIdentifier),
        propertyValue AS (SELECT property_value FROM feature_properties WHERE feature_id = _featureId AND property_name = 'TaskPropertiesConfiguration'),
        allColumns AS (SELECT json_array_elements(propertyValue.property_value :: json -> 'searchTableColumns') AS jsonColumns FROM propertyValue),
        notNullColumns AS (SELECT * FROM allColumns WHERE jsonColumns :: json->> 'columnName' != ''),
        searchTasksColumns AS (SELECT * FROM notNullColumns WHERE (jsonColumns :: json->>'showErrorsTab') :: boolean = false),
        errorTasksColumns AS (SELECT * FROM notNullColumns WHERE (jsonColumns :: json->>'showErrorsTab') :: boolean = true),
        columnLabels AS (SELECT jsonColumns :: json->>'columnName' AS labels, (jsonColumns :: json->>'propertyName') :: json->> 0 AS property_names FROM searchTasksColumns),
        errorColumnLabels AS (SELECT jsonColumns :: json->>'columnName' AS labels, (jsonColumns :: json->>'propertyName') :: json->> 0 AS property_names FROM errorTasksColumns),
        searchTasksPropertyValue as (select string_agg(concat_ws('=', property_names, labels), ',') as property_value
        from columnLabels),
        errorTasksPropertyValue as (select string_agg(concat_ws('=', property_names, labels), ',') as property_value
        from errorColumnLabels)
        select property_value from searchTasksPropertyValue;
        select property_value into _searchColumnLabelsPropertyValue from searchTasksPropertyValue;
        IF(_searchColumnLabelsPropertyValue != '') THEN
        INSERT INTO feature_properties(feature_id, property_value, property_name, is_editable, property_label, is_hidden)
        SELECT _featureId,
            _searchColumnLabelsPropertyValue,
            'TasksColumnHeadersConfiguration',
            true,
            'Configure Tasks Headers',
            false;
        END IF;
      IF(_errorColumnLabelsPropertyValue != '') THEN
      INSERT INTO feature_properties(feature_id, property_value, property_name, is_editable, property_label, is_hidden)
          SELECT _featureId,
          _errorColumnLabelsPropertyValue,
          'ErrorTasksColumnHeadersConfiguration',
          true,
          'Configure Error Tasks Headers',
          false;
      END IF;
    END LOOP;
    END;
    $$;

这是我要尝试的事情

工作脚本:

with feature as (SELECT feature_id FROM features WHERE feature_name = 'Mortgage Default Feature'),
         propertyValue as (select property_value
                           from feature_properties
                           where feature_id = (select feature_id from feature)
                             and property_name = 'TaskPropertiesConfiguration'),
         allColumns as (select json_array_elements(
                                 propertyValue.property_value :: json -> 'searchTableColumns') as jsonColumns
                        from propertyValue),
         notNullColumns as (select * from allColumns where jsonColumns :: json->> 'columnName' != ''),
         searchTasksColumns as (select *
                                from notNullColumns
                                where (jsonColumns :: json->>'showErrorsTab') :: boolean = false),
         errorTasksColumns as (select *
                               from notNullColumns
                               where (jsonColumns :: json->>'showErrorsTab') :: boolean = true),
         columnLabels as (select jsonColumns :: json->>'columnName'                  as labels,
                                 (jsonColumns :: json->>'propertyName') :: json->> 0 as property_names
                          from searchTasksColumns),
         errorColumnLabels as (select jsonColumns :: json->>'columnName'                  as labels,
                                      (jsonColumns :: json->>'propertyName') :: json->> 0 as property_names
                               from errorTasksColumns),
         searchTasksPropertyValue as (select string_agg(concat_ws('=', property_names, labels), ',') into property_value
                                      from columnLabels),
         errorTasksPropertyValue as (select string_agg(concat_ws('=', property_names, labels), ',') as property_value
                                     from errorColumnLabels)
    INSERT INTO feature_properties (feature_id, property_value, property_name, is_editable, property_label, is_hidden)
    VALUES ((SELECT feature_id FROM features WHERE feature_name = 'Mortgage Default Feature'),
            (select property_value from searchTasksPropertyValue),
            'TasksColumnHeadersConfiguration',
            true,
            'Configure Tasks Headers',
            false),
           ((SELECT feature_id FROM features WHERE feature_name = 'Mortgage Default Feature'),
            (select property_value from errorTasksPropertyValue),
            'ErrorTasksColumnHeadersConfiguration',
            true,
            'Configure Error Tasks Headers',
            false);

这已经适用于一个用例。我无法将汇总的字符串结果存储到临时变量中。我收到的错误是:

  

[42601] ERROR: INTO specified more than once at or near "into"

0 个答案:

没有答案