Groovy - 多表旋转到文件

时间:2013-06-12 21:42:45

标签: java sql groovy groovy-console

我正在运行一个多表SQL调用,它会启动到file.write。代码在SQuirreL中工作,甚至在Groovy中工作,直到我到达file.write(“”)行(代码的最后4行)。第一个错误消息我得到它“因为:ORA-00911:无效字符”,其余的与它类似。

这是我认为错误的位置:

file.write("")
db.eachRow(sql) {
    file.append(it.MOPID + "\t" + it.EOM + "\t" + it.Mopstart + "\n")

我认为我的代码存在的问题是,有多个表格,并且在撰写本文时,有些东西会被全部搞砸。非常感谢您的知识。

恭敬~Corpa Loom< 3

以下是完整的代码:

import groovy.sql.Sql

def db = Sql.newInstance(
    'jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:ORCL',
    'reporter', 'XXXXXXX', 'oracle.jdbc.XXXX.OracleDataSource')

def sql = """
DROP TABLE TEMPXMOP24;

CREATE TABLE TEMPXMOP24
(
   MOPID VARCHAR2(12),
   TYPE VARCHAR2(200),
   MOPSTART DATE,
   MOPAPPROVEDTIME DATE,
   TIME_DIF VARCHAR2(45),
   TIME_ORDER NUMBER(10),
   FIXED_MOP VARCHAR2(12),
   MOP_HR VARCHAR2(2)
);

INSERT INTO TEMPXMOP24 (MOPID,TYPE,MOPSTART,MOPAPPROVEDTIME,TIME_DIF,TIME_ORDER,FIXED_MOP,MOP_HR)
SELECT MOPACTIVITY.MOPID,
MOPACTIVITY.MOPSERVICEIMPACTED "TYPE",
MOPACTIVITY.MOPSTART,
MOPACTIVITY.MOPAPPROVEDTIME,
ROUND(MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME,2)||' days' "TIME_DIF",
ROUND(MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME,0) "TIME_ORDER",
 (CASE 
  WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 1, 6)||'00'||SUBSTR(MOPACTIVITY.MOPID, 9, 4)
  ELSE MOPACTIVITY.MOPID
 END ) FIXED_MOP,
(CASE 
  WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 7, 2)
  ELSE SUBSTR(MOPACTIVITY.MOPID, 7, 2)
 END ) MOP_HR 
FROM MOPUSER.MOPACTIVITY
WHERE MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME BETWEEN 0 and 14;

SELECT * FROM TEMPXMOP24
WHERE TRUNC(TO_DATE(FIXED_MOP, 'MMDDYYHH24MISS'))
  BETWEEN TRUNC(SYSDATE-90) AND TRUNC(SYSDATE)
ORDER BY "TIME_ORDER"
"""

def date = new Date() 
def dts = date.format("yyyy-MM-dd-HH-mm-ss") 
File file = new File('C:/OUTPUT/' +dts  + ' Past Due CM Mops.xls')

file.write("")
db.eachRow(sql) {
    file.append(it.MOPID + "\t" + it.TYPE + "\t" + it.MOPSTART + "\n")
}

2 个答案:

答案 0 :(得分:0)

我不确定你是否检查sql首先返回任何行。我弄乱你的SQL,结果证明是无效的。你现在编写sql的方式根本不会返回任何行,尽管它说它是有效的。

预期问题:
AND to_char(mopactivity.mopend, 'dd-mon-yyyy hh24:mi:') > '2013-05-28 06:01'

在上面这一行中,您试图比较两种不同格式的varchars,这些格式永远不会产生任何结果。

这是为您的sql量身定制的SQL Fiddle,格式正确。试试吧。

答案 1 :(得分:0)

这是你的修复Copra。上周末真的很高兴见到你。

import groovy.sql.Sql

def db = Sql.newInstance(
    'jdbc:oracle:thin:@10.10.47.193:1521:ORCL',
    'reporter', 'r3p0rt3r', 'oracle.jdbc.pool.OracleDataSource')

def sql = """
SELECT * FROM
(SELECT MOPACTIVITY.MOPID,
MOPACTIVITY.MOPSERVICEIMPACTED "TYPE",
MOPACTIVITY.MOPSTART,
MOPACTIVITY.MOPAPPROVEDTIME,
ROUND(MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME,2)||' days' "TIME_DIF",
ROUND(MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME,0) "TIME_ORDER",
 (CASE 
  WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 1, 6)||'00'||SUBSTR(MOPACTIVITY.MOPID, 9, 4)
  ELSE MOPACTIVITY.MOPID
 END ) FIXED_MOP,
(CASE 
  WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 7, 2)
  ELSE SUBSTR(MOPACTIVITY.MOPID, 7, 2)
 END ) MOP_HR 
FROM MOPUSER.MOPACTIVITY
WHERE MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME BETWEEN 0 and 14)
WHERE TRUNC(TO_DATE(FIXED_MOP, 'MMDDYYHH24MISS'))
  BETWEEN TRUNC(SYSDATE-90) AND TRUNC(SYSDATE)
ORDER BY "TIME_ORDER"
    """
def date = new Date() 
def dts = date.format("yyyy-MM-dd-HH-mm-ss") 
File file = new File('C:/Output/' + dts + ' EXPEDITE_MOPS.xls')
file.write("")
db.eachRow(sql) {
    file.append(it.MOPID + "\t" + it.TYPE + "\n")
}