0

我需要执行一次性脚本(也就是说,它不会部署在活系统中)来更正现有表中的一些值。

脚本本身不相关。这里的问题是“一些值”部分:要调整的数据(键和旧值和新值)是在电子表格中收集的。假设它有很多行,比如 4 或 5 列。

在 MSSQL Server 术语中,我将创建一个临时表来保存输入值,并在将电子表格导出为 CSV 后将它们全部输入(加上一些文本转换,可能使用 sed(1) 或 paste(1) unix shell 命令):

CREATE TABLE #inputdata (col1 AS INTEGER, col2 AS VARCHAR ...);

INSERT INTO #inputdata VALUES (...);
...
INSERT INTO #inputdata VALUES (...);

-- then comes the script which can SELECT FROM #inputdata
-- just like a normal table

我需要在 Oracle 中执行此操作。

Oracle 临时表似乎只是数据方面的临时表,我的意思是,它们在使用之前应该已经存在于数据库中。我没有,也永远不会拥有创建它的必要权限;SQLServer 允许创建临时表,而不管普通的表创建权限如何。

有任何想法吗?

4

1 回答 1

0

我已经用 WITH 子句解决了这个问题:

WITH inputdata AS (
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    ...
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL
)
SELECT ...
FROM .... INNER JOIN inputdata ON ...
WHERE inputdata...

在我的脚本中,只涉及 SELECT 和 INSERT。

对于 INSERT,语法变为:

INSERT INTO tableX (...)
WITH inputdata AS (
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    ...
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL UNION ALL
    SELECT  ... AS col1, ... AS col2, ... FROM DUAL
)
SELECT ...
FROM .... INNER JOIN inputdata ON ...
WHERE inputdata...
于 2014-06-12T13:24:48.807 回答