Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Looking at each value in a VARRAY

I have a table containing a VARRAY of objects as shown below. But I don't know how to find which divisions have...

an employee called John working in them. How could I look each value of the VARRAY: if the first value is not John, look at the next and so on until I get the value desired, or it realizes that the value doesn't exist?

```CREATE OR REPLACE TYPE	emp_obj
AS	OBJECT
(
last_name       VARCHAR (50),
first_name      VARCHAR (50),
birth_dt        DATE
);
/

CREATE OR REPLACE TYPE	emp_va
AS	VARRAY (999)
OF	emp_obj;
/

CREATE TABLE	division
(
division_id	NUMBER (9)	PRIMARY KEY,
division_name	VARCHAR2 (100),
emps		emp_va
);
```
```SELECT	e.first_name
FROM	division        d
,	TABLE (d.emps)  e
WHERE	d.division_name	= 'Sales';
```

You've already done the hard part, isolating one component of each entry in the VARRAY, and you've shown how to use it in a SELECT clause. You can use single elements from the VARRAY in the WHERE clause in exactly the same way:

```SELECT DISTINCT
d.division_name
FROM    division        d
, TABLE (d.emps)  e
WHERE   e.first_name = 'John';
```
what how
```SELECT  division_name
FROM    division        d
WHERE   EXISTS
( SELECT  'X'   -- Why 'X'?  See note below.
FROM    TABLE (d.emps)
WHERE   first_name = 'John'
);
```
Why 'X'?

This problem does not really revolve around the VARRAY: you could use the same techniques on plain old regular vanilla-flavored relational tables:

```-- The straightforward but slower solution
SELECT DISTINCT
d.division_name
FROM    division d
JOIN  emp      e USING (division_id)
WHERE   e.first_name = 'John';

-- The faster but more convoluted solution
SELECT  division_name
FROM    division d
WHERE   EXISTS
( SELECT  'X'
FROM    emp
WHERE   first_name = 'John'
AND   division_id = d.division_id
);
```

If you want run the examples, or to experiment with related questions, you might want to copy these statements to populate the tables.

```
-- After creating the division table, as shown on the main page,
-- the following INSERT statements can be used to populate it.

-- The 'Sales' division will have two employees named John.
INSERT INTO division
(
division_id,
division_name,
emps
)
VALUES
(
1,
'Sales',
emp_va ( emp_obj ('Adams', 'John',    '30-Oct-1735'),
emp_obj ('Brown', 'John',    ' 9-May-1800')
)
);

-- The 'Administration' division will have no employees named John.
INSERT INTO division
(
division_id,
division_name,
emps
)
VALUES
(
2,
emp_va ( emp_obj ('Bingen', 'Hildegard', '17-Sep-1098'),
emp_obj ('Bush',   'George W.', ' 6-Jul-1946')
)
);

-- Now the SELECT statements (based on the division table, only)
-- on the main page will run.

CREATE TABLE emp
AS
SELECT  d.division_id
, e.last_name
, e.first_name
, e.birth_dt
FROM    division       d
, TABLE (d.emps) e;

-- Now the queries on the main page that use the emp table will work.
```

• Dozens more answers to tough SQL and PL/SQL questions from Jason Law.

• The Best SQL and PL/SQL Web Links: tips, tutorials, scripts and more.

• Have a tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!

• I know how to get all the items in my VARRAY with a query like: That's the cleanest, easiest to understand way to get the results you want. Like many SQL statements, it does you want, but it doesn't do it you want it to: it doesn't stop after finding the first match within a single division. The solution below does quit looking after it finds the first match, so it will usually run faster: You could SELECT anything in the subquery: a literal (like I did) a column, SYSDATE, USER or a list of any kind(s) of values. The only crucial thing is whether or not the subquery returns any rows. SELECTing a literal is faster than SELECTing a real column or SYSDATE, so that's what I did.
This was last published in July 2003

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Hyperledger Fabric offers path to enterprise blockchain future

Blockchain arose from bitcoin, but it's looking to find a place in the enterprise. Frameworks like Hyperledger Fabric could ...

• ### MongoDB 4.0 takes ACID transactions to multi-document level

MongoDB is taking a deeper step into SQL-style processing waters with a 4.0 update that brings increased support for ...

• ### Data lake concept needs firm hand to pay big data dividends

Data lakes pose technology deployment and data management challenges that can leave analytics users high and dry if the ...

• ### AI functionality limited today but could be a game-changer

Limited AI capabilities could soon give way to technology that is truly transformative for enterprises, surpassing the overhyped ...

## SearchSAP

• ### ControlPanelGRC app eases Steelcase's compliance pain

When Steelcase's SAP environment grew in size and complexity, it turned to Symmetry ControlPanelGRC to save time, have more ...

• ### Translytical data platforms emerge with SAP HANA as a leader

SAP HANA is a leading translytical platform, according to Forrester, and consulting firm Convergent IS says the combination of ...

• ### SAP HANA and Esri combine for geospatial database platform

SAP and Esri are combining SAP HANA's in-memory database capabilities with Esri geospatial applications, and utilities are taking...

## SearchSQLServer

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Pluralsight IQ, Stack Overflow boost developer street cred

Tying the Pluralsight IQ skills test to the Stack Overflow Developer Story helps developers measure their technical skills and ...

• ### Why this quantum computing breakthrough is a security risk

Quantum computing will void pretty much all security encryption techniques and open the door to hackers. Here's how to protect ...

• ### Database automation drives DevOps into the persistence layer

A new breed of software tools is driving DevOps processes deep into the persistence layer, as database automation and continuous ...

## SearchDataCenter

• ### Evaluate read-intensive and write-intensive SSD use cases

Consider write wear, performance and other factors when choosing between read-intensive, write-intensive and mixed-use ...

• ### Some hyper-converged infrastructure use cases pose pitfalls

Hyper-converged infrastructure adoption is skyrocketing, but that doesn't mean that the technology is the best choice for every ...

• ### Dell hyper-converged reorg streamlines products, ups CI odds

Market pressures and manufacturing synergies drove Dell to integrate its HCI and CI products with its core business units, but ...

## SearchContentManagement

• ### Content management in the cloud a main theme in 2018

The future of content management resides in the cloud and with AI, as several 2018 conferences will assure you.

• ### Six things to know about today's SharePoint implementations

As companies migrate their on-premises Microsoft SharePoint sites to the cloud, here are some things they should know about the ...

• ### Upgrades for the SharePoint Online portal

As more organizations migrate SharePoint sites to the cloud, Microsoft has increased at-a-glance dashboard data and analytics to ...

## SearchHRSoftware

• ### Don't overlook the many benefits of Microsoft Excel for HR

The maligned spreadsheet tool is no substitute for enterprise apps like HRMS and people analytics, but it will do in a pinch and ...

• ### HR is failing to use people analytics tools, new report says

Human resource departments fail to use people analytics tools effectively, a new global study concludes. The findings were called...

• ### Does your company need new human resource management tools?

Finding the best human resource management tools starts with identifying your company's present and future needs, before ...

Close