SQL function support within cfquery
Are all SQL statements supported by <cfquery>?
I'm tring to use FORMAT() with no luck.
p.s. using MySQL db
I am doing simple math within a query of query.
Specifically:
<cfquery dbtype="query" name="qClubRecord">
SELECT
SUM(WIN) AS WINS,
SUM(LOSS) AS LOSSES,
SUM(TIE) AS TIES,
SUM(WIN)/(SUM(WIN) + SUM(LOSS) + SUM(TIE)) AS WIN_PERCENTAGE
FROM
qGetRecords
</cfquery>
This works.
My problem arises when displaying. I would like to show only 2 decimal places.
Now, I'm using <cfgrid> with format="html", so I can't use numberFormat.
I should be able to solve this problem within SQL. After a quick look I found the FORMAT() statement.
Works as advertised from the command line, but incorporating into <cfquery> fails (whether QoQ or not).
So I'm stuck.
<cfquery dbtype="query" name="qClubRecord">
SELECT
SUM(WIN) AS WINS,
SUM(LOSS) AS LOSSES,
SUM(TIE) AS TIES,
FORMAT(SUM(WIN)/(SUM(WIN) + SUM(LOSS) + SUM(TIE)),2) AS WIN_PERCENTAGE
FROM
qGetRecords
</cfquery>
Thank you both for answering.
Similar Messages
-
SQL Functions Supported by RDBMS Adapter
Hi,
Where can I find the list of SQL Functions supported by RDBMS Adapter?
I am retrieving date from Oracle 9i using RDBMS Adapter 8.1 SP2.However when I use any SQL Function (like to_char, to_date) in select query the service creation fails with a parsing exception.
Please help me resolving this.I want a date to be selected as a string in a particular format.
Thanks,
VishvjitSorry,just got the resolution in Known Limitations of the adapter Release Notes. Actually we have to create a service of arbitory sql for using SQL Functions in the query.
-
SYS_GUID() SQL functions within XQuery
Is there any simple way to invoke regular SQL functions from within XQuery?
I would like to create an identifier attribute, and set the value to SYS_GUID(), but when I try to directly embed the function, it comes back saying:
ORA-19237: XP0017 - unable to resolve call to function - fn:SYS_GUID
select XMLQUERY('
<AgeCmf dtm="{current-dateTime()}" id="{SYS_GUID()}"/>
' RETURNING CONTENT) from dual;
I could dump the value into a temp table, or make it an input parameter via another function, but I am looking for something simple within a SQL call.
Thanks,
HWMaybe this?:
michaels> select xmlquery('<AgeCmf dtm="{current-dateTime()}" id="{s}"/>' passing xmltype('<s>' || sys_guid() || '</s>') returning content) x from dual
X
<AgeCmf dtm=" 2007-06-20T07:05:19.515000+02:00" id="6304A70F5C104ACB98C69EEB3D47891C"></AgeCmf> -
Does anybody can suggest me how do i write SQL FUNCTIONS in
Coldfusion, i tried to run a SQL FUNCTION in a <cfquery> but
that returned me an Coldfusion Error. do i need to use
<cfstoredProc> for that or. any other <tag available>
<cfquery name="blahblah" datasource="#dsn#">
CREATE FUNCTION [dbo].[ConcatUserGroups](@UserID int)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @groupname Varchar(2000)
SET @groupname = ''
SELECT @groupname = @groupname + ', ' + CONVERT(varchar(20),
u.group_name)
FROM [user] A
JOIN UserGroup_combo UG
ON UG.ug_userID = a.[USER_ID]
JOIN usergroup U
ON UG.ug_groupID = u.group_ID
WHERE A.[USER_ID] = @userID
AND LEN(ISNULL(U.group_name,'')) > 0
ORDER BY U.GROUP_NAME
IF(LEN(@groupname) >= 2) BEGIN
SET @groupname = RIGHT(@groupname,(LEN(@groupname) - 2))
END
RETURN @groupname
END
GO
</cfquery>There's no such thing as "coldfusion sql" (sic). Everything
between the
<cfquery> tags is simply resolved (ie: CF runtime
statements between
pound-signs) and passed straight to the DB drivers, and
thence to the DB
itself. CF does no SQL processing at all.
Your error kind of suggests a syntac error AFTER the query
has been
executed though.
Could you post some code, and point out which precise line is
erroring?
Adam -
How to use SQL function in Tabular Form
Edited by: JuniorNewbi on 03-Nov-2009 08:15
Hi
I want to have a Tabular Form in which the Report Source sql statement is retrieved via an SQL function, so that the resulting report can show a variable number of columns and column names.
The following great web site shows just the kind of thing i'm trying to do (Tabular Form Dynamic):
http://htmldb.oracle.com/pls/otn/f?p=31517:195:518536699796436::NO:::
However, as an Apex newbie, i don't know how to invoke the SQL function from within the page. The "Report Source" appears to only allow a static "select...." style of entry.
I don't have access to the Denes Kubicek's workspace so don't know how he would have invoked his PL/SQL Function.
Can anyone help?
Thanks in advance!
Edited by: JuniorNewbi on 03-Nov-2009 08:46 -
Support of PL/SQL Functions in Data Modeler
Hello,
Sorry if this question has already been answered, I searched the forum but got 215 hits on data modeler + pl/sql functions.
So my question is:
Stored Procedures are already supported by the physical model (11g) of data modeler, but I could not find any method
to create pl/sql functions via data modeler - neither in the documentation nor on the user interface. Is this
currently not supported?Is this currently not supported?There will be support for functions and packages in next release.
Philip -
Do Report Builder support custom SQL Functions
Hi All,
I’m new to SSRS Report builder. I had written a SQL query which executes really well when I run in Management Studio. I tried to put that same query in new report to everyone can easily access. I build the DataSource and connection successfully tested.
At the time of creating new Dataset, I pasted same query and that’s where I got stuck.
Is it possible to add custom SQL Functions in the Report Builder SSRS?
The Syntax of method is as follow
Create Function [dbo].[ufn_Sqlstatement]( @str as varchar(max)) RETURNS varchar(max)
Thanks in AdvanceYou do not need to put the create function script into the query. It only needs to be executed once. Instead, run it on the database server. Then, simply call the function from your query:
SELECT dbo.ufn_SQLstatement('select * from table') -
I want to build a script logic but the dimensions that I am testing do not have the fields I want in the format I want. In my case I would like to test the "Start" property in the Employee dimension. The property represents the employee start date but is a var char in the format dd.mm.yyyy rather thna a date field. I want to test whether it is five years prior to the date being processed.
My time dimension has a year property and a monthnum property. Each of these are varchar. I can write SQL that would convert these to a date and compare them with the start property on the employee dimension.
Can I do iinsert SQL functions in the When statement. An example would be
*When convert(datetime, substring(employee.start,4,2) + '/' + substring(employee.start,1,2) + '/' + substring(employee.start,7,4))
Can I use SQL functions in the Is statement? An example would be
*Is <DateAdd(YY,-5 , convert(datetime, Time.Monthnum + '/01/'' + time.year)
Finally when I use such techniques and look at the log by using "show log" in the logic debugger I see no reference to the SQL I used. It is as if the generated SQL has not made use of my functions. Instead all I seee are the extract statements from the three fact tables. Is this normal? Is there a way to see what SQL is really being generated and run?
At the moment it seems my only option is to make changes, try running it and hope I get an output. If no records are written I can't be sure whether it is because embedding SQL function is not supported or because I have made some other error.No, the "BPC SQL" script logic doesn't allow for this extensive use of real SQL within the scripting syntax. As a concept, it's best to think of "BPC SQL" script logic as, "this unique BPC logic syntax will generate real SQL code when it's executed."
There's very little real SQL that you can use within the syntax itself, and definitely not within the WHEN or IF statements. Nothing like cast, convert, substring, etc. (Except you CAN use these, a little bit, in a *SELECT statement. But I don't see how that will solve your current problem.)
Plus, when you consider how the logic engine processes a set of data, it's helpful to think of it in terms of selects & reading through a recordset. So in that regard, a SQL background can be helpful, in understanding the concepts. (Sort of helpful, except when it confuses you, or frustrates you since you don't have all the power you want.)
Anyway, to solve your problem, you might be able to restate the problem, and then use something like this code, which is valid syntax:
*WHEN TIME.TIMEID
*IS >= VERSION.FirstPlanMonthTimeID
In this case, FirstPlanMonthTimeID is set up in the version dimension as, for example, 20090400 (representing 2009.APR). This timeID is set by BPC automatically when processing the time dimension, and I need to ensure that my Version property value is set to match it.
A key point is that in BPC fact tables, the time dimension is stored as TIMEID (not ID, unlike all the other dimensions), and can be used for comparative evaluation in script logic WHEN/IF. The comparison property needs to be stored as an 8-digit integer value. If your time dimension has days in it (along with months), check the TIMEID after processing, and you should see the pattern for assigning values to TIMEID for days and months, etc.
Normally, TIMEID is assigned pretty sensible and stable values. If it doesn't meet your logic requirements, you can create another property (be careful of USERTIMEID, since that has a specific purpose in the system which I've never fully understood) and assign that whatever (integer) values make sense. -
Region source (PL/SQL function body returning SQL query)
Hi, guys.
Here is what i try to do:
Create a region of type SQL Query (PL/SQL function body returning SQL query). In the source area i tried to put this:
DECLARE
v_new VARCHAR2(10);
v_SQL varchar2(32000);
BEGIN
v_new := :P102_HDN_NEW;
-- htp.p(v_new);
IF v_new = 'N-Set' THEN
v_SQL := 'select ' ||
ELSIF v_new = 'Y-Set' THEN
v_SQL := 'select ' ||
END IF;
RETURN v_SQL;
END;
And here is the reply from APEX:
1 error has occurred
Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the ''generic columns'' checkbox below the region source to proceed without parsing. The query can not be parsed, the cursor is not yet open or a function returning a SQL query returned without a value.
Now.
1. Variable is set with the right value.
2. Each statement (separately) returns SQL that works with no problems
3. Problem occures if i try to put IF statement around the SQL creation.
4. If i select "Use Generic Column Names (parse query at runtime only)" instead of "Use Query-Specific Column Names and Validate Query" then the script returns SQL properly, however report's column names are set to Col1, Col2,Col3 ......
Thnks in advence
MikeOK. Here is enire statement:
DECLARE
v_new VARCHAR2(10);
v_SQL varchar2(32000);
BEGIN
v_new := :P102_HDN_NEW;
htp.p(v_new);
IF v_new = 'N-Set' THEN
v_SQL := 'select ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(12,DECODE(ld.level,''All'', ''All Categories'',ld.level)) level, ' ||
'apex_item.checkbox(1, ld.opt_in_auto_flag, decode(ld.opt_in_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
'apex_item.checkbox(2, ld.opt_in_manual_flag, decode(ld.opt_in_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
'apex_item.checkbox(3, ld.opt_out_auto_flag, decode(ld.opt_out_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
'apex_item.checkbox(4, ld.opt_out_manual_flag, decode(ld.opt_out_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
'DECODE(c.code, ''NMBR'', NULL,''Change to '' || DECODE(ld.level,''All'',''Categories'',''All Categories'')) link_change, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
'from ' ||
'tbl1 c, ' ||
'tbl2 ld ' ||
'where c.code = ld.code ' ||
'and c.type = ''TYPE1'' ' ||
'and c.active = ''Y'' ' ||
'order by c.sorting ';
ELSIF v_new = 'Y-Set' THEN
v_SQL := 'select ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(12,''All Categories'') level, ' ||
'apex_item.checkbox(1, c.option_1, decode(c.option_1,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
'apex_item.checkbox(2, c.option_3, decode(c.option_3,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
'apex_item.checkbox(3, c.option_2, decode(c.option_2,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
'apex_item.checkbox(4, c.option_4, decode(c.option_4,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
'DECODE(c.code, ''AAA'', NULL,''Options by AAA'') link_change, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
'from ' ||
'tbl1 c ' ||
'where 1 = 1 ' ||
'and c.type = ''TYPE1'' ' ||
'and c.active = ''Y'' ' ||
'order by c.sorting ';
END IF;
RETURN v_SQL;
END;
If i put just this
DECLARE
v_new VARCHAR2(10);
v_SQL varchar2(32000);
BEGIN
v_new := :P102_HDN_NEW;
htp.p(v_new);
v_SQL := 'select ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(10,c.sdescr) descr, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(12,DECODE(ld.level,''All'', ''All Categories'',ld.level)) level, ' ||
'apex_item.checkbox(1, ld.opt_in_auto_flag, decode(ld.opt_in_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_in, ' ||
'apex_item.checkbox(2, ld.opt_in_manual_flag, decode(ld.opt_in_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_in, ' ||
'apex_item.checkbox(3, ld.opt_out_auto_flag, decode(ld.opt_out_auto_flag,NULL,''disabled'',''Y'',''checked'')) auto_out, ' ||
'apex_item.checkbox(4, ld.opt_out_manual_flag, decode(ld.opt_out_manual_flag,NULL,''disabled'',''Y'',''checked'')) manual_out, ' ||
'DECODE(c.code, ''NMBR'', NULL,''Change to '' || DECODE(ld.level,''All'',''Categories'',''All Categories'')) link_change, ' ||
'APEX_ITEM.DISPLAY_AND_SAVE(11,c.code) code ' ||
'from ' ||
'tbl1 c, ' ||
'tbl2 ld ' ||
'where c.code = ld.code ' ||
'and c.type = ''TYPE1'' ' ||
'and c.active = ''Y'' ' ||
'order by c.sorting ';
RETURN v_SQL;
END;
it works fune... -
How to test a simple PL SQL function from another PL SQL script
Hi,
I have created a function. Now i need to test that whether it is returning the correct values or not.
For that, i have written anothe pl sql script and trying to call this function. Im passing all the IN parameters in that function. I assume here that OUT parameters will provide me the result. Im trying to display the OUT parameter one by one to see my result.
I'm using toad as sql client here connected with oracle.
pl sql script:-
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('$$$$$$$ VINOD KUMAR NAIR $$$$$$$');
FETCH_ORDER_PRODUCT_DATA(320171302, 1006, 6999,
ODNumber OUT VARCHAR2, Line_Number OUT VARCHAR2,
ServiceID OUT VARCHAR2, BilltoNumber OUT VARCHAR2,
AnnualPrice OUT NUMBER, CoverageCode OUT VARCHAR2)
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | ODNumber );
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | Line_Number );
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | ServiceID );
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | BilltoNumber );
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | AnnualPrice );
DBMS_OUTPUT.PUT_LINE('HERE IS THE RESULT ' | CoverageCode );
END;
Function:-
Program Name : SPOT_Order_Product_Data_For_CFS.sql
Description : Function to Validate parameters from CFS
By : Vinod Kumar
Date : 08/19/2011
Modification History
By When TAR Description
CREATE OR REPLACE FUNCTION FETCH_ORDER_PRODUCT_DATA(orderNumber IN VARCHAR2, customerNumber IN VARCHAR2,
productLine IN VARCHAR2, ODNumber OUT VARCHAR2,
Line_Number OUT VARCHAR2, ServiceID OUT VARCHAR2,
BilltoNumber OUT VARCHAR2, AnnualPrice OUT NUMBER,
CoverageCode OUT VARCHAR2)
RETURN VARCHAR2 IS
lv_err_msg VARCHAR2(100) := '';
lv_bucket_id VARCHAR2(14);
lv_bill_number VARCHAR2(30);
lv_anual_price NUMBER;
lv_coverage_code VARCHAR2(8);
lv_quote_num NUMBER(10) := NULL;
lv_line_num NUMBER(5) := 0;
lv_customer_number VARCHAR2(30) := customerNumber;
lv_product_id VARCHAR2(14) := productLine;
lv_count_quote NUMBER := 0;
lv_quote_status VARCHAR2(5);
lv_quote_version NUMBER(2):=0;
BEGIN
IF INSTR(orderNumber, '-') = 0 THEN
lv_quote_num := orderNumber;
ELSE
lv_quote_num := SPT_Delimiter(orderNumber, 1, '-');
lv_line_num := SPT_Delimiter(orderNumber, 2, '-');
END IF;
--Check status of the quote COM, APP
SELECT COUNT(*) INTO lv_count_quote FROM sot_order_header WHERE ORDER_NUMBER=lv_quote_num
AND ORDER_STATUS IN ('APP', 'COM') AND CUSTOMER_NUMBER = lv_customer_number;
IF lv_count_quote = 0 THEN
lv_err_msg := 'Invalid Order number';
RETURN lv_err_msg;
END IF;
-- Fetch the latest version on SPOT quote
SELECT MAX(VERSION_NUMBER) INTO lv_quote_version FROM SPT_QUOTE_HEADER WHERE QUOTE_NUMBER = lv_quote_num
AND CUSTOMER_NUMBER = lv_customer_number;
-- If quote is valid fetch the data in OUT parameters
IF lv_line_num = 0 THEN
BEGIN
SELECT a.CUSTOMER_BILLTO_NUMBER,
b.LINE_NUMBER, b.BUCKET_ID,
b.ANNUAL_REF_RATE_USD, b.COVERAGE_CODE
INTO lv_bill_number,lv_line_num,lv_bucket_id,lv_anual_price,lv_coverage_code
FROM SPT_QUOTE_HEADER a, SPT_QUOTE_LINE b
WHERE a.QUOTE_NUMBER = lv_quote_num
AND a.CUSTOMER_NUMBER = lv_customer_number
AND a.VERSION_NUMBER = lv_quote_version
AND a.QUOTE_NUMBER = b.QUOTE_NUMBER
AND a.VERSION_NUMBER = b.VERSION_NUMBER
AND b.PRODUCT_ID = lv_product_id;
ODNumber := lv_quote_num;
BilltoNumber := lv_bill_number;
Line_Number := lv_line_num;
ServiceID := lv_bucket_id;
AnnualPrice := lv_anual_price;
CoverageCode := lv_coverage_code;
RETURN '';
EXCEPTION WHEN OTHERS THEN
lv_err_msg := 'Multiple PIDs existing in the SPOT order, please provide the SPOT order + line number as input data';
RETURN lv_err_msg;
END;
ELSE
BEGIN
SELECT a.CUSTOMER_BILLTO_NUMBER,
b.BUCKET_ID, b.ANNUAL_REF_RATE_USD,
b.COVERAGE_CODE
INTO lv_bill_number,lv_bucket_id,lv_anual_price,lv_coverage_code
FROM SPT_QUOTE_HEADER a, SPT_QUOTE_LINE b
WHERE a.QUOTE_NUMBER = lv_quote_num
AND a.CUSTOMER_NUMBER = lv_customer_number
AND a.VERSION_NUMBER = lv_quote_version
AND a.QUOTE_NUMBER = b.QUOTE_NUMBER
AND a.VERSION_NUMBER = b.VERSION_NUMBER
AND b.PRODUCT_ID = lv_product_id
AND b.LINE_NUMBER = lv_line_num;
ODNumber := lv_quote_num;
BilltoNumber := lv_bill_number;
Line_Number := lv_line_num;
ServiceID := lv_bucket_id;
AnnualPrice := lv_anual_price;
CoverageCode := lv_coverage_code;
RETURN '';
EXCEPTION WHEN OTHERS THEN
lv_err_msg := 'Multiple SPOT lines exist with same parameter';
RETURN lv_err_msg;
END;
END IF;
EXCEPTION
WHEN OTHERS THEN
lv_err_msg := '@@@ EXCEPTION THROWN @@@ '|| SUBSTR(SQLERRM,1,120);
RETURN lv_err_msg ;
END;
Don't look at the function, it might have errors but my primary concern is how to test this function. Once I start doing its testing then only i can understand any bugs(if any).
My pl sql is not so good. Im still learning. I don't understand IN and OUT parameters are.
I just know that IN parameters r those whick we pass in to the function wen we call it and OUT parameters are those through which we get the result.
Thanks in advance
Vinod Kumar Nair20100511 wrote:
I wondered how I could test the output of the function from within TOAD?I usually create the following function in my developer schema:
create or replace function BoolToChar( b boolean ) return varchar2 is
begin
if b then
return( 'TRUE' );
else
return( 'FALSE' );
end if;
end;To test a function like yours, the following will do in SQL*Plus/TOAD/etc:
begin
DBMS_OUTPUT.put_line(
BoolToChar( XCCC_PO_APPROVALLIST_S1.does_cpa_exist(1017934) )
end;
I'm probably doing 101 things wrong here, but thought I'd ask anyway and risk being shouted at.Shout at? You reckon? I thought people risked being beaten with a lead pipe, or pelted with beer cans and stale pretzels - which makes being shouted at a really safe and viable alternative. {noformat};-){noformat} -
How to user HTMLDB_COLLECTIONS in a PL/SQL function?
Hi!
I am trying to validate/check that an item from an updateable report is not in a collection before I add the item. I wrote a PL/SQL function to do this. It appears that a "normal" PL/SQL function cannot correctly see the HTMLDB_COLLLECTIONS view. The idea for this was posted to the forum about using a decode statement and a function to detemine whether to display the link value or not for each row. If an item has been added to the collection I want to not display the link. If the item is removed from the collection I need to redisplay the link.
I have tried validations but they depend on the page being resumbitted. And I tried to force that using a branch where the request was = 'ADD'. However everytime I set it up as a redirect to a URL it kept resetting the branch to a 'branch to a page within this application'.
Any suggestions on how to check the HTMLDB_COLLECTIONS before it is updated?
This is V2.0.
Thanks! Dave VenusHi!
Yes things work much better when run from within the "application". I had been trying to test the code from both SQL*Plus and from the SQL WorkShop.
The problem I was having even within the application was that I was getting back NULL return values. So I put some exception handling into the code and trapped the
data_not_found and others exceptions. I also had SQL%NOTFOUND code in my function. It appears the SQL%NOTFOUND was not being processed and the exception code was. I can paste the code I was using here if you want.
I went another way around the problem and rewrote the query (esp. the where clause) for the report and it seems to be doing what I need now. I would love to get the function working and understand how it should have worked so that I can use that functionallity in other applications.
Thanks!
Dave Venus -
SQL Query (pl/sql function body returning Sql query)
Hi All,
I have created a region of "SQL Query (pl/sql function body returning Sql query)" type and it is working fine , but when I am migrating(export /import) this application from development to systest environment ,
It gives error for this region :
Error ERR-1101 Unable to process function body returning query.
OK
ORA-06550: line 1, column 52: PLS-00306: wrong number or types of arguments in call to 'FU_TRADE_REPORT_QUERY' ORA-06550: line 1, column 45: PL/SQL: Statement ignored
Any pointer ...why this is happening.
Thanks
DikshitIf your function is a stored function that is called from within APEX (function body not coded into the app itself), have you made sure that the function has been created and compiles ok prior to installing your apex app.
If there are some dependency issues between other PL/SQL units or database objects that are causing your function not to be compiled, you apex install will fail as you are trying to reference an uncompelled bit of pl/sql.
Let me know how you get on
Regards
Duncan -
Query on Reoprts-pl/sql function body returning sql query
Hi,
I am facing a starnge problem.. and would like to know the reason for it.
The situation is as follows:
I have a report (pl/sql function body returning sql query based). My query is as follows:
declare
l_query1 varchar2(2000);
begin
if (:P102_min_value is not null and :P102_max_value is null) then
l_query1 := 'select decode(:P102_date,'1',date1) Transaction_Date, decode(:P102_first_name,'2',first_name) First_Name from (
select * from MORTGAGE_LOAN_LEADS where APPRX_LOAN_AMOUNT >=:P102_min_value
return (l_query1);
end if;
end;
This returns the error as :
1 error has occurred
Function returning SQL query: Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the generic columns checkbox below the region source to proceed without parsing.
(ORA-06550: line 7, column 20: PLS-00103: Encountered the symbol "1" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_ between || multiset member SUBMULTISET_)
How do i correct this? am i right in thinking that the single quotes indicating a string within a select query is creating the problem? Beacuse if i use an sql query report and use the following query, it is working fine...
select
decode(:P102_date,'1',date1) Transaction_Date,
decode(:P102_first_name,'2',first_name) First_Name
from (
select * from MORTGAGE_LOAN_LEADS where APPRX_LOAN_AMOUNT >=:P102_min_value
But i need to write pl/sql function, so that multiple report can be returned based on different conditions. Please guide me.
Thanks,
SumanaDoes that mean that both in if and else we need to have same columns selected?
if thats the case.. any idea how i can implement it in my code. The situation here is that the columns that need to be selected are dynamic and keep on changing...
if i try to implement like this and run the page, it gives the following error:
Query is:
declare
l_query1 varchar2(4000);
begin
if apex_application.get_current_flow_sgid(:APP_ID) = apex_application.get_sgid then
if (:P102_min_value is not null and :P102_max_value is null) then
l_query1 := 'select decode(' || :P102_date|| ',''1'',date1) Transaction_Date, decode(' || :P102_first_name || ',''2'',first_name) First_Name from (
select * from MORTGAGE_LOAN_LEADS where APPRX_LOAN_AMOUNT >=' || :P102_min_value ||'
elsif (:P102_min_value is null and :P102_max_value is not null) then
l_query1 := 'select decode(' || :P102_date|| ',''1'',date1) Transaction_Date, decode(' || :P102_first_name || ',''2'',first_name) First_Name from (
select * from MORTGAGE_LOAN_LEADS where APPRX_LOAN_AMOUNT >=' || :P102_max_value ||'
else
l_query1 := 'select date1,first_name from MORTGAGE_LOAN_LEADS where rownum = 1';
end if;
else
l_query1 := 'select date1,first_name from MORTGAGE_LOAN_LEADS where rownum = 1';
end if;
return l_query1;
--dbms_output.put_line( l_query1);
end;
The Error is:
failed to parse SQL query:
ORA-00936: missing expression
If i try to run the same in command prompt, it always goes to the else part... (else of outer if) -
SQL Query (PL/SQL function body returning SQL query) when using to_char
we are trying to build a report page of Type SQL Query (PL/SQL function body returning SQL query).
our query is so simple, we need to extract the month from the recording_date column.
declare
l_query varchar2(1000);
begin
l_query:='select to_char(recording_date,'MM')from re_productive';
return l_query;
end;
but we are having the following problem for this query
Function returning SQL query: Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the generic columns checkbox below the region source to proceed without parsing.
(ORA-06550: line 4, column 42: PLS-00103: Encountered the symbol "MON" when expecting one of the following: . ( * @ % & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like between || multiset member SUBMULTISET_ The symbol ". was inserted before "MON" to continue.)
Notes:
1-the query is correct and it was tested under Toad and SQL Plus.
2- we tried Use Generic Column Names (parse query at runtime only) option but we get the same problem.
any quick help please.Hi
You haven't escaped your quotes in the string. Try this...
DECLARE
l_query VARCHAR2(32767);
BEGIN
l_query:= 'select to_char(recording_date,''MM'') from re_productive';
RETURN l_query;
END;Cheers
Ben -
How to distinguish built-in SQL functions of PL/SQL?
I m having a hard time to figure out which functions are used ONLY in SQL statements and which are used in regular expr(ie, variable assignment,). Can anyone show me a list of each or perhaps a URL to look for?
I have searched through either the developer's guide and reference but couldn't find any appropriate indication in one place that make it clear.
For instance, I thought I can use CAST function in a variable assginment like the following:
declare
cursor myCur is SELECT Value_varchar2(1) FROM table WHERE id = 1;
myRec myCur%ROWTYPE;
var_a NUMBER(1);
begin
OPEN myCur;
FETCH myCur INTO myRec;
CLOSE myCur;
var_a := CAST(myCur.Value_varchar2(1) AS NUMBER(1));
DBMS_OUTPUT.PUT_LINE('var_a = ' || TO_CHAR(var_a));
end;
. It seems like CAST function can ONLY be used in SQL statement, but no doc so far states that?!
Edited by: HappyJay on 2010/05/12 12:05Sorry to bother you, Bob!
I think I might already found the list. Is it the following list?
---------------------- QUOTED FROM Oracle® Database PL/SQL Language Reference 11g Release 2 (11.2)Part Number E10472-06
SQL Functions in PL/SQL Expressions
In PL/SQL expressions, you can use all SQL functions except:
Aggregate functions (such as AVG and COUNT)
Analytic functions (such as LAG and RATIO_TO_REPORT)
Data mining functions (such as CLUSTER_ID and FEATURE_VALUE)
Encoding and decoding functions (such as DECODE and DUMP)
Model functions (such as ITERATION_NUMBER and PREVIOUS)
Object reference functions (such as REF and VALUE)
XML functions (such as APPENDCHILDXML and EXISTSNODE)
These conversion functions:
BIN_TO_NUM
These miscellaneous functions:
CUBE_TABLE
DATAOBJ_TO_PARTITION
LNNVL
NVL2
SYS_CONNECT_BY_PATH
SYS_TYPEID
WIDTH_BUCKET
PL/SQL supports an overload of BITAND for which the arguments and result are BINARY_INTEGER.
When used in a PL/SQL expression, the RAWTOHEX function accepts an argument of data type RAW and returns a VARCHAR2 value with the hexadecimal representation of bytes that comprise the value of the argument. Arguments of types other than RAW can be specified only if they can be implicitly converted to RAW. This conversion is possible for CHAR, VARCHAR2, and LONG values that are valid arguments of the HEXTORAW function, and for LONG RAW and BLOB values of up to 16380 bytes.
----------------------
Maybe you are looking for
-
Table for purchase order history
Hi All, I am working on Import PO. I have created PO and I have done MIRO for planned delivery costs now when I am checking in EKBE table its not getting updated but I am able to see the same documents in PO history. Please tell me in which table pla
-
Dear expert how can i find closed po's in the system i.e those po's whose delievery is completed but there is no goods reciept for that PO Regards
-
Why is the top part of my computer black
How do i undo the first four out of five top lines from being from black ,up around the top of the sreen.
-
  values showing in the 11g reports!
Hello, Can anyone please help me with the following scenario. I have a report with few columns and rows. Some of the cells do not have any data in them. But when I see the final report I see   displayed in the cells. Is there a way to show empty
-
How to configure Integration Builder using an Integration Process?
Dear experts, I have problems configuring the Integration builder using a Integration process. Scenario = idoc to PI, integration process transforms idoc to file, file to external system. My integration process starts with an abstract interface (stru