---
title: "Associate Vs. Allocate: DB2 Stored Procedure Example"
description: "Here's an example of DB2 Stored procedure and explained the purpose of Associate and Allocate."
url: https://srinimf.com/2016/10/24/how-to-know-the-cursor-type-is-associate-in-db2/
date: 2016-10-24
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/10/pexels-photo-530024.jpeg?fit=1880%2C1253&ssl=1
categories: ["SQL Stored Procedures"]
tags: ["DB2 Cursors", "What is Associate Cursor in DB2"]
type: post
lang: en
---

# Associate Vs. Allocate: DB2 Stored Procedure Example

Here’[s all about](https://srinimf.com/2015/02/22/nested-cursor-db2/) the Associate/Allocate cursor. The purpose of Associate is to get the cursor from the [Stored procedure](https://srinimf.com/2015/08/17/calling-stored-procedure-in-db2-a-best-example/) that’s called. The Allocate just assigns a cursor name to it.

## DB2 nested [Stored procedure](https://srinimf.com/2013/02/19/stored_procedure_cobol/)

```
CREATE PROCEDURE EMP_NAME (OUT p_name VARCHAR(30)) 
  BEGIN
    DECLARE result RESULT_SET_LOCATOR VARYING;
 
    -- Call the procedure returning the result set
    CALL emp();
 
    -- Get a cursor for the result set
    ASSOCIATE RESULT SET LOCATOR (result) WITH PROCEDURE emp;
    ALLOCATE cur CURSOR FOR RESULT SET result;
 
    -- Cursor already open, so you can start fetching rows
    FETCH cur INTO p_name;
    -- ...
    CLOSE cur;
END
```

Related[How to Resolve SQL Error -500 Quickly](https://srinimf.com/2015/12/05/sql-error-500-cursor-was-closed-when-the-connection-destroyed/)

**Recent Posts**

Related[DB2 Fetch ideas on how to optimize result](https://srinimf.com/2012/05/02/db2-fetch-rules/)

- [30 SQL Practice Queries with Solutions — Run Them Live](https://srinimf.com/2026/10/01/30-sql-practice-queries-with-solutions-run-them-live/)
- [Supercharge Your Database Skills: Try Our New Interactive SQL Plugin!](https://srinimf.com/2026/09/30/supercharge-your-database-skills-try-our-new-interactive-sql-plugin/)
- [How to Count Spark Stages by Sight and Optimize Shuffles](https://srinimf.com/2026/09/27/how-to-count-spark-stages-by-sight-and-optimize-shuffles/)

References

- [Examples of DB2 Associate and Allocate Cursor](http://www.sqlines.com/db2-to-oracle/process_result_sets_in_procedures)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2016%2F10%2F24%2Fhow-to-know-the-cursor-type-is-associate-in-db2%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fpexels-photo-530024.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Associate%20Vs.%20Allocate%3A%20DB2%20Stored%20Procedure%20Example)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2016/10/24/how-to-know-the-cursor-type-is-associate-in-db2/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2016/10/24/how-to-know-the-cursor-type-is-associate-in-db2/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2016/10/24/how-to-know-the-cursor-type-is-associate-in-db2/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2016/10/24/how-to-know-the-cursor-type-is-associate-in-db2/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Associate%20Vs.%20Allocate%3A%20DB2%20Stored%20Procedure%20Example&body=https%3A%2F%2Fsrinimf.com%2F2016%2F10%2F24%2Fhow-to-know-the-cursor-type-is-associate-in-db2%2F&share=email)
-

### Like this:

Like Loading…
