From keyword not found where expected in PL-SQL - oracle

I have the below code:
v_SQL := '
-- ==========================================================
-- Main Unpivot Table - To move years from column headings
-- to rows values
-- ==========================================================
SELECT
CAST(FieldYear AS Number) FieldYear
, FieldType
, CASE FieldValue
WHEN '''' THEN NULL
ELSE CAST (FieldValue AS Number(38,8))
END FieldValue
FROM
(SELECT ' ||
v_InsertType ||' AS FieldType
,Y1 as ' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '
,Y2 as ' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '
,Y3 as ' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '
,Y4 as ' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '
,Y5 as ' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '
,Y6 as ' || CAST(v_FY0 - v_OffSet AS CHAR) || '
,Y7 as ' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '
,Y8 as ' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '
,Y9 as ' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '
,Y10 as ' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '
,Y11 as ' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '
,Y12 as ' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '
,Y13 as ' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '
,Y14 as ' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '
,Y15 as ' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '
,Y16 as ' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '
,Y17 as ' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '
,Y18 as ' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '
,Y19 as ' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '
,Y20 as ' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '
,Y21 as ' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '
,Y22 as ' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '
,Y23 as ' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '
,Y24 as ' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '
,Y25 as ' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '
,Y26 as ' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '
,Y27 as ' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '
,Y28 as ' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '
,Y29 as ' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '
,Y30 as ' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '
,Y31 as ' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '
,Y32 as ' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '
,Y33 as ' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '
,Y34 as ' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '
,Y35 as ' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '
FROM ' ||
v_tblInsertType || ') UP
UNPIVOT
(FieldValue FOR FieldYear IN
(' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '
)
)AS unpvt
Execute immediate v_SQL;
where v_FY0 is NUMBER(10);v_SQL NVarchar2(4000),v_InsertType VARCHAR2(4000); and v_OffSet Number(10); v_tblInsertType VARCHAR2(4000);
Upon executing, I get: v_FY0 =2018 and v_OffSet = 0;
Upon executing ,i am getting the error - ORA-00923: FROM Keyword not found where expected.
How to resolve this issue and how to get this working.

You can't use as in the from clause.
This query:
select * from (select dummy from dual) as tab
produces error ORA-00933. So simply remove as:
select * from (select dummy from dual) tab

Here is the modified code that gives me the required answer:
v_SQL := '
-- ==========================================================
-- Main Unpivot Table - To move years from column headings
-- to rows values
-- ==========================================================
SELECT
CAST(FieldYear AS Number) FieldYear
, FieldType
, CASE FieldValue
WHEN n'''' THEN NULL
ELSE CAST (FieldValue AS Number(38,8))
END FieldValue
FROM
(SELECT ' ||
v_InsertType ||' AS FieldType
,Y1 as "' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '"
,Y2 as "' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '"
,Y3 as "' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '"
,Y4 as "' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '"
,Y5 as "' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '"
,Y6 as "' || CAST(v_FY0 - v_OffSet AS CHAR) || '"
,Y7 as "' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '"
,Y8 as "' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '"
,Y9 as "' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '"
,Y10 as "' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '"
,Y11 as "' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '"
,Y12 as "' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '"
,Y13 as "' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '"
,Y14 as "' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '"
,Y15 as "' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '"
,Y16 as "' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '"
,Y17 as "' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '"
,Y18 as "' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '"
,Y19 as "' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '"
,Y20 as "' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '"
,Y21 as "' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '"
,Y22 as "' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '"
,Y23 as "' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '"
,Y24 as "' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '"
,Y25 as "' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '"
,Y26 as "' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '"
,Y27 as "' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '"
,Y28 as "' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '"
,Y29 as "' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '"
,Y30 as "' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '"
,Y31 as "' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '"
,Y32 as "' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '"
,Y33 as "' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '"
,Y34 as "' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '"
,Y35 as "' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '"
FROM ' ||
v_tblInsertType || ') UP
UNPIVOT
(FieldValue FOR FieldYear IN
("' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '"
,"' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '"
)
) unpvt' ;
Execute immediate v_SQL;

Related

represent vertical output horizontally in oracle

my display cade looks like below i want to covert my display from vertical to horizontal
code:-
dbms_output.put_line('output1 : ' || value1);
dbms_output.put_line('output2 : ' || value2);
dbms_output.put_line('output3 : ' || value3);
dbms_output.put_line('output4 : ' || value4);
dbms_output.put_line('output5 : ' || value5);
dbms_output.put_line('output6 : ' || value6);
output:-
output1 : 100
output2 : 200
output3 : 300
output4 : 400
output5 : 500
output6 : 600
I want output like below:-
output1 output2 output3 output4 output5 output6
100 200 300 400 500 600
As a first step, you will need to assign rows to the columns
select output1,output2,....., row_number() over (partition by value1 ,....order by output) col from yourtable
then you add PIVOT:
PIVOT
(
max(output)
for col in (1, 2, 3, ..., 240)
)```
You can use LPAD:
DECLARE
value1 NUMBER(8,2) := 100;
value2 NUMBER(8,2) := 200;
value3 NUMBER(8,2) := 300;
value4 NUMBER(8,2) := 400;
value5 NUMBER(8,2) := 500;
value6 NUMBER(8,2) := 600;
c_width CONSTANT PLS_INTEGER := 10;
BEGIN
dbms_output.put_line(
LPAD('output1', c_width, ' ')
|| ' ' || LPAD('output2', c_width, ' ')
|| ' ' || LPAD('output3', c_width, ' ')
|| ' ' || LPAD('output4', c_width, ' ')
|| ' ' || LPAD('output5', c_width, ' ')
|| ' ' || LPAD('output6', c_width, ' ')
);
dbms_output.put_line(
LPAD(value1, c_width, ' ')
|| ' ' || LPAD(value2, c_width, ' ')
|| ' ' || LPAD(value3, c_width, ' ')
|| ' ' || LPAD(value4, c_width, ' ')
|| ' ' || LPAD(value5, c_width, ' ')
|| ' ' || LPAD(value6, c_width, ' ')
);
END;
/
Which outputs:
output1 output2 output3 output4 output5 output6
100 200 300 400 500 600
or TO_CHAR:
DECLARE
value1 NUMBER(8,2) := 100;
value2 NUMBER(8,2) := 200;
value3 NUMBER(8,2) := 300;
value4 NUMBER(8,2) := 400;
value5 NUMBER(8,2) := 500;
value6 NUMBER(8,2) := 600;
c_width CONSTANT PLS_INTEGER := 10;
c_format CONSTANT VARCHAR2(10) := '999999.99';
BEGIN
dbms_output.put_line(
LPAD('output1', c_width, ' ')
|| ' ' || LPAD('output2', c_width, ' ')
|| ' ' || LPAD('output3', c_width, ' ')
|| ' ' || LPAD('output4', c_width, ' ')
|| ' ' || LPAD('output5', c_width, ' ')
|| ' ' || LPAD('output6', c_width, ' ')
);
dbms_output.put_line(
TO_CHAR(value1, c_format)
|| ' ' || TO_CHAR(value2, c_format)
|| ' ' || TO_CHAR(value3, c_format)
|| ' ' || TO_CHAR(value4, c_format)
|| ' ' || TO_CHAR(value5, c_format)
|| ' ' || TO_CHAR(value6, c_format)
);
END;
/
Which outputs:
output1 output2 output3 output4 output5 output6
100.00 200.00 300.00 400.00 500.00 600.00
db<>fiddle here

I want to write dynamic query from update statement to merge merge statement

I want to write dynamic query from update statement to merge merge statement
'UPDATE ' || T1_TABLENAME || ' t1 ' || 'SET ( ' ||
v_t1_fields || ' ) = (SELECT ' || v_t2_fields || ' FROM ' ||
T2_TEMPTABLE_NAME || ' tmp WHERE ' || v_con || ' ) ' ||
' WHERE EXISTS ( SELECT 1 FROM ' || T2_TEMPTABLE_NAME ||
' tmp WHERE ' || v_con || ' )';
-- HERE v_con = t1.D=t2.D,
v_t1_fields-it can store dynamically-A,B,C
v_t2_fields-it can store dynamically-A,B,C
------------
MERGE INTO TABLE1 t1
USING TABLE2 t2
ON(t1.D=t2.D)
WHEN MATCHED THEN
UPDATE
SET
t1.A=t2.A,
t1.B=t2.B, //update set (t1.A,t1.B,t1.C =t2.A,t2.B,t2.C) not work
t1.C=t2.C;
------------------
'MERGE INTO ' TABLE1 || ' t1 ' ||
' USING ' TABLE2 || ' t2 ' ||
' ON ( ' || v_cons || ' )
when matched then update set ('
|| v_t1_fields || ') = '( || v_t2_fields || ' );' // Its not work--ORA-01747: invalid user.table.column, table.column, or column specification
then i use reg_exp to split the columns
---------------------------
v_Sql := 'MERGE INTO ' TABLE1 || ' t1 ' ||
' USING ' TABLE2 || ' t2 ' ||
' ON ( ' || v_cons || ' )
when matched then update set ('
( ' || regexp_substr(v_t1_fields, '[^,]+', 1, 1) || ' ) = ( ' || regexp_substr(v_t2_fields, '[^,]+', 1, 1) || ' )
( ' || regexp_substr(v_t1_fields, '[^,]+', 1, 2) || ' ) = ( ' || regexp_substr(v_t2_fields, '[^,]+', 1, 2) || ' )
( ' || regexp_substr(v_t1_fields, '[^,]+', 1,3) || ' ) = ( ' || regexp_substr(v_t2_fields, '[^,]+', 1,3) || ' ) ';
this one also not work ----ORA-01747: invalid user.table.column, table.column, or column specification
dynamic update to dynamic merge
while changing merge with update statement
not working
There are few mistakes in concatenation and usage of the MERGE statement.
Try the following dynamic query:
'MERGE INTO '
|| TABLE1
|| ' t1 '
|| ' USING '
|| TABLE2
|| ' t2 '
|| ' ON ( '
|| V_CONS
|| ' ) when matched then update set '
|| REGEXP_SUBSTR(V_T1_FIELDS, '[^,]+', 1, 1)
|| ' = '
|| REGEXP_SUBSTR(V_T2_FIELDS, '[^,]+', 1, 1)
|| ', '
|| REGEXP_SUBSTR(V_T1_FIELDS, '[^,]+', 1, 2)
|| ' = '
|| REGEXP_SUBSTR(V_T2_FIELDS, '[^,]+', 1, 2)
|| ', '
|| REGEXP_SUBSTR(V_T1_FIELDS, '[^,]+', 1, 3)
|| ' = '
|| REGEXP_SUBSTR(V_T2_FIELDS, '[^,]+', 1, 3);
I am assuming that
TABLE1 and TABLE2 are valid table names
V_CONS is a valid condition on tables
V_T1_FIELDS and V_T2_FIELDS contains valid column names of the table
Cheers!!

ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: " while executing code

for i in (select * from order_dtls order by 1 asc) loop
select b.dealer_id
into n_dealer
from booking_mst b
where b.booking_id = i.booking_id;
dbms_output.put_line(n_cnt ||'--'|| to_char(sysdate,'yyyymmdd') ||'--'|| chr(1) || '43' ||'--'||
chr(1) || n_dealer ||'--'|| '1525' ||'--'|| '0' ||'--'|| i.amount ||'--'||
chr(1) || i.product_id ||'--'|| chr(1) || i.quantity ||'--'||
chr(1) || to_char(i.order_date,'yyyymmdd') + 1 ||'--'|| chr(1) ||
to_char(i.order_date,'yyyymmdd'));
commit;
n_cnt := n_cnt + 1;
end loop;
The issue is here:
. . . || to_char(i.order_date,'yyyymmdd') + 1 ||
Which is currently seen as:
(. . . || to_char(i.order_date,'yyyymmdd')) + 1 ||
The implicit number conversion fails for the above and hence the error.
Enclose the addition to_char(i.order_date,'yyyymmdd') + 1 inside parentheses:
dbms_output.put_line(n_cnt ||'--'|| to_char(sysdate,'yyyymmdd') ||'--'|| chr(1) || '43' ||'--'||
chr(1) || n_dealer ||'--'|| '1525' ||'--'|| '0' ||'--'|| i.amount ||'--'||
chr(1) || i.product_id ||'--'|| chr(1) || i.quantity ||'--'||
chr(1) || (to_char(i.order_date,'yyyymmdd') + 1) ||'--'|| chr(1) ||
to_char(i.order_date,'yyyymmdd'));
EDIT:
As noted by Wernfried, there is another problem in your code. You should add 1 to the date, not the result string.
So correct code will be:
inside parentheses:
dbms_output.put_line(n_cnt ||'--'|| to_char(sysdate,'yyyymmdd') ||'--'|| chr(1) || '43' ||'--'||
chr(1) || n_dealer ||'--'|| '1525' ||'--'|| '0' ||'--'|| i.amount ||'--'||
chr(1) || i.product_id ||'--'|| chr(1) || i.quantity ||'--'||
chr(1) || to_char(i.order_date + 1,'yyyymmdd') ||'--'|| chr(1) ||
to_char(i.order_date,'yyyymmdd'));

Elasticsearch Routing allocation awarness and shard allocation

I have an elasticsearch cluster containing 10 nodes splited across 3 zones (A, B, C).
The ES configuration file contain:
cluster.routing.allocation.awareness.force.zone.values = "A,B,C";
I have a single index (index1) containing =~ 283674 docs with a size of =~ 231MB.
The setting for this index contain:
"number_of_replicas" : "2", "number_of_shards" : "5",
Shards seems correctly distributed across zone but within a single zone they are not evenly distributed (see table below, E.g. Zone C | S = shard, (P = Primary | S = Secondary)
What can I do to correctly distribute my shards?
++------+----------------------------------------------------+
||Zone A-----------------------------------------------------|
|| | ||
|| Node 1| S1 (P) ||
|| +-----------------------------------------------------+ ||
|| | ||
|| | S2 (S) S4 (S) ||
|| | ||
|| Node 2| ||
|| +-----------------------------------------------------+ ||
|| | ||
|| | ||
|| | S0 (S) S3 (S) ||
|| | ||
|| Node 3| ||
|-------+----------------------------------------------------|
||Zone B-----------------------------------------------------|
|| | ||
|| | S0 (P) ||
|| Node 4| ||
|| +-----------------------------------------------------+ ||
|| | ||
|| | ||
|| | S3 (P) S4 (P) ||
|| Node 5| ||
|| +-----------------------------------------------------+ ||
|| | ||
|| | ||
|| | S1 (S) S2 (S) ||
|| Node 6| ||
|-------+----------------------------------------------------|
||Zone C-----------------------------------------------------|
|| | ||
|| | S0 (S) S1 (S) S2 (P) S3 (S) S4 (S) ||
|| Node 7| ||
|| +------------------------------------------------------+ ||
|| | ||
|| | ||
|| | ||
|| Node 8| ||
|| +------------------------------------------------------+ ||
|| | ||
|| | ||
|| | ||
|| Node 9| ||
|| +------------------------------------------------------+ ||
|| | ||
|| | ||
|| - ||
|| Node 10 ||
|------------------------------------------------------------|
+------------------------------------------------------------+
I fixed the issue by forcing the number of shard per host and changing the number of primary / secondary per index:
index.number_of_shards = "5";
index.number_of_replicas = "3";
index.routing.allocation.total_shards_per_node = "2";
This results in 2 shards per node evenly distributed across zone.

WHERE CURRENT OF raising and invalid rowid error

I'm trying to increase the age of every customer by 1 and display them; using a cursor.
This is the Table structure.
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
Here is my code:
DECLARE
c_id customers.id%TYPE;
c_name customers.name%TYPE;
c_age customers.age%TYPE;
CURSOR c1 IS
SELECT id, name, age
FROM customers
FOR UPDATE OF salary;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c_id, c_name, c_age;
UPDATE customers
SET age = age + 1
WHERE CURRENT OF c1;
EXIT WHEN c1%NOTFOUND;
dbms_output.put_line( c_id || ' ' || c_name || ' ' || c_age );
END LOOP;
CLOSE c1;
END;
/
However, I am getting the following error:
Error report:
ORA-01410: invalid ROWID
ORA-06512: at line 13
01410. 00000 - "invalid ROWID"
*Cause:
*Action:
1 Ramesh 32
2 Khilan 25
3 kaushik 23
4 Chaitali 25
5 Hardik 27
6 Komal 22
Why is this happening and how can I stop it?
You have your exit in the wrong place; it should be straight after the fetch. You are processing your six real rows properly, but then you have a seventh fetch - after which %notfound will be true - so there is no 'current' row to update. You need to exit before you try to do anything with that invalid row.
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c_id, c_name, c_age;
EXIT WHEN c1%NOTFOUND;
UPDATE customers
SET age = age + 1
WHERE CURRENT OF c1;
dbms_output.put_line( c_id || ' ' || c_name || ' ' || c_age );
END LOOP;
CLOSE c1;
END;
Hopefully this is just an exercise, as it's not a very efficient way to do the updates.

Resources