我有一个表t2,其中包含表名和列名。我在sql文件中编写了plsql块,使用shell为每个表动态生成脚本。表(AAA)中有一列有250列,因此我我得到错误输入太长。打印标题时出现此错误。
示例数据
create table t2 (table_nm varchar2(10), col_nm varchar2(10));
DECLARE
i number := 0;
BEGIN
loop
insert into t2 values ('AAA','COLUMN_'||i);
i:= i+1;
EXIT WHEN I=250;
commit;
END LOOP;
end;
/
sql.file。
SET termout OFF
SET SERVEROUTPUT ON
SET echo OFF
SET feedback OFF
SET timing OFF
spool v_out.sql
DECLARE
CURSOR c1
IS
SELECT rnm,
cnt,
tbl_col_nm,
col_nm,
table_nm,
LVL_NBR
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY EXTR.TABLE_NM ORDER BY EXTR.COL_NM) AS RNM,
COUNT(EXTr.COL_NM) OVER (PARTITION BY EXTR.TABLE_NM ORDER BY EXTR.TABLE_NM) AS CNT,
EXTR.TABLE_NM
||'.'
||extr.COL_NM AS TBL_COL_NM,
extr.col_nm col_nm,
extr.table_nm,
1 as lvl_nbr
FROM T2 EXTR
WHERE TABLE_NM = 'AAA'
)
ORDER BY lvl_nbr DESC,
table_nm,
rnm;
v_col_list VARCHAR2(4000) := '';
v_header_select VARCHAR2(4000) := '';
v_sql VARCHAR2(32767) := '';
v_from_clause VARCHAR2(4000) := '';
v_table_nm VARCHAR2(100) := '';
v_rec_count NUMBER := 0;
err_code VARCHAR2(100);
err_msg VARCHAR2(200);
BEGIN
FOR i IN c1
LOOP
IF i.rnm = 1 THEN
DBMS_OUTPUT.PUT_LINE('set colsep ,');
DBMS_OUTPUT.PUT_LINE('set pagesize 0');
DBMS_OUTPUT.PUT_LINE('set trimspool on');
DBMS_OUTPUT.PUT_LINE('set headsep off');
DBMS_OUTPUT.PUT_LINE('set feedback off');
DBMS_OUTPUT.PUT_LINE('set echo off');
DBMS_OUTPUT.PUT_LINE('set timing off');
DBMS_OUTPUT.PUT_LINE('set termout off');
DBMS_OUTPUT.PUT_LINE('set linesize 32767');
DBMS_OUTPUT.PUT_LINE('SET VERIFY OFF');
DBMS_OUTPUT.PUT_LINE('SET HEADING OFF');
DBMS_OUTPUT.PUT_LINE('SET NEWPAGE NONE');
DBMS_OUTPUT.PUT_LINE('COLUMN SCRIPT FORM A3000');
DBMS_OUTPUT.PUT_LINE('spool '||i.table_nm||'_'||TO_CHAR(SYSDATE,'YYYYMMDD')||'.dat ');
V_COL_LIST := '';
v_header_select := chr(13);
v_sql := '';
v_table_nm := i.table_nm;
v_from_clause := v_table_nm;
END IF;
v_col_list := v_col_list || i.col_nm || '|';
IF i.rnm = i.cnt THEN
v_col_list := rtrim(v_col_list, '|' );
v_sql := v_sql || i.tbl_col_nm || ' ' ;
ELSE
v_sql := v_sql || i.tbl_col_nm || CHR(10) || '|| ''|'' || ' ;
END IF;
IF I.RNM = I.CNT THEN
V_HEADER_SELECT := 'SELECT '|| V_HEADER_SELECT || '''' || V_COL_LIST || '''' ||' FROM DUAL ;';
V_SQL := ' SELECT ' || V_SQL ||' AS ot ' || CHR(10) || ' FROM ' ||V_FROM_CLAUSE || CHR(10)||' ;';
DBMS_OUTPUT.PUT_LINE(v_header_select);
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('spool off');
END;
/
spool OFF
@v_out.sql;
SET serveroutput OFF
有关如何摆脱此错误的任何建议。感谢
答案 0 :(得分:0)
当我从Oracle SQL * Plus尝试此代码时,我收到以下错误 -
SP2-0734: unknown command beginning "'COLUMN_0|..." - rest of line ignored.
SP2-0734: unknown command beginning "_104|COLUM..." - rest of line ignored.
SP2-0734: unknown command beginning "|COLUMN_11..." - rest of line ignored.
SP2-0734: unknown command beginning "LUMN_118|C..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
我更新了代码以删除作业 -
v_header_select := chr(13);
使用此更改运行代码后,没有错误,列标题打印在假脱机文件AAA_20180530.dat中。
希望这也能解决你的问题。