delimiter // CREATE FUNCTION DBROAM.GET_FORMVALUE ( str in varchar2 ) return number is cursor_name integer; ret integer; val number(12,4); begin val := -1; begin cursor_name := dbms_sql.open_cursor; dbms_sql.parse(cursor_name, 'select '||str||' from dual', dbms_sql.v7); dbms_sql.define_column (cursor_name, 1, val); ret := dbms_sql.execute(cursor_name); if dbms_sql.fetch_rows (cursor_name)<>0 then dbms_sql.column_value (cursor_name, 1, val); end if; dbms_sql.close_cursor(cursor_name); exception when others then dbms_sql.close_cursor(cursor_name); end; return val; end; // delimiter ;改写成mysql可以运行的
时间: 2024-01-31 19:04:54 浏览: 227
好的,根据你提供的Oracle函数代码,我将其转换为MySQL函数代码如下:
```
delimiter //
CREATE FUNCTION GET_FORMVALUE (str VARCHAR(100)) RETURNS DECIMAL(12,4)
BEGIN
DECLARE cursor_name INTEGER;
DECLARE ret INTEGER;
DECLARE val DECIMAL(12,4);
SET val = -1;
SET cursor_name = 0;
SET ret = 0;
BEGIN
SET cursor_name = (SELECT @@session.pseudo_thread_id);
PREPARE my_query FROM CONCAT('SELECT ', str, ' FROM dual');
EXECUTE my_query;
DEALLOCATE PREPARE my_query;
IF cursor_name > 0 THEN
SET ret = 1;
END IF;
IF ret = 1 THEN
FETCH cursor_name INTO val;
END IF;
CLOSE cursor_name;
END;
RETURN val;
END;//
delimiter ;
```
请注意,MySQL中不存在`dbms_sql`包,所以我使用了动态SQL来编写查询语句。此外,由于MySQL不支持`%TYPE`语法,因此我将返回值类型设置为DECIMAL(12,4)。如果你需要返回不同的数据类型,可以根据需要进行更改。
阅读全文