Oracle PL / SQL使用WITH子句选择变量

时间:2016-05-03 15:28:10

标签: oracle variables plsql select-into with-clause

我有一个WITH子句,它给了我想要的结果,并且我试图把它放在一个变量中。为简单起见,我省略了代码。最后一行代码是最相关的。

WITH ALL_VE_ERRORS AS (

        SELECT *
        FROM ASN.AN_VALIDATION_ERRORS
        WHERE ...

    ), FILTER_STATUS AS (

        SELECT *
        FROM ALL_VE_ERRORS
        WHERE ...

      ) SELECT UNIT_DISCREPANCY FROM FILTER_STATUS INTO W_UNIT_DISCREPANCY; <-- like this

但这不起作用,编译器并不喜欢这样。我也试过把它放在第一位:

SELECT INTO W_UNIT_DISCREPANCY <-- or like this
       WITH ALL_VE_ERRORS AS (...

任何人都知道做这样的事情的正确语法吗?

1 个答案:

答案 0 :(得分:2)

如果您没有理由我无法看到,您的def compare_DB_Data(DB_Detail1=[], DB_Detail2=[]): ''' This method collects the data from both the given db and then compares the data If any difference found, it appends the same into a file for post-verification ''' get_db_data(Instance_name=DB_Detail1[0], DB_name=DB_Detail1[1]) get_db_data(Instance_name=DB_Detail2[0], DB_name=DB_Detail2[1]) data_DB1 = "C:\\SQL_Data\\DB_Data-%s_Output.txt" %(DB_Detail1[1]) data_DB2 = "C:\\SQL_Data\\DB_Data-%s_Output.txt" %(DB_Detail2[1]) data_difference = "C:\\SQL_Data\\Data-Difference-%s-vs-%s_Output.txt" %(DB_Detail1[1], DB_Detail2[1]) with open(data_difference, 'w') as f3: f3.write("Data Difference in %s vs %s \n" %(DB_Detail1[1], DB_Detail2[1])) f3.write("-"*100 + '\n') f3.write(DB_Detail1[1]) f3.write('\t\t\t\t\t\t\t') f3.write(DB_Detail2[1] + '\n') f3.write("-"*100 + '\n') data_diff =0 with open(data_DB1, 'r') as f1, open(data_DB2, 'r') as f2: for line_f1, line_f2 in zip(f1, f2): if line_f1 != line_f2: data_diff = 1 # Flag set if any difference found with open(data_difference, 'a') as f3: f3.write(line_f1.strip() + '\t\t\t\t\t\t\t' + line_f2.strip()) if data_diff: print "Data of both DB Varies" else: print "Data of both DB Matches" 条款中不需要两个表格;你可以用这种方式简化它:

WITH

否则,您可以使用:

WITH ALL_VE_ERRORS AS (    
    SELECT *
    FROM ASN.AN_VALIDATION_ERRORS
    WHERE ...    
)
SELECT UNIT_DISCREPANCY
INTO W_UNIT_DISCREPANCY
FROM ALL_VE_ERRORS
WHERE ...