0

我正在运行一个多表 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")
}
4

2 回答 2

0

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

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

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

这是为您的 sql 量身定制的具有正确格式的SQL Fiddle 。试试看。

于 2013-06-13T01:54:24.593 回答
0

这是你的修复椰子干。上周末见到你真是太好了。

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")
}
于 2013-06-17T13:58:35.983 回答