<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLochlainns Weblog &#187; PL/SQL actual parameters</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/tag/plsql-actual-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 00:02:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Quick review of PL/SQL formal parameter modes</title>
		<link>http://blog.mclaughlinsoftware.com/2008/10/26/quick-review-of-plsql-formal-parameter-modes/</link>
		<comments>http://blog.mclaughlinsoftware.com/2008/10/26/quick-review-of-plsql-formal-parameter-modes/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 05:11:57 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[IN mode]]></category>
		<category><![CDATA[IN OUT mode]]></category>
		<category><![CDATA[OUT mode]]></category>
		<category><![CDATA[PL/SQL actual parameters]]></category>
		<category><![CDATA[PL/SQL formal parameters]]></category>
		<category><![CDATA[PL/SQL function parameter modes]]></category>
		<category><![CDATA[PL/SQL pass-by-reference]]></category>
		<category><![CDATA[PL/SQL pass-by-value]]></category>
		<category><![CDATA[PL/SQL procedure parameter modes]]></category>

		<guid isPermaLink="false">http://maclochlainn.wordpress.com/?p=656</guid>
		<description><![CDATA[My students wanted a supplement on how variable modes work in PL/SQL, so I figured it would fit nicely in a quick blog entry. If you're interested, read on [...]]]></description>
			<content:encoded><![CDATA[<p>My students wanted a supplement on how variable modes work in PL/SQL, so I figured it would fit nicely in a quick blog entry. If you&#8217;re interested, read on &#8230;</p>
<p>PL/SQL supports three patterns of variable modes in functions and procedures. The easiest supports a <em>pass-by-value</em> function or procedure, and it is the <code>IN</code> mode of operation. The other two are related but different, and support a <em>pass-by-reference</em> function or procedure. The differences between a function and procedure are straightforward: (1) A function can return a value as an output, which is known as an expression; (2) A function can be used as a right operand; (3) A procedure can&#8217;t return a value because it more or less returns a void (borrowing from the lexicon of C, C++, C#, or Java and many other languages), (4) A procedure can be used as a statement by itself in a PL/SQL program while a function can&#8217;t. The variables you define in a function or procedure signature (or prototype) are the formal paramters, while the values or variables assigned when calling a function or procedure are the actual parameters.</p>
<p><strong><code>IN</code> mode:</strong></p>
<p>An <code>IN</code> mode variable is really a copy of the variable, but you can ask to pass a reference. PL/SQL typically obliges when using the <code>IN</code> mode of operation. The following defines a pass-by-value PL/SQL function (other than the return type, you could do the same in a procedure too):</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inonly.png"><img class="aligncenter size-full wp-image-657" title="inonly" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inonly.png" alt="" style="border:none" width="561" height="162" /></a></p>
<p>You can test the values of the actual parameter before and after the function call while also testing it inside the function. You can also assign a literal number or string as the actual parameter because the <code>IN</code> mode only requires a value because it discard the variable reference and value when it completes.</p>
<p>There is an exception data type for this IN mode operation, and it is the PL/SQL system reference data type (<a title="Reference Cursors" href="http://blog.mclaughlinsoftware.com/2008/10/11/reference-cursors-why-when-and-how/" target="_blank">more on this type can be found in the following post</a>). A PL/SQL reference cursor can only be passed when it is already opened, and it actually passes a reference to the cursor work area in the Private Global Area (PGA).</p>
<p>You can&#8217;t assign a value to a formal parameter inside a function when the variable has the default (or <code>IN</code>) mode of operation. Any attempt to do so raises a <code>PLS-00363</code> with a warning that expression (formal parameter) can&#8217;t be used as an assignment target. A test of the function follows:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inmodetest.png"><img class="aligncenter size-full wp-image-658" title="inmodetest" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inmodetest.png" alt="" style="border:none" width="561" height="184" /></a></p>
<p>This seems to be the preferred way to implement functions for beginning programmers.</p>
<p><strong><code>IN OUT</code> mode:</strong></p>
<p>An <code>IN OUT</code> mode variable is typically a reference to a copy of the actual variable for a couple reasons. If the function or procedure fails the original values are unchanged (this is a departure from the behavior of collections passed as actual parameters in Java). You can assign values to the formal parameter at runtime when using an <code>IN OUT</code> mode variable.</p>
<p>At the conclusion of the function or procedure the internal variable&#8217;s reference is passed to the calling program scope and replaces the original reference to the actual parameter. Here&#8217;s an example of an <code>IN OUT</code> mode variable in a function.</p>
<p style="text-align:center;"><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inout1.png"><img class="aligncenter size-full wp-image-663" title="inout1" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inout1.png" alt="" style="border:none" width="558" height="179" /></a></p>
<p>As you can see the external value is changed inside the function and at completion of the function the external variable passed as an actual parameter is changed:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inoutmodetest.png"><img class="aligncenter size-full wp-image-660" title="inoutmodetest" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inoutmodetest.png" alt="" style="border:none" width="558" height="186" /></a></p>
<p>This seems to be used more frequently in procedures than functions in PL/SQL. However, you can use the approach in either. I&#8217;d recommend it for functions that you call through the OCI or Java.</p>
<p><strong><code>OUT</code> mode:</strong></p>
<p>An <code>OUT</code> mode variable is very much like an <code>IN OUT</code> mode variable with one exception. There is no initial value in it. You must assign a value to an <code>OUT</code> mode variable because it has no value otherwise. If the function or procedure fails, the external variable is unchanged. At the successful conclusion of the function or procedure, the reference for the internal variable replaces the reference to the external scoped variable.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/outonly.png"><img class="aligncenter size-full wp-image-661" title="outonly" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/outonly.png" alt="" style="border:none" width="569" height="202" /></a></p>
<p>The following shows you the test case:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/outmodetest.png"><img class="aligncenter size-full wp-image-662" title="outmodetest" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/outmodetest.png" alt="" style="border:none" width="567" height="194" /></a></p>
<p>The <code>OUT</code> mode also has an exception, which relates to <code>CLOB</code> and <code>BLOB</code> datatypes. You can find more about large objects in this presentation made at the Utah Oracle Users Group &#8211; <a title="An Indepth View of LOBs" href="http://www.utoug.org/pls/htmldb/UTOUG.get_presentation?p_file=94128024696037373" target="_blank">Oracle LOBs</a>.</p>
<p>This should be pretty straightforward but if you have suggestions to improve it let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2008/10/26/quick-review-of-plsql-formal-parameter-modes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
