Oracle writes a function, the parameter is an ID string, and returns the name string

 

The system has an id set of the code table stored in a certain field in the demand business table. Now, when querying, the text in the code table should be returned.

So I thought of writing a function to call in the select statement, Baidu searched, but I didn't find it at first.

Later, I submitted a question and answer in Baidu Know, and found the solution path in recommending similar questions: , Baidu Know Address, .

Since he uses the writing method of mssql, he needs to modify the field type: referring to his writing method, the tables and fields queried are constructed using dynamic sql, and the writing method is as follows:

 

CREATE OR REPLACE FUNCTION F_GetTableText(t_table  in VARCHAR2 , p_id  in VARCHAR2,  caption in VARCHAR2, p_id_data  in VARCHAR2)
RETURN VARCHAR2 AS
  TYPE   refcursor IS REF CURSOR;
  v_cursor  REFCURSOR;
  v_name   VARCHAR2(10);
  v_result   VARCHAR2(1000);
  v_id_dada VARCHAR2(1000);
  v_SQL  VARCHAR2(1000);
BEGIN
  --1 The incoming where in condition needs to be processed into a valid 'a,b' ---->Conversion--> 'a','b'
  v_id_dada := REPLACE(p_id_data, ',', ''',''');
  v_id_dada :=  '''' || v_id_dada ||  '''';
  v_result := '';
  
  --Generate dynamic sql similar to select catption from table where id in('','');
    v_SQL := 'SELECT '||caption||' FROM '||t_table||' WHERE '||p_id||' IN (' || v_id_dada || ')';

  -- Open the cursor.
  OPEN v_cursor FOR v_SQL;
  LOOP--start loop cursor
    -- Data input.
    FETCH v_cursor INTO v_name;
    -- Exit the loop when there is no data.
    EXIT WHEN v_cursor%NOTFOUND;
    v_result := v_result || v_name|| ',';
  END LOOP;--end the loop cursor
  CLOSE v_cursor;-- close the cursor
  --Remove the comma at the end of the processing result
  v_result := TRIM(BOTH ',' FROM  v_result);
  RETURN v_result;
END F_GetTableText;

 

 

Returns the text field of the searched table according to the id query condition,

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327004086&siteId=291194637