How to omit aliased used in XMLForest function in oracle XML - oracle

pl SQL code segment
SELECT Xmlserialize(DOCUMENT
XMLELEMENT("intrastat",
XMLAGG(
Xmlforest(ENVELOPE_ID AS "envID",
XMLFOREST(DATE_ AS "date",TIME_ AS "Time")AS "Date
time", PARTY_ID AS "pid",PARTY_NAME AS "pname",
XMLFOREST(Xmlelement("RC",REGION_CODE) AS RC,Xmlelement("TCPCODE",MODE_OF_TRANSPORT_CODE) AS TCPCODE) AS "item")
)))
FROM INTRASTAT_XML_TEMPLATE_LINE_TMP
part of actual output that make the trouble
<item><RC><RC>as</RC></RC><TCPCODE><TCPCODE>22</TCPCODE></TCPCODE></item>
what i want to get
<item><RC>ads</RC><TCPCODE>22</TCPCODE></item>

Your current plsql code segment :
XMLFOREST(Xmlelement("RC",REGION_CODE) AS RC,Xmlelement("TCPCODE",MODE_OF_TRANSPORT_CODE) AS TCPCODE) AS "item")
And as we have used aliases in here, we are getting multiple tags - One for alias, and one for the first parameter of XMLELEMENT function.
Now, since you just want an element - item, with two tags - RC (holding data of REGION_CODE field) and TCPCODE (holding data of MODE_OF_TRANSPORT_CODE field),
in my opinion, this should suffice your requirement:
Xmlelement("item", XMLFOREST(REGION_CODE "RC", MODE_OF_TRANSPORT_CODE "TCPCODE")
~Kuntal

with data(rc, tcpcode) as (select 'ads', 22 from dual)
select xmlelement("item", xmlforest(rc, tcpcode)) from data;
XMLELEMENT("ITEM",XMLFOREST(RC,TCPCODE))
--------------------------------------------------------------------------------
<item><RC>ads</RC><TCPCODE>22</TCPCODE></item>

Related

BIRT allow user to dynamically select report's columns

I want to add an option for the user when creating the report to select the columns that the report will show. See the attached image below on how it is suppose to look.
Is there a way to do that?
I don't know about the parameter dialog, but assuming that your column names are in an array.
You can have an SQL query with all possible column names
(probably you should use special comments to mark the beginning and end of the select list).
E.g.
select
'X' as dummy
-- BEGIN COLS
, column1
, column2
...
-- END COLS
from ...
where ...
order by ...
Then, in the beforeOpen event of the query, you can access and modify the query with this.queryText (IIRC) and remove all those lines ("," + columnname) in the marked part for which columnname is not contained in the array.

Multi values search from one field PLSQL

I'm creating a report in PLSQL.
The report is working fine and additionally, I need to add when users search multiple values from one field separating by ;. It should select all the data that he entered.
Example: Salesman - Amanda; Michelle; Sharmain
Then it should select data regarding amenda, michelle and sharmain.
My code only returns data for one value only.
please refer this line from the code and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
where i.catalog_group='FPMB'
and i.order_no like 'M%'
and ((i.invoice_date between to_date( '&From_Date', 'YYYY/MM/DD' ) and to_date( '&To_Date', 'YYYY/MM/DD' ) ) or ('&From_Date' is null and '&To_Date' is null))
and t.source_ref1=i.order_no
and (t.source_ref3=i.release_no)
and (t.source_ref2=i.line_no)
and i.contract=t.contract
and t.transaction_code='OESHIP'
and t.qty_reversed=0
and t.source_ref1=i.order_no
and t.serial_no <> '*'
and t.cost<>0.00
and i.order_no LIKE '&Order_No
and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
You can use hierarchy query as follows:
ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) IN
(SELECT TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL))
FROM DUAL CONNECT BY TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL)) IS NOT NULL)

Retrieving list of values in a clob

How can I retrieve a list off nth occurence of data in a clob?
Example of a clob:
<bank>
<bankDetails>
<bankDetailsList>
<pk>1</pk>
<accountName>
<asCurrent>EDGARS LESOTHO</asCurrent>
</accountName>
<bankAccountType>
<asCurrent>CURR</asCurrent>
</bankAccountType>
</bankDetailsList>
<bankDetailsList>
<pk>2</pk>
<accountName>
<asCurrent>EDGARS LESOTHO 2</asCurrent>
</accountName>
<bankAccountType>
<asCurrent>CURR</asCurrent>
</bankAccountType>
</bankDetailsList>
</bankDetails>
</bank>
So I would like to retrieve all values of account names in sql assuming there might be up to nth list of this account names occurring in a clob.
I am using oracle 11g and SqlDeveloper 4.1.3
Your response is highly appreciated.
SELECT EXTRACTVALUE( v.COLUMN_VALUE, '/asCurrent' )
FROM table_name t,
TABLE(
XMLSequence(
EXTRACT(
XMLType( t.clob_column ),
'/bank/bankDetails/bankDetailsList/accountName/asCurrent'
)
)
) v
SELECT level as rnk, regexp_substr(t.clob_column,
'<accountName>[^<]*?<asCurrent>([^<]*?)<', 1, level, null, 1) as acct_name
FROM t
CONNECT BY level <= (select regexp_count(clob_column, '<accountName>') FROM t);
t is the table name and clob_column is the column with clob values (in my test case, the table has one row and one column, the value being the one in the original post).
If you have a column of clob values and need to do this simultaneously for more than one value, this needs to be modified a bit; please clarify the requirement and we can take it from there.
ADDED: To make it work with several rows, you need to modify the CONNECT BY LEVEL clause. You want each row to only reference itself; and to avoid issues with cycles, you need to add one more condition. Like this:
...
CONNECT BY level <= (select regexp_count(clob_column, '<accountName>') FROM t)
and clob_column= prior clob_column
and prior sys_guid() is not null;

birt sql dataset with parameters sql error

I have a birt dataset for a db2 query. My query works fine without parameters with the following query...
with params as (SELECT '2014-02-16' enddate,'1' locationid FROM sysibm.sysdummy1)
select
t.registerid
from (
select
...
FROM params, mytable sos
WHERE sos.locationid=params.locationid
AND sos.repositorytype ='xxx'
AND sos.repositoryaccountability='xxx'
AND sos.terminalid='xxx'
AND DATE(sos.balanceDate) between date(params.enddate)-6 DAY and date(params.enddate)
GROUP BY sos.terminalid,sos.balancedate,params.enddate) t
GROUP BY
t.registerid
WITH UR
But when I change the top line to ...
with params as (SELECT ? enddate,? locationid FROM sysibm.sysdummy1)
And make the two input paramters of string datatype I get db2 errors sqlcode -418. But i know that it is not my querty because my query works.
What is the right way for me to set up the parameters so there is no error?
thanks
I'm not familiar with DB2 programming, but on Oracle the ? works anywhere in the query.
Have you looked at http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z9.doc.codes%2Fsrc%2Ftpc%2Fn418.htm?
Seems that on DB2 it's a bit more complicated and you should use "typed parameter markers".
The doc says:
Typed parameter marker
A parameter marker that is specified with its target data type. A typed parameter marker has the general form:
CAST(? AS data-type)
This invocation of a CAST specification is a "promise" that the data type of the parameter at run time will be of the data type that is specified or some data type that is assignable to the specified data type.
Apart from that, always assure that your date strings are in the format that the DB expects, and use explicit format masks in the date function, like this:
with params as (
SELECT cast (? as varchar(10)) enddate,
cast (? as varchar2(80)) locationid
FROM sysibm.sysdummy1
)
select
...
from params, ...
where ...
AND DATE(sos.balanceDate) between date(XXX(params.enddate))-6 DAY and date(XXX(params.enddate))
...
Unfortunately I cannot tell you how the XXX function should look on DB2.
On Oracle, an example would be
to_date('2014-02-18', 'YYYY-MM-DD')
On DB2, see Converting a string to a date in DB2
In addition to hvb answer, i see two options:
Option 1 you could use a DB2 stored procedure instead of a plain SQL query. Thus there won't be these limitations you face to, due to JDBC query parameters.
Option 2, we should be able to remove the first line of the query "with params as" and replace it with question marks within the query:
select
t.registerid
from (
select
sos.terminalid,sos.balancedate,max(sos.balanceDate) as maxdate
FROM params, mytable sos
WHERE sos.locationid=?
AND sos.repositorytype ='xxx'
AND sos.repositoryaccountability='xxx'
AND sos.terminalid='xxx'
AND DATE(sos.balanceDate) between date(?)-6 DAY and date(?)
GROUP BY sos.terminalid,sos.balancedate) t
GROUP BY
t.registerid
A minor drawback is, this time we need to declare 3 dataset parameters in BIRT instead of 2. More nasty, i removed params.endDate from "group by" and replaced it with "max(sos.balanceDate)" in select clause. This is very near but not strictly equivalent. If this is not acceptable in your context, a stored procedure might be the best option.

pl-sql include column names in query

A weird request maybe but. My boss wants me to create an admin version of a page we have that displays data from an oracle query in a table.
The admin page, instead of displaying the data (query returns 1 row), needs to return the table name and column name
Ex: Instead of:
Name Initial
==================
Bob A
I want:
Name Initial
============================
Users.FirstName Users.MiddleInitial
I realize I can do this in code but would rather just modify the query to return the data I want so I can leave the report generation code mostly alone.
I don't want to do it in a stored procedure.
So when I spit out the data in the report using something like:
blah blah = MyDataRow("FirstName")
I can leave that as is but instead of it displaying "BOB" it would display "Users.FirstName"
And I want to do the query using select * if possible instead of listing all the columns
So for each of the columns I am querying in the * , I want to get (instead of the column value) the tablename.ColumnName or tablename|columnName
hope you are following- I am confusing myself...
pseudo:
select tablename + '.' + Columnname as WhateverTheColumnNameIs
from Table1
left join Table2 on whatever...
Join Table_Names on blah blah
Whew- after writing all this I think I will just do it on the code side.
But if you are up for it maybe a fun challenge
Oracle does not provide an authentic way(there is no pseudocolumn) to get the column name of a table as a result of a query against that table. But you might consider these two approaches:
Extract column name from an xmltype, formed by passing cursor expression(your query) in the xmltable() function:
-- your table
with t1(first_name, middle_name) as(
select 1,2 from dual
), -- your query
t2 as(
select * -- col1 as "t1.col1"
--, col2 as "t1.col2"
--, col3 as "t1.col3"
from hr.t1
)
select *
from ( select q.object_value.getrootelement() as col_name
, rownum as rn
from xmltable('//*'
passing xmltype(cursor(select * from t2 where rownum = 1))
) q
where q.object_value.getrootelement() not in ('ROWSET', 'ROW')
)
pivot(
max(col_name) for rn in (1 as "name", 2 as "initial")
)
Result:
name initial
--------------- ---------------
FIRST_NAME MIDDLE_NAME
Note: In order for column names to be prefixed with table name, you need to list them
explicitly in the select list of a query and supply an alias, manually.
PL/SQL approach. Starting from Oracle 11g you could use dbms_sql() package and describe_columns() procedure specifically to get the name of columns in the cursor(your select).
This might be what you are looking for, try selecting from system views USER_TAB_COLS or ALL_TAB_COLS.

Resources