我正在检查列长度和精度,如果精度高于数据库,那么我应该执行alter system。
这里出现的错误是(COL_COUNT <> 0 AND TO_NUMBER(OLD_LENGTH) < 6,2 )
修改
DECLARE COL_COUNT NUMBER;
OLD_LENGTH number(6);
old_prec number(6);
BEGIN
COL_COUNT:= 0;
SELECT DATA_LENGTH, DATA_PRECISION INTO OLD_LENGTH , old_prec FROM USER_TAB_COLS WHERE TABLE_NAME='EX_EMPLOYEE' AND COLUMN_NAME='ID';
SELECT COUNT (1) INTO COL_COUNT FROM USER_TAB_COLUMNS WHERE TABLE_NAME='EX_EMPLOYEE'AND COLUMN_NAME='ID';
IF (COL_COUNT <> 0 AND to_number(OLD_LENGTH) < 6.2 ) THEN
EXECUTE IMMEDIATE ('ALTER TABLE EX_EMPLOYEE MODIFY ID NUMERIC(6,2)');
END IF;
END;
/
答案 0 :(得分:1)
这不是您应该如何比较data_length
和data_precision
的值。将它们作为正确的数字进行比较,不要将其转换为字符串:
DECLARE
old_length NUMBER;
old_prec NUMBER;
BEGIN
SELECT data_length, coalesce(data_precision,0)
INTO old_length, old_prec
FROM user_tab_cols
WHERE table_name='EX_EMPLOYEE'
AND column_name='ID';
IF (old_length <> 6 and old_prec <> 2) THEN
EXECUTE IMMEDIATE ('ALTER TABLE EX_EMPLOYEE MODIFY ID NUMERIC(6,2)');
END IF;
END;
/
请注意,第一次选择后的SELECT COUNT(*)
是无用的。如果没有这样的列,则第一个SELECT data_length, data_precision INTO
将抛出ORA-01403: no data found
异常,如果列存在,代码将永远不会到达检查。
答案 1 :(得分:0)
Oracle使用小数周期。逗号把它丢掉了。尝试:
DECLARE COL_COUNT NUMBER;
OLD_LENGTH varchar2(50);
BEGIN
COL_COUNT:= 0;
SELECT DATA_LENGTH||'.'||DATA_PRECISION INTO OLD_LENGTH FROM USER_TAB_COLS WHERE TABLE_NAME='EX_EMPLOYEE' AND COLUMN_NAME='ID';
SELECT COUNT (1) INTO COL_COUNT FROM USER_TAB_COLUMNS WHERE TABLE_NAME='EX_EMPLOYEE'AND COLUMN_NAME='ID';
IF (COL_COUNT <> 0 AND to_number(OLD_LENGTH) < 6.2 ) THEN
EXECUTE IMMEDIATE ('ALTER TABLE EX_EMPLOYEE MODIFY ID NUMERIC(6,2)');
END IF;
END;
答案 2 :(得分:0)
尝试使用“。”分隔符而不是逗号:
select to_number('6.2') from dual; --works
select to_number('6,2') from dual; --returns error