PLS-00103: Encountered the symbol "," - plsql

This procedure is getting following error.
CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE
'CREATE TABLE COLUMN_NAMES AS (
SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS
FROM
(SELECT DISTINCT COLUMN_NAME
FROM BW_COLUMN_ROW_CELL_JOIN)
)';
END;
/
gives:
PLS-00103: Encountered the symbol "," when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || multiset bulk member submultiset
Can any one say what is wrong in this?
Thanks.

Another way (in Oracle 10g and later) is to use the alternative string literal notation - this means you don't need to worry about correctly escaping all the single quotes in the string, e.g. q'{my string's got embedded quotes}':
CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE q'[
CREATE TABLE COLUMN_NAMES AS (
SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS
FROM
(SELECT DISTINCT COLUMN_NAME
FROM BW_COLUMN_ROW_CELL_JOIN)
)]';
END;
/

The problem I think is you have single quotes within single quotes. I cant test this at the moment, but I'd suggest you try the following (note the inner quotes are double quotes '', which escapes them:
CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE COLUMN_NAMES AS ( SELECT LISTAGG(COLUMN_NAME, '','') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS FROM (SELECT DISTINCT COLUMN_NAME FROM BW_COLUMN_ROW_CELL_JOIN) )';
END;
/
I'd also try the create table part of the code standalone first just to make sure its valid before wrapping it in a proc.

You can't use single quotes directly in select statement of Execute Immediate it need to be coded using CHR(39)
CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE
'CREATE TABLE COLUMN_NAMES AS (
SELECT LISTAGG(COLUMN_NAME,'||chr(39)||','||chr(39)||') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS
FROM
(SELECT DISTINCT COLUMN_NAME FROM BW_COLUMN_ROW_CELL_JOIN))';
END;

Related

Create a table as, where 'date condition' in dynamic PL/SQL

I got assigned the following task.
Assume we have a table A structured with an id column and a date column.
Write a procedure in PL/SQL that: takes as parameters the table name (in our case A) and a date D, creates a backup table named A_bck containing only the records of A with dates < D and removes from the table A all the records inserted in A_bck.
Here there is my code.
Unluckily I get this error:
Error report -
ORA-00904: "MAY": invalid identifier
ORA-06512: at line 41
ORA-06512: at line 80
00904. 00000 - "%s: invalid identifier"
If I try to achieve the same result using a where condition on the id column instead that on the date one, I have no problems.
Where is the mistake? Am I implementing it completely in the wrong way?
The problem you have is that as you're executing dynamic sql you're query is built up as a string. Oracle does not know that the date you've given is actually a date, it is simply being treated as part of the string. To solve this you should be able to do the following:
my_query := 'CREATE TABLE ' || table_name_backup || ' AS (SELECT * FROM ' || table_name || ' WHERE table_date < to_date(''' || backup_date || '''))';
This should sort out your issue for you. As a side note, you will probably want to change your "table_exists" query, as table names are all stored in upper case, e.g.
SELECT COUNT(*) INTO table_exists FROM USER_TABLES WHERE TABLE_NAME = upper(my_table);
Edit: Further explanation following comment
To explain why you don't have the above problem when using integers, it is important to remember that using execute immediate simply executes the given string as an SQL query.
For example:
declare
x INTEGER := 1;
i integer;
my_query VARCHAR2(256);
begin
my_query := 'select 1 from dual where 1 = ' || x;
EXECUTE IMMEDIATE my_query INTO i;
end;
my_query in the above example would be executed as:
select 1 from dual where 1 = 1
which is perfectly valid sql. In your example however, you were ending up with something like this:
CREATE TABLE abaco_bck AS (SELECT * FROM abaco WHERE table_date < 27-MAY-17)
As it isn't wrapped in quotes, or explicitly converted to a date, the SQL engine is trying to subtract "MAY" from 27, but it doesn't know what "MAY" is.
One other thing to mention, is that for some operations you could use bind variables instead of quotes (although you can't for DDL) e.g.
declare
lToday DATE := SYSDATE;
i INTEGER;
my_query VARCHAR2(256);
begin
my_query := 'select 1 from dual where sysdate = :1';
EXECUTE IMMEDIATE my_query INTO i USING lToday;
end;

Parsing string in PL/SQL and insert values into database with RESTful web service

I am reading the values which I want to insert into database. I am reading them by lines. One line is something like this:
String line = "6, Ljubljana, Slovenija, 28";
Web service needs to separate values by comma and insert them into database. In PL/SQL language. How do I do that?
Here is some pl/sql that I have used to parse through delimited strings and then extract the individual words. You may have to mess with it a bit when using with the web service but it works fine when you are running it right in oracle.
declare
string_line varchar2(4000);
str_cnt number;
parse_pos_1 number := 1;
parse_pos_2 number;
parsed_string varchar2(4000);
begin
--counting the number of commas in the string so we know how many times to loop
select regexp_count(string_line, ',') into str_cnt from dual;
for i in 1..str_cnt + 1
loop
--grabbing the position of the comma
select regexp_instr(string_line, ',', parse_pos_1) into parse_pos_2 from dual;
--grabbing the individual words based of the comma positions using substr function
--handling the last loop
if i = str_cnt + 1 then
select substr(string_line, parse_pos_1, length(string_line)+1 - parse_pos_1) into parsed_string from dual;
execute immediate 'insert into your_table_name (your_column_name) values (' || parsed_string || ' )';
execute immediate 'commit';
--handles the rest
else
select substr(string_line, parse_pos_1, parse_pos2 - parse_pos_1) into parsed_string from dual;
execute immediate 'insert into your_table_name (your_column_name) values (' || parsed_string || ' )';
execute immediate 'commit';
end if;
parse_pos_1 := parse_pos_2+1;
end loop;
end;
I found an answer to that particular question. If you have similar values to those I posted for a question, like numbers, which look something like this:
String line = "145, 899";
This string is sent via POST request (RESTful web service, APEX). Now getting the values in PL/SQL and inserting them into table looks something like this:
DECLARE
val1 NUMBER;
val2 NUMBER;
str CLOB;
BEGIN
str := string_fnc.blob_to_clob(:body); // we have to convert body
val1 := TO_NUMBER(REGEXP_SUBSTR(str, '[^,]+', 1, 1));
val2 := TO_NUMBER(REGEXP_SUBSTR(str, '[^,]+', 1, 2));
// REGEXP_SUBSTR(source, pattern, start_position, nth_appearance)
INSERT INTO PRIMER VALUES (val1, val2);
END;
However, this is the method to insert line by line into database, so if you have large amount of rows in a file to insert, this isn't a way to do it. But here is the example which I requested. I hope it helps to someone.

select the union of several tables together in a single step

I am very new to Oracle 11g and am trying to generate a large string by appending text for each column in a select statement and using a cursor to store the results. However I want the last statement to not have a union all included. The final result I want to build large string of each row generated or simply execute the result if possible.
Note: column1 has a list of schemas that I am interested in.
select 'select * from ' || column1 || '.' || column2 || ' union all ' from mytable
This is where column1 is the schema, column2 is the table name.
What is the simplest way to generate the final string without using rtrim to remove the last string. And is there a simple way to append all these rows together in the string automatically?
The final goal is to actually just execute the union into a resulting cursor.
If you're querying in a loop anyway I wouldn't try to construct the string as part of the select at all; I'd do it all within the loop. Something like (untested):
declare
str varchar2(32768);
begin
for rec in (select column1, column2, rownum as rn from mytable)
loop
if rec.rn > 1 then
str := str || ' union all ';
end if;
str := str || 'select * from "' || rec.column[ || '"."' || rec.column2 ||'"';
end loop;
-- do something with str e.g. display to verify the syntax
-- before using in a cursor
dbms_output.put_line(str);
end;
Rather than adding union all to the end of every row except the last one,the rn check means it's added to the start of every row except the first one, which is easier to detect.
I've also wrapped the schema and table names in double quotes, just in case you have to deal with any quoted identifiers. But if your stored values don't match the case of the owners and table names in all_tables this will cause a problem rather than solve it.

PL/SQL - comma separated list within IN CLAUSE

I am having trouble getting a block of pl/sql code to work. In the top of my procedure I get some data from my oracle apex application on what checkboxes are checked. Because the report that contains the checkboxes is generated dynamically I have to loop through the
APEX_APPLICATION.G_F01
list and generate a comma separated string which looks like this
v_list VARCHAR2(255) := (1,3,5,9,10);
I want to then query on that list later and place the v_list on an IN clause like so
SELECT * FROM users
WHERE user_id IN (v_list);
This of course throws an error. My question is what can I convert the v_list to in order to be able to insert it into a IN clause in a query within a pl/sql procedure?
If users is small and user_id doesn't contain commas, you could use:
SELECT * FROM users WHERE ',' || v_list || ',' LIKE '%,'||user_id||',%'
This query is not optimal though because it can't use indexes on user_id.
I advise you to use a pipelined function that returns a table of NUMBER that you can query directly. For example:
CREATE TYPE tab_number IS TABLE OF NUMBER;
/
CREATE OR REPLACE FUNCTION string_to_table_num(p VARCHAR2)
RETURN tab_number
PIPELINED IS
BEGIN
FOR cc IN (SELECT rtrim(regexp_substr(str, '[^,]*,', 1, level), ',') res
FROM (SELECT p || ',' str FROM dual)
CONNECT BY level <= length(str)
- length(replace(str, ',', ''))) LOOP
PIPE ROW(cc.res);
END LOOP;
END;
/
You would then be able to build queries such as:
SELECT *
FROM users
WHERE user_id IN (SELECT *
FROM TABLE(string_to_table_num('1,2,3,4,5'));
You can use XMLTABLE as follows
SELECT * FROM users
WHERE user_id IN (SELECT to_number(column_value) FROM XMLTABLE(v_list));
I have tried to find a solution for that too but never succeeded. You can build the query as a string and then run EXECUTE IMMEDIATE, see http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/dynamic.htm#i14500.
That said, it just occurred to me that the argument of an IN clause can be a sub-select:
SELECT * FROM users
WHERE user_id IN (SELECT something FROM somewhere)
so, is it possible to expose the checkbox values as a stored function? Then you might be able to do something like
SELECT * FROM users
WHERE user_id IN (SELECT my_package.checkbox_func FROM dual)
Personally, i like this approach:
with t as (select 'a,b,c,d,e' str from dual)
--
select val
from t, xmltable('/root/e/text()'
passing xmltype('<root><e>' || replace(t.str,',','</e><e>')|| '</e></root>')
columns val varchar2(10) path '/'
)
Which can be found among other examples in Thread: Split Comma Delimited String Oracle
If you feel like swamping in even more options, visit the OTN plsql forums.

PL/SQL Inserting 1 row for each result in a select

I am writing a PL/SQL Procedure that performs a select based on input variables and then inserts a row for each result in the select. I am having trouble debugging what is wrong with my query due my newness to PL/SQL. I know this must be easy, but I am stuck here for some reason. Thanks for your help!
CREATE OR REPLACE PROCEDURE setup_name_map(ranking_id IN NUMBER, class_string IN VARCHAR2)
IS
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES WHERE NAME = class_string)
LOOP
EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID) VALUES (' || rec.NAME_ID || ', ' || ranking_id || ')';
END LOOP;
END;
According to the Oracle Developer Compiler... 'NAME_ID' is an invalid identifier. I've tried putting it in quotes but no dice. It also complains that loop index variables 'REC' use is invalid. Any help is much appreciated.
There is no need for dynamic SQL here:
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
WHERE NAME = class_string)
LOOP
INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
VALUES (rec.NAME_ID, ranking_id);
END LOOP;
END;
Better still you can avoid a slow row-by-row cursor approach like this:
BEGIN
INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
SELECT NAME_ID, ranking_id FROM PRODUCT_NAMES
WHERE NAME = class_string;
END;
If you really did need the dynamic SQL you should not be concatenating values into it, but using bind variables:
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
WHERE NAME = class_string)
LOOP
EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING
(NAME_ID, RANKING_ID) VALUES (:b1, :b2)
USING rec.NAME_ID, ranking_id;
END LOOP;
END;

Resources