Why this regexp in Oracle SQL loses the beginning of string? - sql

I'm splitting a string p_value using p_delimeter, which may contain one or more symbols (that's why regexp is not like often used [^,]+).
In most of the cases the following query works predictably, but I'm dazed with a case when:
string p_value contains line break chr(10),
p_value doesn't contain p_delimeter as substring,
so I expect to have one row with the whole p_value as the result, but got only the remainder after line break.
It's supposed here that regexp treats line break as ordinary symbol, since
the 'm' modifier is absent in call to regexp_substr.
Please, explain is this behavior correct and how to get the expected result.
WITH
params AS (SELECT 'ab' || chr(10) || 'cd' p_value,
'xxx' p_delimeter
FROM dual
)
SELECT regexp_substr(p_value, '(.*?)(' || p_delimeter || '|$)', 1, level, 'c', 1) AS CUT
FROM params
CONNECT BY regexp_substr(p_value, '(.*?)(' || p_delimeter || '|$)', 1, level, 'c', 1) IS NOT NULL;
Actual result: Expected result:
----- ------
CUT CUT
----- ------
cd ab/cd
^
'this is just a marker for a line break [= chr(10)]'

Allow the . pattern to match all characters by adding the n flag to the regular expression:
WITH params ( p_value, p_delimiter ) AS (
SELECT 'ab' || chr(10) || 'cd', 'xxx' FROM dual
)
SELECT REGEXP_SUBSTR(p_value, '(.*?)(' || p_delimeter || '|$)', 1, level, 'cn', 1) AS CUT
FROM params
CONNECT BY LEVEL < REGEXP_COUNT( p_value, '(.*?)(' || p_delimeter || '|$)' );
or you can use a simple function:
Oracle Setup:
CREATE TYPE VARCHAR2_TABLE AS TABLE OF VARCHAR2(4000);
/
CREATE OR REPLACE FUNCTION split_String(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN VARCHAR2_TABLE DETERMINISTIC
AS
p_result VARCHAR2_TABLE := VARCHAR2_TABLE();
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
END IF;
END IF;
RETURN p_result;
END;
/
Query:
WITH params ( p_value, p_delimiter ) AS (
SELECT 'ab' || chr(10) || 'cd', 'xxx' FROM dual
)
SELECT COLUMN_VALUE AS CUT
FROM params,
TABLE( split_String( p_value, p_delimiter ) );

Related

ORA-06502: PL/SQL: numeric or value error: character to number conversion error in package function

I am trying to create a function that returns pipelined table with input parameters being varchar2, date or number. I am trying to input amount as varchar2 because I can't process it like a number with comma before decimals. Here is body of my package function:
FUNCTION kreiraj_reprogram_rate (p_sifra_kupca VARCHAR2,
p_id_kartice_kupca NUMBER,
p_iznos_s_pdv VARCHAR2,
p_broj_rata NUMBER,
p_frekvencija_dospijeca NUMBER,
p_datum_dospijeca VARCHAR2,
p_datum_dokumenta VARCHAR2)
RETURN reprogram_rate_table
PIPELINED
IS
v_data reprogram_rate_rec;
v_user VARCHAR2 (200) := mc__secure_context.get_context_username ();
v_iznos_rate NUMBER;
v_broj_rate NUMBER;
v_zadnja_rata NUMBER;
v_datum_dokumenta DATE;
v_datum_dospijeca DATE;
BEGIN
write_log_table ('TEST','p_iznos_s_pdv: '|| to_number(p_iznos_s_pdv));
v_datum_dospijeca := TO_DATE (p_datum_dospijeca, 'dd.mm.yyyy');
v_datum_dokumenta := TO_DATE (p_datum_dokumenta, 'dd.mm.yyyy');
v_iznos_rate := ROUND (to_number(p_iznos_s_pdv) / p_broj_rata, 2);
v_zadnja_rata :=
p_iznos_s_pdv - ROUND ((p_broj_rata - 1) * v_iznos_rate, 2);
v_broj_rate := 1;
FOR r IN 1 .. p_broj_rata
LOOP
IF v_broj_rate = p_broj_rata
THEN
v_data.osnovica := ROUND ((v_zadnja_rata / 1.25), 2);
v_data.pdv := ROUND (v_zadnja_rata * 0.2, 2);
v_data.ukupno := v_zadnja_rata;
ELSE
v_data.osnovica := ROUND ((v_iznos_rate / 1.25), 2);
v_data.pdv := ROUND (v_iznos_rate * 0.2, 2);
v_data.ukupno := ROUND (v_iznos_rate, 2);
END IF;
v_data.sifra_kupca := p_sifra_kupca;
v_data.kartica_kupca := p_id_kartice_kupca;
v_data.broj_rate := v_broj_rate;
v_data.datum_dokumenta := v_datum_dokumenta;
v_data.datum_dospijeca := v_datum_dospijeca;
PIPE ROW (v_data);
v_broj_rate := v_broj_rate + 1;
v_datum_dospijeca :=
CASE
WHEN p_frekvencija_dospijeca = 1
THEN
ADD_MONTHS (v_datum_dospijeca, 1)
WHEN p_frekvencija_dospijeca = 3
THEN
ADD_MONTHS (v_datum_dospijeca, 3)
WHEN p_frekvencija_dospijeca = 6
THEN
ADD_MONTHS (v_datum_dospijeca, 6)
WHEN p_frekvencija_dospijeca = 12
THEN
ADD_MONTHS (v_datum_dospijeca, 12)
END;
END LOOP;
END kreiraj_reprogram_rate;
And the error says that error is on the first write_log_table line where I try to convert value 4159,57 to number.
I tried select to_number('4159,57') from dual in SQL developer and it works just fine so I can't figure out what is the catch.
Can someone help?
Thanks.

Query to find the tables size occupied in GB in Oracle SQL for various SCHEMAS

I am trying to find the space occupied by list of tables in Oracle DB (for various SCHEMAS ) ,currently i am using this query
QUERY 1 - DBA_SEGMENTS
SELECT SEGMENT_NAME,SUM (BYTES)/1024/1024/1024 AS GB,
ROUND(100*RATIO_TO_REPORT(GB) OVER (), 4) PERCENTAGE
FROM DBA_SEGMENTS
WHERE OWNER='SCHEMA_NAME'
AND SEGMENT_TYPE ='TABLE'
AND SEGMENT_NAME
IN
(
'LIST OF TABLES'
)
GROUP BY SEGMENT_NAME,BYTES
ORDER BY GB DESC;
QUERY 2 - DBA_OBJECTS
SELECT DISTINCT OBJECT_NAME FROM DBA_OBJECTS
WHERE OWNER='SCHEMA'
AND OBJECT_NAME IN
(
'LIST OF TABLES'
)
GROUP BY OBJECT_NAME
ORDER BY OBJECT_NAME;
when i compare the LIST OF TABLES counts it's not matching , what could be the reason ?
can you please suggest is this correct or is there a better way to achieve this ?
Reference
https://serverfault.com/questions/132620/oracle-how-to-find-out-storage-space-used-by-a-table
Additional Query 1
SET SERVEROUTPUT ON;
DECLARE
input_owner NVARCHAR2(128) := 'SCHEMA_NAME';
segment_size_blocks NUMBER;
segment_size_bytes NUMBER;
used_blocks NUMBER;
used_bytes NUMBER;
expired_blocks NUMBER;
expired_bytes NUMBER;
unexpired_blocks NUMBER;
unexpired_bytes NUMBER;
total_blocks NUMBER;
total_bytes NUMBER;
unused_blocks NUMBER;
unused_bytes NUMBER;
last_ext_file_id NUMBER;
last_ext_blk_id NUMBER;
last_used_blk NUMBER;
result_table NVARCHAR2(128);
result_segment_type NVARCHAR2(128);
result_used_mb NUMBER;
result_unused_mb NUMBER;
result_total_mb NUMBER;
CURSOR cur
IS
SELECT
s.segment_name AS segment_name,
s.owner AS segment_owner,
s.partition_name AS partition_name,
s.segment_type AS segment_type,
CASE WHEN s.segment_type IN ('TABLE', 'TABLE PARTITION', 'TABLE SUBPARTITION')
THEN s.segment_name
WHEN s.segment_type IN ('INDEX', 'INDEX PARTITION', 'INDEX SUBPARTITION')
THEN (SELECT i.table_name
FROM dba_indexes i
WHERE s.segment_name = i.index_name AND s.owner = i.owner)
WHEN s.segment_type IN ('LOBSEGMENT', 'LOB PARTITION')
THEN (SELECT l.table_name
FROM dba_lobs l
WHERE s.segment_name = l.segment_name AND s.owner = l.owner)
WHEN s.segment_type IN ('LOBINDEX')
THEN (SELECT l.table_name
FROM dba_lobs l
WHERE s.segment_name = l.index_name AND s.owner = l.owner)
ELSE 'Unknown'
END AS table_name,
s.bytes AS segment_bytes
FROM dba_segments s
WHERE owner = input_owner
and segment_name in
(
)
ORDER BY table_name, segment_type;
BEGIN
dbms_output.put_line('table ; segment type ; used (mb) ; unused (mb) ; total (mb)');
FOR ro IN cur
LOOP
result_table := ro.table_name;
result_segment_type := ro.segment_type;
IF ro.segment_type IN ('TABLE', 'INDEX')
THEN
dbms_space.unused_space(
segment_owner => ro.segment_owner,
segment_name => ro.segment_name,
segment_type => ro.segment_type,
total_blocks => total_blocks,
total_bytes => total_bytes,
unused_blocks => unused_blocks,
unused_bytes => unused_bytes,
last_used_extent_file_id => last_ext_file_id,
last_used_extent_block_id => last_ext_blk_id,
last_used_block => last_used_blk);
result_used_mb := (total_bytes - unused_bytes) / 1024 / 1024;
result_unused_mb := unused_bytes / 1024 / 1024;
result_total_mb := total_bytes / 1024 / 1024;
ELSIF ro.segment_type IN ('LOBSEGMENT')
THEN
dbms_space.space_usage(
segment_owner => ro.segment_owner,
segment_name => ro.segment_name,
segment_type => 'LOB',
partition_name => ro.partition_name,
segment_size_blocks => segment_size_blocks,
segment_size_bytes => segment_size_bytes,
used_blocks => used_blocks,
used_bytes => used_bytes,
expired_blocks => expired_blocks,
expired_bytes => expired_bytes,
unexpired_blocks => unexpired_blocks,
unexpired_bytes => unexpired_bytes
);
result_used_mb := used_bytes / 1024 / 1024;
result_unused_mb := (segment_size_bytes - used_bytes) / 1024 / 1024;
result_total_mb := segment_size_bytes / 1024 / 1024;
ELSE
-- TODO ??
result_used_mb := ro.segment_bytes / 1024 / 1024;
result_unused_mb := 0;
result_total_mb := result_used_mb + result_unused_mb;
END IF;
dbms_output.put_line(
RPAD(result_table, 30) || '; ' ||
RPAD(result_segment_type, 20)|| '; ' ||
TO_CHAR(result_used_mb / 1024 / 1024, '999999999990D00')|| '; ' ||
TO_CHAR(result_unused_mb / 1024 / 1024, '999999999990D00')|| '; ' ||
TO_CHAR(result_total_mb / 1024 / 1024, '999999999990D00'));
END LOOP;
END;
Additional Query 2
WITH DA AS (
SELECT OWNER, SEGMENT_NAME, SUM(BYTES)/1024/1024 SIZE_MB
FROM DBA_EXTENTS
GROUP BY ROLLUP(OWNER, SEGMENT_NAME)
) SELECT OWNER, SEGMENT_NAME, SIZE_MB, ROUND(SIZE_MB/TOTAL_MB*100)
FROM DA
CROSS JOIN (
SELECT SIZE_MB AS TOTAL_MB
FROM DA T WHERE OWNER IS NULL AND SEGMENT_NAME IS NULL
)
ORDER BY SIZE_MB DESC
Here is a fairly accurate query that takes tables, indexes, and any partitioned tables/indexes into account for all tables for a single schema owner. Note that there will be some additional overhead in actual space used due to blocksize, file headers, etc.
select segment_name
, segment_type
, sum(bytes)/1024/1024/1024 as GB
, ROUND(100*RATIO_TO_REPORT(bytes) OVER (), 4) PERCENTAGE
from dba_segments ds
where
owner ='SCOTT'
and (
segment_type like 'TABLE%'
and segment_name in ('EMP', 'SALGRADE', 'EMP_BKP', 'DEPT')
or
(
segment_type like 'INDEX%'
and segment_name in (
select index_name from dba_indexes di
where owner ='SCOTT'
and di.table_name in ('EMP', 'SALGRADE', 'EMP_BKP', 'DEPT')
)
-- more fun here for LOBs, etc.
)
)
group by segment_name, segment_type, bytes
order by GB desc;
Take a look at DBA_LOBS for additional criteria to add to the -- more fun here to pick up LOB information.

PL/SQL cursor that skip rows recording in database

I can`t understand why cursor that I have in my procedure
skip rows.
Here is my cursor:
OPEN c_denormalized_data FOR lv_sql_str;
LOOP
FETCH c_denormalized_data
INTO r_denormalized_data;
EXIT WHEN c_denormalized_data%NOTFOUND;
INSERT INTO bpdev.bp_real_to_fiskal_reference
(r_customer_trx_id,
r_trx_number,
r_trx_date,
r_amount,
r_amount_due_rem,
diff,
f_trx_number,
f_trx_date,
f_amount,
NULLS,
notnulls,
org_id,
data_loaded_for_days)
VALUES
(r_denormalized_data.r_customer_trx_id,
r_denormalized_data.r_trx_number,
r_denormalized_data.r_trx_date,
r_denormalized_data.r_amount,
r_denormalized_data.r_amount_due_rem,
r_denormalized_data.diff,
r_denormalized_data.f_trx_number,
r_denormalized_data.f_trx_date,
r_denormalized_data.f_amount,
r_denormalized_data.nulls,
r_denormalized_data.notnulls,
r_denormalized_data.org_id,
days_before_today);
COMMIT;
END LOOP;
lv_sql_str is dynamic SELECT the execution of which takes about 30 min.
The cursor is used because this select is dynamically generated based on the input parameters.
If I execute only a SELECT statement I receive all data that I expect, but when I execute procedure to insert in database I losе records
that appear in the data returned by my SELECT.
I can't understand why these records are skipped and are not inserted in the table.
Does anyone have an idea where my mistake is?
Here is my full code:
PROCEDURE load_denormalized_data(errbuf OUT NOCOPY VARCHAR2,
retcode OUT NOCOPY NUMBER,
days_before_today IN NUMBER,
real_legal_entyties_id_list IN VARCHAR2) IS
TYPE tc_denormalized_data IS REF CURSOR;
c_denormalized_data tc_denormalized_data;
TYPE tr_denormalized_data IS RECORD(
r_customer_trx_id bpdev.bp_real_to_fiskal_reference.r_customer_trx_id%TYPE,
r_trx_number bpdev.bp_real_to_fiskal_reference.r_trx_number%TYPE,
r_trx_date bpdev.bp_real_to_fiskal_reference.r_trx_date%TYPE,
r_amount bpdev.bp_real_to_fiskal_reference.r_amount%TYPE,
r_amount_due_rem bpdev.bp_real_to_fiskal_reference.r_amount_due_rem%TYPE,
diff bpdev.bp_real_to_fiskal_reference.diff%TYPE,
f_trx_number bpdev.bp_real_to_fiskal_reference.f_trx_number%TYPE,
f_trx_date bpdev.bp_real_to_fiskal_reference.f_trx_date%TYPE,
f_amount bpdev.bp_real_to_fiskal_reference.f_amount%TYPE,
NULLS bpdev.bp_real_to_fiskal_reference.nulls%TYPE,
notnulls bpdev.bp_real_to_fiskal_reference.notnulls%TYPE,
org_id bpdev.bp_real_to_fiskal_reference.org_id%TYPE);
r_denormalized_data tr_denormalized_data;
lv_where_le_ids VARCHAR2(3000);
lv_sql_str VARCHAR2(5000);
BEGIN
bpdev.bp_utils.put_log('>>>--------ПОТРЕБИТЕЛСКИ СЪОБЩЕНИЯ--------<<<');
bpdev.bp_utils.put_log('Изтриване на старите данни.');
DELETE bpdev.bp_real_to_fiskal_reference rfr;
COMMIT;
lv_where_le_ids := bp_utils.set_where_clause('invr.org_id',
real_legal_entyties_id_list,
',',
';',
0);
lv_sql_str :=
'SELECT invr.customer_trx_id r_customer_trx_id,
invr.trx_number r_trx_number,
invr.trx_date r_trx_date,
round(invr.extended_amount, 2) r_amount,
round(nvl(ps.amount_due_remaining, invr.extended_amount), 2) r_amount_due_rem,
round(invr.amount_without_credits - invf.extended_amount, 3) diff,
invf.trx_number f_trx_number,
invf.trx_date f_trx_date,
round(invf.extended_amount, 2) f_amount,
round(invr.extended_amount +
nvl((SELECT SUM(psa.amount_credited)
FROM ar_payment_schedules_all psa
WHERE psa.customer_trx_id = invr.customer_trx_id),
0) - invf.extended_amount,
1) NULLS,
round(nvl(ps.amount_due_remaining, (invr.extended_amount * 1.2)), 2) notnulls,
invr.org_id
FROM (SELECT cth.customer_trx_id,
cth.trx_number,
cth.trx_date,
SUM(ctl.extended_amount) * 1.2 extended_amount,
round(((SUM(ctl.extended_amount) * 1.2 +
nvl((SELECT SUM(psa.amount_credited)
FROM ar_payment_schedules_all psa
WHERE psa.customer_trx_id = cth.customer_trx_id),
0))),
2) amount_without_credits,
cth.org_id,
nvl(decode(TRIM(ctl.interface_line_attribute13),
0,
NULL,
TRIM(ctl.interface_line_attribute13)),
cth.interface_header_attribute13) interface_line_attribute13
FROM ra_customer_trx_all cth,
ra_customer_trx_lines_all ctl
WHERE cth.customer_trx_id = ctl.customer_trx_id AND
ctl.line_type = ''LINE'' AND
ctl.set_of_books_id = 1001
GROUP BY cth.customer_trx_id,
cth.trx_number,
cth.trx_date,
cth.org_id,
nvl(decode(TRIM(ctl.interface_line_attribute13),
0,
NULL,
TRIM(ctl.interface_line_attribute13)),
cth.interface_header_attribute13)) invr,
(SELECT cth.customer_trx_id,
cth.trx_number,
cth.trx_date,
SUM(ctl.extended_amount) * 1.2 extended_amount,
round(((SUM(ctl.extended_amount) * 1.2 +
nvl((SELECT SUM(psa.amount_credited)
FROM ar_payment_schedules_all psa
WHERE psa.customer_trx_id = cth.customer_trx_id),
0))),
2) amount_without_credits,
cth.org_id,
nvl(decode(TRIM(ctl.interface_line_attribute13),
0,
NULL,
TRIM(ctl.interface_line_attribute13)),
cth.interface_header_attribute13) interface_line_attribute13
FROM ra_customer_trx_all cth,
ra_customer_trx_lines_all ctl
WHERE cth.customer_trx_id = ctl.customer_trx_id AND
ctl.line_type = ''LINE'' AND
cth.set_of_books_id = 2001
GROUP BY cth.customer_trx_id,
cth.trx_number,
cth.trx_date,
cth.org_id,
nvl(decode(TRIM(ctl.interface_line_attribute13),
0,
NULL,
TRIM(ctl.interface_line_attribute13)),
cth.interface_header_attribute13)) invf,
ar_payment_schedules_all ps
WHERE invf.interface_line_attribute13(+) = invr.customer_trx_id AND
ps.customer_trx_id(+) = invr.customer_trx_id AND
invr.trx_date >= SYSDATE - ' ||
days_before_today || ' ' || lv_where_le_ids;
bpdev.bp_utils.put_log('Зареждане на новите данни.');
OPEN c_denormalized_data FOR lv_sql_str;
LOOP
FETCH c_denormalized_data
INTO r_denormalized_data;
EXIT WHEN c_denormalized_data%NOTFOUND;
INSERT INTO bpdev.bp_real_to_fiskal_reference
(r_customer_trx_id,
r_trx_number,
r_trx_date,
r_amount,
r_amount_due_rem,
diff,
f_trx_number,
f_trx_date,
f_amount,
NULLS,
notnulls,
org_id,
data_loaded_for_days)
VALUES
(r_denormalized_data.r_customer_trx_id,
r_denormalized_data.r_trx_number,
r_denormalized_data.r_trx_date,
r_denormalized_data.r_amount,
r_denormalized_data.r_amount_due_rem,
r_denormalized_data.diff,
r_denormalized_data.f_trx_number,
r_denormalized_data.f_trx_date,
r_denormalized_data.f_amount,
r_denormalized_data.nulls,
r_denormalized_data.notnulls,
r_denormalized_data.org_id,
days_before_today);
END LOOP;
COMMIT;
CLOSE c_denormalized_data;
bpdev.bp_utils.put_log('>>>------КРАЙ ПОТРЕБИТЕЛСКИ СЪОБЩЕНИЯ-----<<<');
EXCEPTION
WHEN OTHERS THEN
bpdev.bp_utils.put_log(retcode || errbuf);
END load_denormalized_data;
we dont have all info , but I suspect the problem is here
invr.trx_date >= SYSDATE - ' || days_before_today || ' ' || lv_where_le_ids;
try to add the date you expect as static then try again.

Multiple tables in SELECT query

I'm trying to select columns from multiple tables. Here's the code I have:
SELECT
Sum(IIf((tblChain.InitialZone="1"),1,0)) AS SCCH,
tblStatHistory.HomeTeam,
Sum(IIf(([Pentagraph]="HANDB") Or ([Pentagraph]="HANBB") Or ([Pentagraph]="HBCL") Or
([Pentagraph]="KBBW") Or ([Pentagraph]="KBCL") Or ([Pentagraph]="KBIN") Or
([Pentagraph]="KBLO") Or ([Pentagraph]="KBSH") Or ([Pentagraph]="KKBW") Or
([Pentagraph]="KKCL") Or ([Pentagraph]="KKGKN") Or ([Pentagraph]="KKBW") Or
([Pentagraph]="KKGKO") Or ([Pentagraph]="KKGKP") Or([Pentagraph]="KKIN") Or
([Pentagraph]="KKLO") Or ([Pentagraph]="KKSH"),1,0)) AS D,
Sum(IIf(([Pentagraph]="KBBW") Or ([Pentagraph]="KBCL") Or ([Pentagraph]="KBIN") Or
([Pentagraph]="KBLO") Or ([Pentagraph]="KBSH") Or ([Pentagraph]="KKBW") Or
([Pentagraph]="KKCL") Or ([Pentagraph]="KKGKN") Or ([Pentagraph]="KKBW") Or
([Pentagraph]="KKGKO") Or ([Pentagraph]="KKGKP") Or ([Pentagraph]="KKIN") Or
([Pentagraph]="KKLO") Or ([Pentagraph]="KKSH"),1,0)) AS K,
Sum(IIf(([Pentagraph]="KBBW") Or ([Pentagraph]="KKBW"),1,0)) AS K_Back,
Sum(IIf(([Pentagraph]="MACOO") Or ([Pentagraph]="MACOP") Or ([Pentagraph]="MAPAO") Or
([Pentagraph]="MAPAP") Or ([Pentagraph]="MAOL") Or ([Pentagraph]="MAUNO") Or
([Pentagraph]="MAUNP"),1,0)) AS M,
Sum(IIf(([Pentagraph]="MAERO") Or ([Pentagraph]="MAERP") Or ([Pentagraph]="MAUNO") Or
([Pentagraph]="MAUC") Or ([Pentagraph]="MAUNP") Or ([Pentagraph]="MAOL"),1,0)) AS UM,
Sum(IIf(([Pentagraph]="MACOO") Or ([Pentagraph]="MACOP"),1,0)) AS CM,
Sum(IIf(([Pentagraph]="HANDB") Or ([Pentagraph]="HANBB") Or ([Pentagraph]="HBCL"),1,0)) AS H,
Sum(IIf(([Pentagraph]="CBCL") Or ([Pentagraph]="BUCL") Or ([Pentagraph]="TICL"),1,0)) AS C,
Sum(IIf(([Pentagraph]="CBCL"),1,0)) AS C_CSB,
Sum(IIf(([Pentagraph]="BUCL") Or ([Pentagraph]="TICL"),1,0)) AS C_BUTI,
Sum(IIf(([Pentagraph]="CBFP") Or ([Pentagraph]="BUFP") Or ([Pentagraph]="TIFP"),1,0)) AS FP,
Sum(IIf(([Pentagraph]="BUFP") Or ([Pentagraph]="TIFP"),1,0)) AS FP_BUTI,
Sum(IIf(([Pentagraph]="CBFP"),1,0)) AS FP_CSB,
Sum(IIf(([Pentagraph]="CBHO") Or ([Pentagraph]="BUHO") Or ([Pentagraph]="TIHO"),1,0)) AS HO,
Sum(IIf(([Pentagraph]="BUHO") Or ([Pentagraph]="TIHO"),1,0)) AS HO_BUTI,
Sum(IIf(([Pentagraph]="CBHO"),1,0)) AS HO_CSB,
Sum(IIf(([Pentagraph]="IN50"),1,0)) AS I50,
Sum(IIf(([Pentagraph]="GOAL"),1,0)) AS GOAL,
Sum(IIf(([Pentagraph]="BEHI"),1,0)) AS BHND,
Sum(IIf(([Pentagraph]="TACKN") Or ([Pentagraph]="TACKO") Or ([Pentagraph]="TACKP") Or
(([Pentagraph]="RDTDD") And ([ROLE]=1)) Or (([Pentagraph]="TACKL") And ([ROLE]=1)) Or
(([Pentagraph]="RDTAK") And ([ROLE]=1)) Or (([Pentagraph]="DISP") And ([ROLE]=1)),1,0)) AS T,
Sum(IIf(([Pentagraph]="GEHAN") Or ([Pentagraph]="GEHAO") Or ([Pentagraph]="GEHAP") Or ([Pentagraph]="GEHCN") Or ([Pentagraph]="GEHCO") Or([Pentagraph]="GEHCP") Or
([Pentagraph]="GERU") Or ([Pentagraph]="GELON") Or ([Pentagraph]="GELOO") Or ([Pentagraph]="GELOP") Or ([Pentagraph]="GELCN") Or ([Pentagraph]="GELCO") Or ([Pentagraph]="GELCP"),1,0)) AS GB
FROM
tblStatHistory
WHERE
(((tblStatHistory.Period)=IIf(:QTR1,1) Or (tblStatHistory.Period)=IIf(:QTR2,2) Or
(tblStatHistory.Period)=IIf(:QTR3,3) Or (tblStatHistory.Period)=IIf(:QTR4,4)) AND
((tblStatHistory.LogicalZone)=IIf(:ZONEF50,"F") Or (tblStatHistory.LogicalZone)=IIf(:ZONEAM,"S") Or
(tblStatHistory.LogicalZone)=IIf(:ZONEDM,"R") Or (tblStatHistory.LogicalZone)=IIf(:ZONED50,"D")) AND
((tblStatHistory.MatchID)=:GameID) AND ((tblStatHistory.PeriodSecs)>:Seconds))
GROUP BY
tblStatHistory.HomeTeam
ORDER BY
tblStatHistory.HomeTeam DESC;
This part of the very first line is the second table I'm trying to access
Sum(IIf((tblChain.InitialZone="1"),1,0)) AS SCCH
What's happening is if I try write the value of "SCCH" to a variable, it's spitting back that the SCCH field is empty. It also kills the output from the rest of the code, reporting 0 instead of the count of the query.
Any suggestions on how to best implement this?
EDIT: Here is the original code
Procedure TGame.UpdateTeamStats;
var
Query: TADOQuery;
P,Z,T: integer;
begin
try
Query := TADOQuery.Create(nil);
Query.Connection := connection;
Query.Close;
Query.SQL.Add('SELECT tblStatHistory.HomeTeam, Sum(IIf(([Pentagraph]="HANDB") Or ');
Query.SQL.Add('([Pentagraph]="HANBB") Or ([Pentagraph]="HBCL") Or ([Pentagraph]="KBBW") Or ([Pentagraph]="KBCL") Or ');
Query.SQL.Add('([Pentagraph]="KBIN") Or ([Pentagraph]="KBLO") Or ([Pentagraph]="KBSH") Or ');
Query.SQL.Add('([Pentagraph]="KKBW") Or ([Pentagraph]="KKCL") Or ([Pentagraph]="KKGKN") Or ');
Query.SQL.Add('([Pentagraph]="KKBW") Or ([Pentagraph]="KKGKO") Or ([Pentagraph]="KKGKP") Or ');
Query.SQL.Add('([Pentagraph]="KKIN") Or ([Pentagraph]="KKLO") Or ([Pentagraph]="KKSH"),1,0)) AS D, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="KBBW") Or ([Pentagraph]="KBCL") Or ([Pentagraph]="KBIN") Or ');
Query.SQL.Add('([Pentagraph]="KBLO") Or ([Pentagraph]="KBSH") Or ([Pentagraph]="KKBW") Or ');
Query.SQL.Add('([Pentagraph]="KKCL") Or ([Pentagraph]="KKGKN") Or ([Pentagraph]="KKBW") Or ');
Query.SQL.Add('([Pentagraph]="KKGKO") Or ([Pentagraph]="KKGKP") Or ([Pentagraph]="KKIN") Or ');
Query.SQL.Add('([Pentagraph]="KKLO") Or ([Pentagraph]="KKSH"),1,0)) AS K, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="MACOO") Or ([Pentagraph]="MACOP") Or ([Pentagraph]="MAPAO") Or ');
Query.SQL.Add('([Pentagraph]="MAPAP") Or ([Pentagraph]="MAOL") Or ([Pentagraph]="MAUNO") Or ');
Query.SQL.Add('([Pentagraph]="MAUNP"),1,0)) AS M, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="MAERO") Or ([Pentagraph]="MAERP") Or ([Pentagraph]="MAUNO") Or ');
Query.SQL.Add('([Pentagraph]="MAUC") Or ([Pentagraph]="MAUNP") Or ([Pentagraph]="MAOL"),1,0)) AS UM, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="MACOO") Or ([Pentagraph]="MACOP"),1,0)) AS CM, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="KBBW") Or ([Pentagraph]="KKBW"),1,0)) AS K_Back, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="HANDB") Or ([Pentagraph]="HANBB") Or ([Pentagraph]="HBCL"),1,0)) AS H, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBCL") Or ([Pentagraph]="BUCL") Or ([Pentagraph]="TICL"),1,0)) AS C, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBCL"),1,0)) AS C_CSB, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="BUCL") Or ([Pentagraph]="TICL"),1,0)) AS C_BUTI, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBFP") Or ([Pentagraph]="BUFP") Or ([Pentagraph]="TIFP"),1,0)) AS FP, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="BUFP") Or ([Pentagraph]="TIFP"),1,0)) AS FP_BUTI, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBFP"),1,0)) AS FP_CSB, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBHO") Or ([Pentagraph]="BUHO") Or ([Pentagraph]="TIHO"),1,0)) AS HO, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="BUHO") Or ([Pentagraph]="TIHO"),1,0)) AS HO_BUTI, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="CBHO"),1,0)) AS HO_CSB, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="IN50"),1,0)) AS I50, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="GOAL"),1,0)) AS GOAL, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="BEHI"),1,0)) AS BHND, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="TACKN") Or ([Pentagraph]="TACKO") Or ([Pentagraph]="TACKP") Or ');
Query.SQL.Add('(([Pentagraph]="RDTDD") And ([ROLE]=1)) Or (([Pentagraph]="TACKL") And ([ROLE]=1)) Or ');
Query.SQL.Add('(([Pentagraph]="RDTAK") And ([ROLE]=1)) Or (([Pentagraph]="DISP") And ([ROLE]=1)),1,0)) AS T, ');
Query.SQL.Add('Sum(IIf(([Pentagraph]="GEHAN") Or ([Pentagraph]="GEHAO") Or ([Pentagraph]="GEHAP") Or ([Pentagraph]="GEHCN") Or ([Pentagraph]="GEHCO") Or([Pentagraph]="GEHCP") Or ');
Query.SQL.Add('([Pentagraph]="GERU") Or ([Pentagraph]="GELON") Or ([Pentagraph]="GELOO") Or ([Pentagraph]="GELOP") Or ([Pentagraph]="GELCN") Or ([Pentagraph]="GELCO") Or ([Pentagraph]="GELCP"),1,0)) AS GB');
Query.SQL.Add('FROM tblStatHistory');
Query.SQL.Add('WHERE (((tblStatHistory.Period)=IIf(:QTR1,1) Or (tblStatHistory.Period)=IIf(:QTR2,2) Or ');
Query.SQL.Add('(tblStatHistory.Period)=IIf(:QTR3,3) Or (tblStatHistory.Period)=IIf(:QTR4,4)) AND ');
Query.SQL.Add('((tblStatHistory.LogicalZone)=IIf(:ZONEF50,"F") Or (tblStatHistory.LogicalZone)=IIf(:ZONEAM,"S") Or ');
Query.SQL.Add('(tblStatHistory.LogicalZone)=IIf(:ZONEDM,"R") Or (tblStatHistory.LogicalZone)=IIf(:ZONED50,"D")) AND ');
Query.SQL.Add('((tblStatHistory.MatchID)=:GameID) AND ((tblStatHistory.PeriodSecs)>:Seconds))');
Query.SQL.Add('GROUP BY tblStatHistory.HomeTeam');
Query.SQL.Add('ORDER BY tblStatHistory.HomeTeam DESC;');
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'GameID';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'Seconds';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'QTR1';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'QTR2';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'QTR3';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'QTR4';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'ZONEF50';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'ZONEAM';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'ZONEDM';
end;
with Query.Parameters.AddParameter do
begin
DataType := ftinteger;
name:= 'ZONED50';
end;
Query.Parameters.ParamByName('GameID').Value := GameID;
for P := 0 to 4 do
begin
if updatePeriodCheck(p) then
begin
for Z := 0 to 7 do
begin
SetParameters(P,Z,0,Query.Parameters);
Query.Open;
Query.First;
for T := 0 to 3 do
begin
if T = 1 then Query.Next;
if T < 2 then
Stats[P,Z].Team[T] := PopulateStatsRecord(Query)
else if T = 2 then // Difference
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,Z].Team[1],Stats[P,Z].Team[0])
else if T=3 then begin // Difference Z v Z
if Z = 0 then
Stats[P,Z].Team[T] := Stats[P,Z].Team[2]
else if Z = 1 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,1].Team[1],Stats[P,4].Team[0])
else if Z = 2 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,2].Team[1],Stats[P,3].Team[0])
else if Z = 3 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,3].Team[1],Stats[P,4].Team[0])
else if Z = 4 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,4].Team[1],Stats[P,1].Team[0])
else if Z = 5 then
Stats[P,Z].Team[T] := Stats[P,Z].Team[2]
else if z = 6 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,6].Team[1],Stats[P,7].Team[0])
else if z = 7 then
Stats[P,Z].Team[T] := SubtractStatsRecords(Stats[P,7].Team[1],Stats[P,6].Team[0]);
end;
end;
Query.Close;
end;
end;
end;
finally
Query.Free;
end;
end;

Invalid Identifier in Oracle Function

This is my SQL;
select a.hesap_no,
a.teklif_no1 || '/' || a.teklif_no2 as teklif,
a.mus_k_isim as musteri,
b.marka,
c.sasi_no,
c.sasi_durum,
d.tas_mar,
nvl(risk_sasi(a.teklif_no1, a.teklif_no2, c.urun_sira_no, c.sira_no), 0) as risk,
nvl(mv_sasi(a.teklif_no1, a.teklif_no2, c.sira_no, c.urun_sira_no, sysdate), 0) as mv
from s_teklif a,
s_urun b,
s_urun_detay c,
koc_ktmar_pr d
where a.teklif_no1 || a.teklif_no2 = b.teklif_no1 || b.teklif_no2
This is my MV_SASI Function;
create or replace
FUNCTION MV_SASI
(
TEK1 IN VARCHAR2,
TEK2 IN NUMBER,
SIRA IN NUMBER,
USIRA IN NUMBER,
DT IN DATE
)
RETURN NUMBER IS MV number;
fat number;
adet number;
pd number;
pds number;
kt date;
ktv number;
dtv number;
frk number;
yfrk number;
TKM VARCHAR2(10);
BEGIN
SELECT COUNT(*)
INTO adet
FROM S_URUN_DETAY
WHERE (SASI_DURUM IS NULL OR SASI_DURUM IN ('A','R'))
AND TEKLIF_NO1 = TEK1
AND TEKLIF_NO2 = TEK2;
SELECT SUM(CASE WHEN B.SASI_DURUM IS NULL OR B.SASI_DURUM IN ('A','R') THEN A.KDV_FIYAT ELSE 0 END)
INTO fat
FROM S_URUN A,S_URUN_DETAY B
WHERE A.TEKLIF_NO1 = tek1
AND A.TEKLIF_NO2 = tek2
AND A.TEKLIF_NO1 = B.TEKLIF_NO1
AND A.TEKLIF_NO2 = B.TEKLIF_NO2
AND A.SIRA_NO = B.URUN_SIRA_NO;
SELECT KULLAN_TARIH
INTO kt
FROM S_TEKLIF
WHERE TEKLIF_NO1 = tek1
AND TEKLIF_NO2 = tek2 ;
yfrk:= EXTRACT(YEAR FROM dt) - EXTRACT(YEAR FROM kt);
ktv := EXTRACT(MONTH FROM kt);
dtv := EXTRACT(MONTH FROM dt);
frk := yfrk * 12 + (dtv-ktv);
IF frk <= 0 THEN
pd := fat * 0.85;
ELSE
pd := fat*0.85 - (fat * 0.0101 * frk);
END IF;
SELECT NVL(ROUND((CASE WHEN SASI_DURUM IS NULL OR SASI_DURUM IN ('A','R') THEN pd / adet ELSE 0 END),2),0)
INTO pds
FROM S_URUN_DETAY
WHERE TEKLIF_NO1 = TEK1
AND TEKLIF_NO2 = TEK2
AND URUN_SIRA_NO = USIRA
AND SIRA_NO = SIRA;
RETURN pds;
END;
But in my page i getting an error with this line of code;
OracleDataReader dr = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Where am i doing wrong?
Best Regards, Soner
myCommand is;
select a.hesap_no,
a.teklif_no1 || '/' || a.teklif_no2 as teklif,
a.mus_k_isim as musteri,
b.marka,
c.sasi_no,
c.sasi_durum,
d.tas_mar,
nvl(risk_sasi(a.teklif_no1, a.teklif_no2, c.urun_sira_no, c.sira_no), 0) as risk,
nvl(mv_sasi(a.teklif_no1, a.teklif_no2, c.sira_no, c.urun_sira_no, sysdate), 0) as mv
from s_teklif a,
s_urun b,
s_urun_detay c,
koc_ktmar_pr d
where a.teklif_no1 || a.teklif_no2 = b.teklif_no1 || b.teklif_no2
and a.teklif_no1 || a.teklif_no2 = c.teklif_no1 || c.teklif_no2
and b.sira_no = c.urun_sira_no
and b.distributor = d.dist_kod
and b.marka = d.marka_kod
and b.urun_kod = d.tas_kod
and a.hesap_no in (select a.hesap_no
from s_teklif a
where a.mus_k_isim in (system.collections.arraylist)
)
This bit at the end of your SQL seems odd: WHERE A.MUS_K_ISIM IN (System.Collections.ArrayList). If that's really your actual SQL text, then I think this is your problem. Oracle has no idea what System.Collections.ArrayList is.
Moreover, if what you're trying to do is bind an actual arraylist into the SQL so that you can test against it with IN -- that won't work. Even if you successfully got it to bind to a known datatype in Oracle, the syntax of IN is such that the operand is compared to each element in a comma-delimited list of identifiers -- Oracle won't break the list apart for you.
You either need to bind each item of the list so you have a condition like A.MUS_K_ISIM IN (:bind1, :bind2, :bind3), or map your arraylist to a nested table or varray type in Oracle so you can use a subquery with IN, like A.MUS_K_ISIM IN (SELECT columnValue FROM TABLE(CAST(:bindlist AS someOracleType))).
I have come accross that kind of problem a short time ago. The problem was:
Stupid oracle has not a stacktrace type error handling mechanism so it only gives the top error, which prevents us to see the other ones.
Your function MV_SASI has no grant to be used by the oracle user. So Oracle could not find it and tried to run it like MV_SASI is a column name. so it gives the error.
Check the grant and visibility settings of the MV_SASI func.