{"id":22092,"date":"2013-05-13T10:35:32","date_gmt":"2013-05-13T05:05:32","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=22092"},"modified":"2024-04-12T14:16:43","modified_gmt":"2024-04-12T08:46:43","slug":"cursor-variables-using-plsql-functions-record-updates-and-shortcuts","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/","title":{"rendered":"Cursor variables, using PL\/SQL functions, record updates and shortcuts"},"content":{"rendered":"\n<p><strong>Cursor variables, using PL\/SQL functions, record updates and shortcuts<\/strong><\/p>\n\n\n\n<p>Cursor variables: In PL\/SQL, a cursor variable is a variable that stores a pointer to a cursor, rather than storing the result set itself. This allows us to pass a cursor between different PL\/SQL blocks or functions, and allows us to write more flexible and modular code. Here&#8217;s an example of declaring and using a cursor variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE\r\n   TYPE emp_cursor IS REF CURSOR;\r\n   emp_cur emp_cursor;\r\n   emp_rec employees%ROWTYPE;\r\nBEGIN\r\n   OPEN emp_cur FOR SELECT * FROM employees;\r\n   LOOP\r\n      FETCH emp_cur INTO emp_rec;\r\n      EXIT WHEN emp_cur%NOTFOUND;\r\n      DBMS_OUTPUT.PUT_LINE(emp_rec.first_name || ' ' || emp_rec.last_name);\r\n   END LOOP;\r\n   CLOSE emp_cur;\r\nEND;\r<\/code><\/pre>\n\n\n\n<p>In the example above, we declared a cursor variable named <code>emp_cur<\/code>, which points to a cursor that selects all the rows from the <code>employees<\/code> table. We then used a loop to fetch each row from the cursor and display the first name and last name of each employee.<\/p>\n\n\n\n<p><strong>Using PL\/SQL functions<\/strong><\/p>\n\n\n\n<p>In PL\/SQL, a function is a block of code that returns a value. Functions can be called from other PL\/SQL blocks or from SQL statements. Here&#8217;s an example of defining and using a function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE OR REPLACE FUNCTION calc_salary (emp_id IN NUMBER) RETURN NUMBER IS\r\n   salary NUMBER;\r\nBEGIN\r\n   SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;\r\n   RETURN salary * 1.1;\r\nEND;\r<\/code><\/pre>\n\n\n\n<p>In the example above, we defined a function named <code>calc_salary<\/code> that takes an employee ID as a parameter and returns the employee&#8217;s salary multiplied by 1.1. We can then call this function from other PL\/SQL blocks or SQL statements.<\/p>\n\n\n\n<p>Record updates: In PL\/SQL, we can update a record in a table using the <code>UPDATE<\/code> statement. Here&#8217;s an example of updating a record in the <code>employees<\/code> table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE employees SET salary = salary * 1.1 WHERE employee_id = 100;\r<\/code><\/pre>\n\n\n\n<p>In the example above, we updated the salary of the employee with ID 100 by multiplying it by 1.1.<\/p>\n\n\n\n<p>Shortcuts in PL\/SQL: PL\/SQL provides several shortcuts to make coding more efficient. Here are a few examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%ROWTYPE<\/code>: This is a shortcut for defining a variable that has the same structure as a table. For example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE\r\n   emp_rec employees%ROWTYPE;\r\nBEGIN\r\n   -- Code to use the emp_rec variable\r\nEND;\r<\/code><\/pre>\n\n\n\n<p>In this example, the <code>emp_rec<\/code> variable is defined with the same structure as the <code>employees<\/code> table.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%TYPE<\/code>: This is a shortcut for defining a variable that has the same data type as a column. For example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">DECLARE\r\n   emp_name employees.first_name%TYPE;\r\nBEGIN\r\n   -- Code to use the emp_name variable\r\nEND;\r<\/pre>\n\n\n\n<p>In this example, the <code>emp_name<\/code> variable is defined with the same data type as the <code>first_name<\/code> column in the <code>employees<\/code> table.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>:=<\/code>: This is a shortcut for assigning a value to a variable. For example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE\r\n   emp_name VARCHAR2(50) := 'John';\r\nBEGIN\r\n   -- Code to use the emp_name variable\r\nEND;\r\n<\/code><\/pre>\n\n\n\n<p>In this example, we assigned the value &#8216;John&#8217; to the <code>emp_name<\/code> variable using the <code>:=<\/code> shortcut.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cursor variables, using PL\/SQL functions, record updates and shortcuts Cursor variables: In PL\/SQL, a cursor variable is a variable that stores a pointer to a cursor, rather than storing the result set itself. This allows us to pass a cursor between different PL\/SQL blocks or functions, and allows us to write more flexible and modular&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[288],"tags":[],"class_list":["post-22092","page","type-page","status-publish","hentry","category-pl-sql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials<\/title>\n<meta name=\"description\" content=\"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials\" \/>\n<meta property=\"og:description\" content=\"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:46:43+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/\",\"name\":\"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-05-13T05:05:32+00:00\",\"dateModified\":\"2024-04-12T08:46:43+00:00\",\"description\":\"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cursor variables, using PL\/SQL functions, record updates and shortcuts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","description":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/","og_locale":"en_US","og_type":"article","og_title":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","og_description":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:46:43+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/","name":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-05-13T05:05:32+00:00","dateModified":"2024-04-12T08:46:43+00:00","description":"Cursor variables, using PL\/SQL functions, record updates and shortcuts tutorials","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cursor-variables-using-plsql-functions-record-updates-and-shortcuts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Cursor variables, using PL\/SQL functions, record updates and shortcuts"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=22092"}],"version-history":[{"count":6,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22092\/revisions"}],"predecessor-version":[{"id":129904,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22092\/revisions\/129904"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=22092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=22092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=22092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}