Oracle - Concatenate Strings as column name - sql

I am trying to concatenate strings that are used as column names
I want to do something like:
Select someData as "ONE" || :someVariable) from sometable;
where someVariable is a bind variable, which does not work inside double quotes.
(The column should have the name "ONE2018" if someVariable = 2018.)
I tried it with single quotes and with the concat function. It doesn't work.
Is there some way to accomplish this?
EDIT:
With inspiration from littlefoots answer I tried
declare
customVariable number(4);
rc sys_refcursor;
begin
open rc for 'select 1 as bla' || :customVariable || ' from dual';
dbms_sql.return_result(rc);
end;
/
which does have the output
BLA2018
----------
1
I don't know how to put that into a PreparedStatement, but if used on it's own it works and might help someone else

An example based on Scott's EMP table which contains columns whose names begins with an E: ENAME and EMPNO.
You'd pass NAME or MPNO and get the result.
SQL> create or replace function f_one (par_column_name in varchar2)
2 return sys_refcursor
3 is
4 l_rc sys_refcursor;
5 l_str varchar2(200);
6 begin
7 l_str := 'select e' || par_column_name || ' from emp where rownum < 3';
8 open l_rc for l_str;
9 return l_rc;
10 end;
11 /
Function created.
SQL> select f_one('mpno') from dual;
F_ONE('MPNO')
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
EMPNO
----------
7369
7499
SQL> select f_one('name') from dual;
F_ONE('NAME')
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
ENAME
----------
SMITH
ALLEN
SQL>

Related

How can I return a select-query that canges based on the parameters i use when calling a function?

I have been wrestling with this problem for a while now. I want to make a function that takes parameters. The function is supposed to, with user submitted parameters, change a select-statment and after return/run the select.
The function:
create or replace FUNCTION F_GET_TABLE(column1_in varchar2, column2_in varchar2)
return query
is
base_query constant varchar2(5000 char) :=
'select column1, column2 from CustomersTable;';
begin
replace(replace(base_query, 'column1', column2_in),'column2', column2_in );
queryToReturn query := base_query;
return queryToReturn;
end F_GET_TABLE;
In my head the end result should be that I call the function like this:
select F_GET_TABLE('f_name','e_mail') from dual;
And I should have the same result as if I wrote the select-statment as:
select f_name, e_mail from CustomersTable;
So I've tried in different ways to make the function return the query as I've described. However the best I managed to do was return a varchar2 with the select-statement - however then I have to remove "" from the start and end of the select-block and run it manually.. I couldn't seem to find any answers to my problem while searching the internet, please help me out here!
Here's how:
SQL> CREATE OR REPLACE FUNCTION f_get_table (column1_in VARCHAR2,
2 column2_in VARCHAR2)
3 RETURN VARCHAR2
4 IS
5 base_query VARCHAR2 (5000) := 'select column1, column2 from emp';
6 BEGIN
7 RETURN REPLACE (REPLACE (base_query, 'column1', column1_in),
8 'column2', column2_in);
9 END f_get_table;
10 /
Function created.
SQL> select f_get_table('ename', 'job') from dual;
F_GET_TABLE('ENAME','JOB')
--------------------------------------------------------------------------------
select ename, job from emp
SQL>
If you want to return result, then return ref cursor:
SQL> CREATE OR REPLACE FUNCTION f_get_table (column1_in VARCHAR2,
2 column2_in VARCHAR2)
3 RETURN SYS_REFCURSOR
4 IS
5 base_query VARCHAR2 (5000) := 'select column1, column2 from emp';
6 l_rc SYS_REFCURSOR;
7 BEGIN
8 OPEN l_rc FOR
9 REPLACE (REPLACE (base_query, 'column1', column1_in),
10 'column2',
11 column2_in);
12
13 RETURN l_rc;
14 END f_get_table;
15 /
Function created.
Testing:
SQL> SELECT f_get_table ('ename', 'job') FROM DUAL;
F_GET_TABLE('ENAME',
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
ENAME JOB
---------- ---------
KING PRESIDENT
BLAKE MANAGER
CLARK MANAGER
JONES MANAGE
<snip>

Get column names of the result of a query Oracle SQL

I need a query that will get the column names of the result of another query. The other query can be anything - I can't make any assumptions about it but it will typically be some SELECT statement.
For example, if I have this table Members
Id | Name | Age
---|------|----
1 | John | 25
2 | Amir | 13
And this SELECT statement
SELECT Name, Age FROM Members
Then the result of the query I'm trying to write would be
Name
Age
In SQL Server, there is a function - sys.dm_exec_describe_first_result_set - that does this but I can't find an equivalent in Oracle.
I tried to use this answer but I can't use CREATE TYPE statements because of permissions issues and I probably can't use CREATE FUNCTION statements for the same reason.
Suppose you have a query like this:
select *
from (select deptno, job, sal from scott.emp)
pivot (avg(sal) as avg_sal for job in
('ANALYST' as analyst, 'CLERK' as clerk, 'SALESMAN' as salesman)
)
order by deptno
;
This produces the result:
DEPTNO ANALYST_AVG_SAL CLERK_AVG_SAL SALESMAN_AVG_SAL
---------- --------------- ------------- ----------------
10 1300
20 3000 950
30 950 1400
Notice the column names (like ANALYST_AVG_SAL) - they don't appear exactly in that form anywhere in the query! They are made up from two separate pieces, put together with an underscore.
Now, if you were allowed to create views (note that this does not create any data in your database - it just saves the text of a query), you could do this:
Create the view (just add the first line of code to what we already had):
create view q201028_vw as
select *
from (select deptno, job, sal from scott.emp)
pivot (avg(sal) as avg_sal for job in
('ANALYST' as analyst, 'CLERK' as clerk, 'SALESMAN' as salesman)
)
order by deptno
;
(Here I assumed you have some way to identify the query, an id like Q201028, and used that in the view name. That is not important, unless you need to do this often and for a large number of queries at the same time.)
Then you can find the column names (and also their order, and - if needed - their data type, etc.) by querying *_TAB_COLUMNS. For example:
select column_id, column_name
from user_tab_columns
where table_name = 'Q201028_VW'
order by column_id
;
COLUMN_ID COLUMN_NAME
---------- --------------------
1 DEPTNO
2 ANALYST_AVG_SAL
3 CLERK_AVG_SAL
4 SALESMAN_AVG_SAL
Now you can drop the view if you don't need it for anything else.
As an aside: The "usual" way to "save" queries in the database, in Oracle, is to create views. If they already exist as such in your DB, then all you need is the last step I showed you. Otherwise, were is the "other query" (for which you need to find the columns) coming from in the first place?
I would use the dbms_sql package and the following code example should show you how to start:
DECLARE
cursorID INTEGER;
status INTEGER;
colCount INTEGER;
rowCount INTEGER;
description dbms_sql.desc_tab;
colType INTEGER;
stringValue VARCHAR2(32676);
sqlCmd VARCHAR2(32767);
BEGIN
-- open cursor
cursorID := dbms_sql.open_cursor;
-- parse statement
dbms_sql.parse(cursorID, 'select * from user_tables', dbms_sql.native);
-- describe columns
dbms_sql.describe_columns(cursorID, colCount, description);
-- cursor close
dbms_sql.close_cursor(cursorID);
-- open cursor
cursorID := dbms_sql.open_cursor;
-- assemble a new select only using up to 5 the "text" columns
FOR i IN 1 .. description.COUNT LOOP
IF (i > 5) THEN
EXIT;
END IF;
IF (description(i).col_type IN (1, 112)) THEN
IF (sqlCmd IS NOT NULL) THEN
sqlCmd := sqlCmd || ', ';
END IF;
sqlCmd := sqlCmd || description(i).col_name;
END IF;
END LOOP;
sqlCmd := 'SELECT ' || sqlCmd || ' FROM user_tables';
dbms_output.put_line(sqlCmd);
-- parse statement
dbms_sql.parse(cursorID, sqlCmd, dbms_sql.native);
-- describe columns
dbms_sql.describe_columns(cursorID, colCount, description);
-- define columns
FOR i IN 1 .. description.COUNT LOOP
dbms_sql.define_column(cursorID, i, stringValue, 4000);
END LOOP;
-- execute
status := dbms_sql.execute(cursorID);
-- fetch up to 5 rows
rowCount := 0;
WHILE (dbms_sql.fetch_rows(cursorID) > 0) LOOP
rowCount := rowCount + 1;
IF (rowCount > 5) THEN
EXIT;
END IF;
dbms_output.put_line('row # ' || rowCount);
FOR i IN 1 .. description.COUNT LOOP
dbms_sql.column_value(cursorID, i, stringValue);
dbms_output.put_line('column "' || description(i).col_name || '" = "' || stringValue || '"');
END LOOP;
END LOOP;
-- cursor close
dbms_sql.close_cursor(cursorID);
END;
/
As astentx suggested, you can use a common table expression function to package the PL/SQL code into a SQL statement. This solution is just a single SQL statement, and requires no non-default privileges and does not create any permanent objects.
(The only downside is that not all SQL tools understand these kinds of WITH clauses, and they may throw an error expecting a different statement terminator.)
SQL> create table members(id number, name varchar2(100), age number);
Table created.
SQL> with function get_result_column_names(p_sql varchar2) return sys.odcivarchar2list is
2 v_cursor_id integer;
3 v_col_cnt integer;
4 v_columns dbms_sql.desc_tab;
5 v_column_names sys.odcivarchar2list := sys.odcivarchar2list();
6 begin
7 v_cursor_id := dbms_sql.open_cursor;
8 dbms_sql.parse(v_cursor_id, p_sql, dbms_sql.native);
9 dbms_sql.describe_columns(v_cursor_id, v_col_cnt, v_columns);
10
11 for i in 1 .. v_columns.count loop
12 v_column_names.extend;
13 v_column_names(v_column_names.count) := v_columns(i).col_name;
14 end loop;
15
16 dbms_sql.close_cursor(v_cursor_id);
17
18 return v_column_names;
19 exception when others then
20 dbms_sql.close_cursor(v_cursor_id);
21 raise;
22 end;
23 select *
24 from table(get_result_column_names(q'[select name, age from members]'));
25 /
COLUMN_VALUE
--------------------------------------------------------------------------------
NAME
AGE

How do I assign the value of a refcursor to a variable in PL/SQL

This stored procedure call returns a single varchar2 type value. The issue is that it comes back in a refcursor. I need to get the ID value and assign it to a variable instead of printing it to the console.
var r refcursor;
DECLARE
BEGIN
P_PACKAGE.INSERT_INVOICE(
IN_INVOICE_TYPE => L_INVOICE.INVOICE_TYPE,
OUTPUT => :R);
END;
/
print r;
Exactly as you said - you need to assign it to a variable. Here's an example based on Scott's schema.
SQL> create or replace procedure p_rc (par_deptno in number, par_rc out sys_refcursor)
2 is
3 begin
4 open par_rc for select ename, sal
5 from emp
6 where deptno = par_deptno;
7 end;
8 /
Procedure created.
Let's test it; pay attention to lines #4, 5 (declaration of variables) and #10 (fetch into those variables):
SQL> declare
2 l_rc sys_refcursor;
3 -- declare variables which will get values returned by refcursor
4 l_ename emp.ename%type;
5 l_sal emp.sal%type;
6 begin
7 p_rc(10, l_rc);
8
9 loop
10 fetch l_rc into l_ename, l_sal;
11 exit when l_rc%notfound;
12 dbms_output.put_line(rpad(l_ename, 10, ' ') ||': '|| l_sal);
13 end loop;
14 end;
15 /
CLARK : 2450
KING : 5001
MILLER : 1300
PL/SQL procedure successfully completed.
SQL>
I wouldn't define the REFCURSOR in SQL*Plus as you've shown it. Assuming that your REFCURSOR was opened with a statement similar to
SELECT ID
FROM SOME_TABLE
WHERE SOMEFIELD = some_value
then your PL/SQL code should look something like
DECLARE
aString VARCHAR2(2000);
rc SYS_REFCURSOR;
BEGIN
P_PACKAGE.INSERT_INVOICE(
IN_INVOICE_TYPE => L_INVOICE.INVOICE_TYPE,
OUTPUT => rc);
FETCH rc
INTO aString;
DBMS_OUTPUT.PUT_LINE('aString = ''' || aString || '''');
END;

Return multiple values in varchar2

I want to return the values of several columns (With a function) in a varchar2 but I get an error when I choose several columns in select.
FUNCTION FU_PRUEBAS (P_IDNUM MAC.IDNUM%TYPE) RETURN VARCHAR2 IS REGISTRO VARCHAR2(100);
BEGIN
select NOMBRES, FECHANACIMIENTO INTO REGISTRO
from MAC WHERE IDNUM = P_IDNUM;
RETURN REGISTRO;
Error:
Error(17,6): PL/SQL: SQL Statement ignored
Error(18,6): PL/SQL: ORA-00947: there are not enough values
Two relatively simple options - if they suit your needs. You didn't really explain what you expect to get as a result so - this might, or might not be applicable to your situation.
Anyway, here you go:
First option uses listagg function which returns a concatenated string (as you wanted to return varchar2). Drawback is that it won't work for the result longer than 4000 characters, but - in that case - you can use XMLAGG.
SQL> create or replace function fu_pruebas (par_deptno in number)
2 return varchar2
3 is
4 retval varchar2(200);
5 begin
6 select listagg(ename, ', ') within group (order by ename)
7 into retval
8 from emp
9 where deptno = par_deptno;
10 return retval;
11 end;
12 /
Function created.
SQL> select fu_pruebas(10) result from dual;
RESULT
------------------------------------------------------------------------------
CLARK, KING, MILLER
SQL>
Another one concatenates different columns into a string; you might need to use e.g. to_char or to_date functions in certain cases, but - generally - it works as follows:
SQL> create or replace function fu_pruebas_2 (par_empno in number)
2 return varchar2
3 is
4 retval varchar2(200);
5 begin
6 select ename ||' - '|| job ||' - '|| sal
7 into retval
8 from emp
9 where empno = par_empno;
10 return retval;
11 end;
12 /
Function created.
SQL> select fu_pruebas_2(7654) result from dual;
RESULT
------------------------------------------------------------------------------
MARTIN - SALESMAN - 1250
SQL>
if you need a multiple values retunr you need a proper object
create or replace
type your_obj as object
( val_1 varchar2(200),
val_2 varchar2(200)
);
then
FUNCTION FU_PRUEBAS (P_IDNUM MAC.IDNUM%TYPE) RETURN VARCHAR2 IS REGISTRO VARCHAR2(100);
BEGIN
select NOMBRES, FECHANACIMIENTO INTO your_var1, your_var2
from MAC WHERE IDNUM = P_IDNUM
RETURN your_obj(your_var1, your_var2)
END;

Using define in simple sql statement

Is there a way of defining a section of SQL as a constant for repeated use in a simple oracle SQL statement. In a similar way that can be done in a script and in a similar way to the WITH statement?
Something like
Define meat as "foodtype<>'fruit' and foodtype<>'veg'"
select * from meals where meat
In SQL*Plus you could use DEFINE.
For example,
SQL> define l_str = 'ename = ''SCOTT'''
SQL>
SQL> select empno, deptno from emp where &l_str;
old 1: select empno, deptno from emp where &l_str
new 1: select empno, deptno from emp where ename = 'SCOTT'
EMPNO DEPTNO
---------- ----------
7788 20
SQL>
NOTE DEFINE is a SQL*Plus command. Define sets a user variable or displays its value.
However, not possible in plain SQL as the query won't parse and would throw an error. Since, during parse time, Oracle would expect it to be static, you cannot have it dynamic.
You can use Dynamic SQL in PL/SQL, such that you could have it as a string variable and use it repeatedly in the scope of the session.
For example,
SQL> DECLARE
2 var_str VARCHAR2(1000);
3 var_str1 VARCHAR2(1000);
4 v_empno emp.empno%type;
5 BEGIN
6 var_str := 'ename = ''SCOTT''';
7 var_str1 := 'select empno FROM emp WHERE '|| var_str;
8 EXECUTE IMMEDIATE var_str1 INTO v_empno;
9
10 dbms_output.put_line(v_empno);
11
12 END;
13 /
7788
PL/SQL procedure successfully completed.
SQL>
As per your question it seems that the condition should be dynamic. It can be acheived in PLSQL Block like below
DECLARE
v1 varchar2(100) := ' foodtype <> ''fruit'' and foodtype <> ''Veg''';
mealstyp meals%ROWTYPE;
BEGIN
EXECUTE IMMEDIATE 'select * from meals where'||v1 into mealstyp;
LOOP
--your type record
END LOOP;
END