如何自动化存储的查询并更新/存储存储查询的输出?

时间:2018-05-09 09:23:36

标签: oracle plsql compilation

此自动化的目标是在每行中运行几个select count(*)查询,并将结果更新/存储到与查询相关的行中。将结果存储到每一行后,它应该继续到下一行并一直到表的末尾。如果在任一行中没有选择查询,也应该考虑它。

例如,总共有4列: a)存储的查询(即从customers_savings中选择count(cust),其中save> ='';) b)存储另一个表的查询(即从customers_spendings中选择count(cust),其中支出> ='';) c)存储a的返回输出, d)存储b

的返回输出

如果有一个我可以密切关注的模板或指南,我将非常感激。 目前正在使用oracle plsql v17.4。

由于

1 个答案:

答案 0 :(得分:0)

创建表

你本可以提供这个;)

{
  "name": "[concat(parameters('virtualMachineName'),'/chocolatey')]",
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "location": "[resourceGroup().location]",
  "apiVersion": "2015-06-15",
  "dependsOn": [
    "[concat('Microsoft.DevTestLab/schedules/', 'shutdown-computevm-', parameters('virtualMachineName'))]"
  ],
  "properties": {
    "publisher": "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.9",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "fileUris": [
        "[concat(parameters('setupChocolatelyScriptLocation'),parameters('setupChocolateyScriptFileName'))]"
      ],
      "commandToExecute": "[concat('powershell -ExecutionPolicy bypass -File ', parameters('setupChocolateyScriptFileName'), ' -chocoPackages ',parameters('chocoPackages'))]"
    }
  }
},

抽样数据

你应该提供这些;)

CREATE TABLE TABLETEST
(
  QUERY1   VARCHAR2(2000 CHAR),
  RESULT1  NUMBER,
  QUERY2   VARCHAR2(2000 CHAR),
  RESULT2  NUMBER
);

运行查询

INSERT INTO TABLETEST VALUES('SELECT 1 FROM DUAL', 0, 'SELECT 2 FROM DUAL', 0);
INSERT INTO TABLETEST VALUES('SELECT 3 FROM DUAL', 0, 'SELECT 4 FROM DUAL', 0);

审核结果

declare
    tmpResult1 NUMBER;
    tmpResult2 NUMBER;
begin
    FOR i IN (SELECT query1, query2 FROM TABLETEST) -- loop through every row
    LOOP
        dbms_output.put_line('query1: "' || i.query1 || '"'); -- let's have a look, what we got back from our select
        dbms_output.put_line('query2: "' || i.query2 || '"');

        EXECUTE IMMEDIATE i.query1 INTO tmpResult1; -- executor out select-statement
        EXECUTE IMMEDIATE i.query2 INTO tmpResult2;

        dbms_output.put_line('tmpResult1: ' || tmpResult1); -- let's have a look at our results
        dbms_output.put_line('tmpResult2: ' || tmpResult2);  

        UPDATE TABLETEST SET RESULT1 = tmpResult1, RESULT2 = tmpResult2 WHERE QUERY1 = i.query1 AND QUERY2 = i.query2; -- Store the results into the table.
    END LOOP;
end;