这是fu功能:
create or replace
FUNCTION pk_max_value(t_name VARCHAR) RETURN NUMBER
is
rws number;
pk_column_name varchar(300);
sql_text VARCHAR(2048);
BEGIN
sql_text := 'SELECT cols.column_name ' ||
'FROM all_constraints cons, all_cons_columns cols ' ||
'WHERE cols.table_name = ' || t_name ||
' AND cons.constraint_type = ' || 'chr(39) P chr(39) ' ||
'AND cons.constraint_name = cols.constraint_name ' ||
'AND cons.owner = cols.owner ' ||
'ORDER BY cols.table_name, cols.position;';
execute immediate sql_text into pk_column_name;
sql_text := 'SELECT MAX(' || pk_column_name || ') FROM ' || t_name;
EXECUTE IMMEDIATE sql_text INTO rws;
RETURN rws;
END;
当我执行此操作时,Oracle给了我一个答案:
SQL命令未正确结束。
有人可以告诉我,我的摔倒了吗?
答案 0 :(得分:3)
首先,sql_text不应以;
应该是:
sql_text := 'SELECT cols.column_name
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '''||t_name||'''
AND cons.constraint_type = ''P''
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position';
Obs:当PK类型不是NUMBER
时,此函数将失败。
Obs2:t_name应该是大写的......