Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Thursday, December 19, 2013

Extracting values from an XML string using SQL query in oracle database

There are two methods you can use for extracting values from XML string (a column in a table). The column type will be XMLType.

These methods are EXTRACT(columnName, xpath). This returns list of nodes, also a record if extracting with a unique filter value.
You can also use ExtractValue(columnName, xpath) - This will work only for a single node, not for a collection of nodes. This way you can retrieve value from a unique record.


See examples below:

<Person>
    <Name>Test</Name>
    <Address>
        <Street Number=1345>Monroe St</Street>
        <City>TestCity</City>
        <Zip>12345</Zip>
    </Address>
</Person>

Assume this xml data in column
You can extract the street number attribute value using the query as below:

This can return multiple records:
select extract(columnName,'/Person/Address/Street/@Number') as StreetNumber from Table1;

For returning single record:
select extract(columnName,'/Person/Address/Street/@Number') as StreetNumber from Table1 where ID = 200;

Also For returning single record only:
select extractvalue(columnName, '/Person/Address/Street/@Number') as StreetNumber from Table1 where ID = 200;

I have used this and works like a charm.
Hope this finds useful to you also.
Njoyy...

Friday, June 8, 2012

Same parameters used multiples times in Oracle Query

If we use same parameters multiple times in a query in oracle as shown in example, it errors out. We need to create another set of variables with same values.


Check the usage of parameters in the below example:
:fromDate
:toDate
:uname


:fromDate1
:toDate1
:uname1



:fromDate2
:toDate2
:uname2


All the above paramaters are having the same values.


For example:-
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN

select user_name,call_reason from call_mrs_tbl, users_tbl where call_placed_dt between :fromdate1 and :toDate1
and user_name = :uname1 order by user_description
FULL JOIN

select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate2 and :toDate2
and user_name = :uname2 order by user_description;

We cannot use the above query as below. It errors out saying invalid datatype.


select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN
select user_name,call_reason from call_mrs_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description;











Order of parameters matters in oracle query

Faced lots of time to figure out why my oracle query was erroring out. It was the order of the dynamic parameters in the query should match the order we pass the parameter with values.

For example:-
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description;

Here you can see three parameters :fromDate, :toDate & :uname. These parameters needs to be passed from C# using command object in the following order in which it is used in the query, which is as below:
1. :fromDate
2. :toDate
3. :uname

I was passing the parameters in the following order:


1. :uname
2. :fromDate
3. :toDate


Took me a while to figure out. These are some tips which can be used if you stumble across such error again, you need not waste much time to figure out.

Hope this helps others.