使用变量和条件创建Oracle函数

时间:2016-06-20 15:57:27

标签: oracle plsql maximo

我很难找到正确的语法来创建一个带变量和条件的函数。

create or replace
FUNCTION createURL( IDName IN varchar2, IDValue IN number )
  RETURN VARCHAR2
IS
  ApplicationURL VARCHAR2(100); 
  AppplicationParm VARCHAR2(255);
  DBName VARCHAR(100);

BEGIN
  select sys_context('USERENV','DB_NAME') AS Instance into DBName FROM DUAL;

  IF DBName = 'WAMDEV' THEN ApplicationURL := 'http://srpwam10:080/maxi';
      ELSIF  DBName ='WAMDEVPJ' THEN ApplicationURL := 'http://srpwam10:080/maxi';
      ELSIF  DBName = 'WAMTST' THEN ApplicationURL := 'http://wamtest/maxi';
      ELSIF  DBName = 'WAMTSTPJ' THEN ApplicationURL := 'http://wamtest/maximo';
      ELSIF  DBName = 'WAMQA' THEN ApplicationURL := 'http://wamqa/maxi';
      ELSIF  DBName = 'WAMQAPJ'  THEN ApplicationURL := 'http://wamqa/maximo';
      ELSE  DBName := 'WAMP'; ApplicationURL := 'http://wam/maxi';
    END IF ;
 IDN := IDName;
  IF IDN = 'workorderid' THEN AppplicationParm :=  '/ui/?event=loadapp' || CHR(38) || 'value=wotrack' || CHR(38) || 'uniqueid=' || TO_CHAR(IDValue);
      ELSIF IDN = 'assetuid' THEN AppplicationParm :=  '/ui/?event=loadapp' || CHR(38) || 'value=asset' || CHR(38) || 'uniqueid=' || TO_CHAR(IDValue);
      ELSIF IDN = 'locationsid' THEN AppplicationParm :=  '/ui/?event=loadapp' || CHR(38) || 'value=location' || CHR(38) || 'uniqueid=' || TO_CHAR(IDValue);
      ELSE AppplicationParm :=   '/ui/?event=loadapp' || CHR(38) || 'value=sr' || CHR(38) || 'uniqueid=' || TO_CHAR(IDValue);
     END IF;

  RETURN 'javascript:void(window.open(''' ||  ApplicationURL || ApplicationParm || ''',''_blank''))';
END;

我不确定代码是否正确以及问题是什么?我不能创造这个功能。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我试图编译你的代码,但是有错误

length = len(list_of_lines)

因此您没有声明变量 IDN

答案 1 :(得分:2)

AppplicationParm有时候是2 p,有时是3次。获取SQLDeveloper,他会告诉你所有的错误。

以下是案例:

CREATE OR REPLACE FUNCTION createURL (IDName IN VARCHAR2, IDValue IN NUMBER)
   RETURN VARCHAR2
IS
   ApplicationURL     VARCHAR2 (100);
   ApplicationParm   VARCHAR2 (255);
   DBName             VARCHAR (100);
BEGIN
   SELECT SYS_CONTEXT ('USERENV', 'DB_NAME') AS Instance INTO DBName FROM DUAL;
   ApplicationURL := case when DBName = 'WAMDEV' OR DBName = 'WAMDEVPJ' then 'http://srpwam10:9080/maximo' 
                          when DBName = 'WAMTST' OR DBName = 'WAMTSTPJ' then 'http://srpwam10:9080/maximo'
                          when DBName = 'WAMQA' OR DBName = 'WAMQAPJ' then 'http://srpwam10:9080/maximo'
                          else 'http://wam/maximo' 
                     end;
   ApplicationParm := case  IDName
                         when 'workorderid' THEN '/ui/?event=loadapp' || CHR (38) || 'value=wotrack' || CHR (38) || 'uniqueid=' || TO_CHAR (IDValue)
                         when 'assetuid' THEN '/ui/?event=loadapp' || CHR (38) || 'value=asset' || CHR (38) || 'uniqueid=' || TO_CHAR (IDValue)
                         when 'locationsid' THEN '/ui/?event=loadapp' || CHR (38) || 'value=location' || CHR (38) || 'uniqueid=' || TO_CHAR (IDValue)
                         else '/ui/?event=loadapp' || CHR (38) || 'value=sr' || CHR (38) || 'uniqueid=' || TO_CHAR (IDValue)
                      end;
   RETURN 'javascript:void(window.open(''' || ApplicationURL || ApplicationParm || ''',''_blank''))';
END;