{"id":828,"date":"2012-08-11T18:47:27","date_gmt":"2012-08-11T23:47:27","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=828"},"modified":"2012-08-12T19:39:26","modified_gmt":"2012-08-13T00:39:26","slug":"oracle-stored-procedures-and-coldfusion","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2012\/08\/oracle-stored-procedures-and-coldfusion\/","title":{"rendered":"Oracle stored procedures and ColdFusion"},"content":{"rendered":"<p>I&#8217;ve heard for years that using Oracle&#8217;s stored procedures is both more efficient and more secure than writing queries against the database. It turns out, not everyone agrees with that and there is quite a bit of room for debate (most of the articles focus on MS-SQL Server and T-SQL, not Oracle and PL\/SQL, although some general principles still apply):<\/p>\n<ul>\n<li><a href=\"http:\/\/www.dba-oracle.com\/art_ioug_proc.htm\">Faster Performance using Oracle Stored Procedures<\/a><\/li>\n<li><a href=\"http:\/\/www.codinghorror.com\/blog\/2005\/05\/stored-procedures-vs-ad-hoc-sql.html\">Stored Procedures vs. Ad-Hoc SQL<\/a><\/li>\n<li><a href=\"http:\/\/www.simple-talk.com\/sql\/t-sql-programming\/to-sp-or-not-to-sp-in-sql-server\/\">To SP or not to SP in SQL Server<\/a><\/li>\n<li><a href=\"http:\/\/weblogs.asp.net\/fbouma\/archive\/2003\/11\/18\/38178.aspx\">Stored procedures are bad, m&#8217;kay?<\/a>\n<\/ul>\n<p>I was still interested in <em>how<\/em> to use Oracle stored procedures with ColdFusion, though.<br \/>\n<!--more--><br \/>\nFirst I created a simple table and populated it with some data:<\/p>\n<table border=\"1\">\n<thead>\n<tr>\n<th>id<\/id><\/p>\n<th>name<\/th>\n<\/tr>\n<\/thead>\n<tfoot><\/tfoot>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Chris<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Ben<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Dave<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Then I added a stored procedure to Oracle:<\/p>\n<pre><code>CREATE OR REPLACE PROCEDURE testselect \r\n    (recordset OUT SYS_REFCURSOR) IS\r\nBEGIN\r\n    OPEN recordset FOR\r\n        SELECT id,name\r\n        FROM myschema.name_table;\r\nEND;\r\n\/<\/code><\/pre>\n<p>It took me a while to get the stored procedure to compile, but with some Google-sleuthing I eventually cobbled together something that worked.<\/p>\n<p>Now, how to get the data out via ColdFusion? First I added the proper data source to the CF administrator. Then I put the following code in a CFM page:<\/p>\n<pre><code>&lt;cfstoredproc datasource=\"sptest\" procedure=\"testselect\"&gt;\r\n    &lt;cfprocresult name=\"data\"\/&gt;\r\n&lt;\/cfstoredproc&gt;\r\n&lt;cfdump var=\"#data#\"&gt;<\/code><\/pre>\n<p><strong><em>Voila!<\/em><\/strong> It worked&#8211;3 rows returned.<\/p>\n<p>But what if I wanted to select a subset of rows (or otherwise include a parameter in the query)? First I needed to update the stored procedure to accept a parameter:<\/p>\n<pre><code>CREATE OR REPLACE PROCEDURE myschema.testselect \r\n    (p_name IN varchar2, recordset OUT SYS_REFCURSOR) IS\r\nBEGIN\r\n    OPEN recordset FOR\r\n        SELECT id,name\r\n        FROM myschema.test_table\r\n        WHERE name = p_name;\r\nEND;\r\n\/<\/code><\/pre>\n<p>Next, I needed to include a parameter in my stored procedure call:<\/p>\n<pre><code>&lt;cfstoredproc datasource=\"sptest\" procedure=\"testselect\"&gt;\r\n     &lt;cfprocresult name=\"data\"\/&gt;\r\n     &lt;cfprocparam value=\"Chris\" dbvarname=\"p_name\" cfsqltype=\"cf_sql_varchar\" maxlength=\"50\"\/&gt;\r\n&lt;\/cfstoredproc&gt;\r\n&lt;cfdump var=\"#data#\"&gt;<\/code><\/pre>\n<p><strong><em>Voila!<\/em><\/strong> Only a single row returned.<\/p>\n<p>Now that I&#8217;ve figured out <em>how<\/em> to use them, I just need to consider the many arguments for and against using stored procedures and decide whether or not I <em>should<\/em> use them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve heard for years that using Oracle&#8217;s stored procedures is both more efficient and more secure than writing queries against the database. It turns out, not everyone agrees with that and there is quite a bit of room for debate (most of the articles focus on MS-SQL Server and T-SQL, not Oracle and PL\/SQL, although &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2012\/08\/oracle-stored-procedures-and-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Oracle stored procedures and ColdFusion<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[349,278,274,275],"class_list":["post-828","post","type-post","status-publish","format-standard","hentry","category-coldfusion","tag-coldfusion","tag-database","tag-oracle","tag-stored-procedures"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=828"}],"version-history":[{"count":10,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/828\/revisions"}],"predecessor-version":[{"id":843,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/828\/revisions\/843"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}