I have the following query
SELECT v.id
FROM visitors v
LEFT JOIN trackings t on v.id = t.visitor_id
WHERE v.app_id = 'xxx'
AND (field = quote_ident('app_name')
AND string_value ILIKE quote_nullable('test_app'))
Problem is that the query fails to return any results when I use quote_nullable on the string_value. It only works with quote_ident, but that is not the appropriate function as far as I understand here.
Example in pseudocode of how I am building the query serverside (using pattern matching on the filter (starts_with, ends_with etc.).
filter = user_params["filter"]
field = user_params["field"]
value = user_params["value"]
...
"starts_with" -> "(field = quote_ident('#{field}') AND string_value ILIKE '#{value}' || '%')"
"ends_with" -> "(field = quote_ident('#{field}') AND string_value ILIKE '%' || '#{value}')"
"contains" -> "(field = quote_ident('#{field}') AND string_value ILIKE '%' || '#{value}' || '%')"
"does_not_contain" -> "(field = quote_ident('#{field}') AND string_value NOT ILIKE '%' || '#{value}' || '%')"
Later in the DB, I invoke a stored procedure that looks something like
filter_length := COALESCE(array_length(filters, 1), 0);
filterstring := array_to_string(filters, ' OR ');
IF filterstring = '' or filterstring is null THEN
filterstring := '1=1';
END IF;
sql := format('
SELECT v.id FROM visitors v
LEFT JOIN trackings t on v.id = t.visitor_id
WHERE v.app_id = app_id and (%s)
group by v.id
having case when %s > 0 then count(v.id) = %s else true end
', filterstring, custom_filter_length, custom_filter_length);
RETURN QUERY EXECUTE sql;
Where filters is an array of conditions (the ones I built earlier)
Any ideas?
Related
I'm using the below insert statement to declare v_text.
also i am using case in the select statement. But i am getting the below error in the case statement.
Error(469,119): PLS-00103: Encountered the symbol "VALUE" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between || member submultiset
v_text := 'INSERT INTO temp_value '||
'(ach_id, language, value, value_ivi, dq_nr, unit, leervalue, gg_id)'||
'SELECT '||p_ach_id||', d.lfd, w.sprache,'||
case when y.zeige_unit = 1 then regexp_replace(w.value,'(\d+\.\d+)','\1'||w.unit) else
w.value end value,||
'w.value, w.value_ivi, '||i||', w.unit, w.leervalue, w.gg_id '||
' FROM ZUL_GEN_NEU_Zwischenvaluee_value w
Join DV_FORMAT_POS_DATENQ y
ON w.dq_id = y.for_pos_dat_id,'||
' (SELECT /*+ no_merge(v) */ DISTINCT v.lfd'||
' FROM '||
'( '||p_vv_text||') v'||
' JOIN ('||v_def_text||') d '||v_def_where||') d '||
' WHERE w.ach_id = '||p_ach_id||
' AND w.pos_id = '||p_pos_id||;
I don't have all of your variables, but as a start:
v_text := 'INSERT INTO temp_value '||
'(ach_id, language, value, value_ivi, dq_nr, unit, leervalue, gg_id)'||
'SELECT '||p_ach_id||', d.lfd, w.sprache,'||
case when y.zeige_unit = 1 then regexp_replace(w.value,'(\d+\.\d+)','\1'||w.unit) else
w.value end || -- removed "value,"
'w.value, w.value_ivi, '||i||', w.unit, w.leervalue, w.gg_id '||
' FROM ZUL_GEN_NEU_Zwischenvaluee_value w
Join DV_FORMAT_POS_DATENQ y
ON w.dq_id = y.for_pos_dat_id,'||
' (SELECT /*+ no_merge(v) */ DISTINCT v.lfd'||
' FROM '||
'( '||p_vv_text||') v'||
' JOIN ('||v_def_text||') d '||v_def_where||') d '||
' WHERE w.ach_id = '||p_ach_id||
' AND w.pos_id = '||p_pos_id; -- fixed "||;"
(I set the SO language to Lua as it seems to display quoted text better.)
SELECT DISTINCT
ent.entity_key_id AS query1kid,
CAST(substr(rtrim(XMLAGG(xmlelement(e,alstd.relationship_desc
||
CASE
WHEN(alstd.joint_flag = 'No' AND ent.anonymous_flag = 'No') THEN ''
ELSE('('
||
CASE
WHEN alstd.joint_flag = 'Yes' THEN 'Joint'
ELSE ''
END
||
CASE
WHEN ent.anonymous_flag = 'Yes' THEN ',Anon'
ELSE ''
END
|| ')')
END
|| ': '
|| allocthm.allocation_description
|| '('
|| substr(allocthm.allocation_code,5,6) || ***count(ben.entity_key_ID)***
|| ')',',').extract('//text()')
ORDER BY
ent.entity_key_id
).getclobval(),','),1,4000) AS VARCHAR(4000) ) AS displayfiled1
FROM
er_datamart.allocation_theme allocthm
left JOIN er_datamart.allocation_stewardee alstd ON (allocthm.allocation_code = alstd.allocation_code and alstd.status_code <> 'F')
INNER JOIN er_datamart.entity_d ent ON alstd.entity_key_id = ent.entity_key_id
LEFT OUTER JOIN ER_DATAMART.ALLOCATION_BENEFICIARY ben ON ben.allocation_code = allocthm.allocation_code
GROUP BY
ent.entity_key_id
This gives me an error:
ORA-00937: not a single-group group function
I'm trying to find the count(ben.entity_key_ID) so that I can have it appended to my already functional query. Any help would be appreciated.
The problem seems to be the count() inside the xmlagg() - you have nested group functions that the group-by clause can't handle.
You can move the string concatenation into an inline view with its own group-by to get that count, and then perform the XML aggregation from that; something like:
SELECT
entity_key_id AS query1kid,
CAST(substr(rtrim(
XMLAGG(xmlelement(e, constructed_string, ',').extract('//text()')
ORDER BY entity_key_id
).getclobval(),','),1,4000) AS VARCHAR(4000) ) AS displayfiled1
FROM (
SELECT ent.entity_key_id,
alstd.relationship_desc
||
CASE
WHEN(alstd.joint_flag = 'No' AND ent.anonymous_flag = 'No') THEN ''
ELSE('('
||
CASE
WHEN alstd.joint_flag = 'Yes' THEN 'Joint'
ELSE ''
END
||
CASE
WHEN ent.anonymous_flag = 'Yes' THEN ',Anon'
ELSE ''
END
|| ')')
END
|| ': '
|| allocthm.allocation_description
|| '('
|| substr(allocthm.allocation_code,5,6) || count(ben.entity_key_ID)
|| ')' as constructed_string
FROM
allocation_theme allocthm
LEFT JOIN allocation_stewardee alstd
ON allocthm.allocation_code = alstd.allocation_code and alstd.status_code <> 'F'
INNER JOIN entity_d ent
ON alstd.entity_key_id = ent.entity_key_id
LEFT OUTER JOIN ALLOCATION_BENEFICIARY ben
ON ben.allocation_code = allocthm.allocation_code
GROUP BY
ent.entity_key_id,
alstd.relationship_desc,
alstd.joint_flag,
ent.anonymous_flag,
allocthm.allocation_description,
allocthm.allocation_code
)
GROUP BY
entity_key_id
The ELSE '' parts are redundant as that is the default behaviour but you might prefer to keep them for clarity/explicitness. Your joint/anon part might need a bit more work - if joint is No and anon is Yes you get (,Anon) - I think - which looks a bit odd, but might be what you need.
I need to pass a column name from front end to back end in my code. i'm using c# with oracle and when i pass the column name as a parameter, it gives an error and it's because the column name is used as a string in here and i need to know how to fix this. here is my code,
PROCEDURE PR_GETCLIENTCONTRACTDATA(INSTRFIELD IN VARCHAR2,INSTRCONTRACTNO IN VARCHAR2,CUR_OUTPUT OUT T_CURSOR)--ADDED BY DIDULA 25/10/2017
IS
BEGIN
OPEN CUR_OUTPUT FOR
SELECT c.con_no,
DECODE (a.clm_cori,
'1', a.clm_cltitle || ' ' || a.clm_initialsfull || ' '
|| a.clm_name,
a.clm_name
) cliname,
a.clm_code,
( a.clm_permaddline1
|| '|'
|| a.clm_permaddline2
|| '|'
|| COALESCE (a.clm_permaddline3, a.clm_permaddline4)
|| '|'
|| NULLIF ((a.clm_permaddline4),
COALESCE (a.clm_permaddline3, a.clm_permaddline4)
)
) address
FROM leaseinfo.tblcontracts c, corpinfo.tblclientmain a
WHERE a.clm_code = c.con_clmcode
AND INSTRFIELD = INSTRCONTRACTNO; ***here INSTRFIELD is the column name
that i need to pass***
END PR_GETCLIENTCONTRACTDATA;
Whitelist the column names:
PROCEDURE PR_GETCLIENTCONTRACTDATA(
INSTRFIELD IN VARCHAR2,
INSTRCONTRACTNO IN VARCHAR2,
CUR_OUTPUT OUT T_CURSOR
)
IS
BEGIN
OPEN CUR_OUTPUT FOR
SELECT -- your select clauses
FROM leaseinfo.tblcontracts c,
INNER JOIN corpinfo.tblclientmain a -- ANSI join syntax
ON a.clm_code = c.con_clmcode
WHERE CASE INSTRFIELD
WHEN 'COLUMNA' THEN ColumnA
WHEN 'COLUMNB' THEN ColumnB
WHEN 'COLUMNC' THEN ColumnC
END = INSTRCONTRACTNO;
END PR_GETCLIENTCONTRACTDATA;
/
When you use OPEN cur FOR ... you can pass a string, i.e.
PROCEDURE PR_GETCLIENTCONTRACTDATA(INSTRFIELD IN VARCHAR2,INSTRCONTRACTNO IN VARCHAR2,CUR_OUTPUT OUT T_CURSOR)
IS
BEGIN
OPEN CUR_OUTPUT FOR
'SELECT c.con_no,
DECODE (a.clm_cori,
''1'', a.clm_cltitle || '' '' || a.clm_initialsfull || '' ''
|| a.clm_name,
a.clm_name
) cliname,
a.clm_code,
( a.clm_permaddline1
|| ''|''
|| a.clm_permaddline2
|| ''|''
|| COALESCE (a.clm_permaddline3, a.clm_permaddline4)
|| ''|''
|| NULLIF ((a.clm_permaddline4),
COALESCE (a.clm_permaddline3, a.clm_permaddline4)
)
) address
FROM leaseinfo.tblcontracts c
JOIN corpinfo.tblclientmain a ON a.clm_code = c.con_clmcode
WHERE '||DBMS_ASSERT.SIMPLE_SQL_NAME(INSTRFIELD)||' = :INSTRCONTRACTNO)'
USING INSTRCONTRACTNO;
END PR_GETCLIENTCONTRACTDATA;
I have complex Select statement in Oracle, with Case When conditions and they select all Concatenate values.
So something similar to this:
END
END
FROM something
...
What I need is to put Other value from other table instead of My Value in concatenate.
So Instead of (My Value) I would have:
(Select textValue from textView A where A.textID = '395')
If I run this statement alone, it will take out one exact value I want. However if I put it instead of (My Value) into concatenate it gives me error: ora-00936 missing expression
(Yes '395' is string in that other table)
Any Ideas please?
You can use inline views inside the case clause in Oracle. Isn't that what you are trying to do? "Missing expression" error is probably a missing bracket or some similar code error.
select case object_type
when 'TABLE' then
'select * from ' || object_name
when 'SYNONYM' then
'describe ' || object_name
when 'PACKAGE' then
(select to_char (count (*))
from user_source s
where s.type = o.object_type and s.name = o.object_name)
else
(select object_type from dual)
end
as objects
from user_objects o
Or simply create a function that returns that does this for you and call it.
create function gettextval (p_textid in varchar2)
return varchar2 is
l_returnval varchar2 (32767);
begin
select text_val
into l_returnval
from v_texts s
where s.text_id = p_textid;
return l_returnval;
exception
when no_data_found then
return p_textid;
end;
In this example I make it return the input string of nothing is found.
Then you can reference the function in your previous select.
' || gettextval('395') || '
can you post you query with the "select text" bit inside?
When use case->when i do like that:
SELECT
CASE A.LIST_TYPE_CD
WHEN '1' THEN '<A HREF="censured?thresholdId=censured' || GET_SITE_SUFFIX() || chr(38) || 'task=runSQL' || chr(38) || 'parseParams=true' || chr(38) || 'list_id=' || A.LIST_ID || chr(38) || 'list_name=' || A.LIST_NAME ||
'">' || (Select textValue from textView A where A.textID = '395') || '</A>'
WHEN '3' THEN '<A HREF=censured' || GET_SITE_SUFFIX() || chr(38) || 'task=runSQL' || chr(38) || 'parseParams=true' || chr(38) || 'list_id=' || A.LIST_ID ||
'">' || (Select textValue from textView B where B.textID = '395') || '</A>'
END
FROM something A
I have a procedure (see below) which is using dynamic query. I wanted to rewrite this procedure with out using dynamic query. How do I write the conditions below?
PROCEDURE DemoProcedure(p_firstname IN VARCHAR2,
p_lastname IN VARCHAR2,
p_phone IN VARCHAR2
o_Cursor OUT t_Cursor) IS
SQLString VARCHAR2(4000);
BEGIN
SQLString :=
'SELECT * FROM
SCHEMA.TABLENAME_a A
INNER JOIN SCHEMA.TABLENAME_b B
ON A.ID = B.ID
WHERE
A.TYPE = 1 ';
IF p_firstname IS NOT NULL THEN
SQLString := SQLString || ' and UPPER(A.FIRST_NAME) like UPPER( ''' || p_firstname || ''')';
END IF;
IF p_lastname IS NOT NULL THEN
SQLString := SQLString || ' and UPPER(A.LAST_NAME) like UPPER( ''' || p_lastname || ''')';
END IF;
IF p_phone IS NOT NULL THEN
SQLString := SQLString || ' and UPPER(A.PHONE) = ''' ||
p_phone || '''';
END IF;
SQLString := SQLString || ' order by a.id ';
OPEN o_Cursor FOR SQLString;
END DemoProcedure;
It looks like you just want
OPEN o_cursor
FOR SELECT ...
WHERE A.TYPE = 1
AND (p_firstname IS NULL or upper(a.first_name) = upper(p_firstname))
AND (p_lastname IS NULL or upper(a.last_name) = upper(p_lastname))
AND (p_phone IS NULL or upper(a.phone) = p_phone)
ORDER BY a.id
I'm not sure why you'd want to bother upper-casing a phone number-- do you have character data in a phone number?
Different way writing the same SQL as #JustinCave suggested -
OPEN o_cursor
FOR SELECT ...
WHERE A.TYPE = 1
AND upper(a.first_name) = nvl(upper(p_firstname), upper(a.first_name))
AND upper(a.last_name) = nvl(upper(p_lastname),upper(a.last_name))
AND upper(a.phone) = nvl(p_phone,upper(a.phone))
ORDER BY a.id