<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pl/Sql Scripts</title>
	<atom:link href="http://plsqlscripts.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://plsqlscripts.wordpress.com</link>
	<description>Just another place to pick up some script</description>
	<lastBuildDate>Tue, 30 Dec 2008 16:50:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='plsqlscripts.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Pl/Sql Scripts</title>
		<link>http://plsqlscripts.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://plsqlscripts.wordpress.com/osd.xml" title="Pl/Sql Scripts" />
	<atom:link rel='hub' href='http://plsqlscripts.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Solving Duplicated Keys &#8211; Oracle</title>
		<link>http://plsqlscripts.wordpress.com/2008/12/29/solvingduplicatekeys/</link>
		<comments>http://plsqlscripts.wordpress.com/2008/12/29/solvingduplicatekeys/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 19:22:33 +0000</pubDate>
		<dc:creator>plsqlscripts</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Duplicated Keys]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[When you spend lot of your time working with data. Once in a while you come to a table where you don´t have  any primary key,  any unique index and, of course, you need one right now. In my work I need to convert data from one system to another all the time and most [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=plsqlscripts.wordpress.com&amp;blog=5989815&amp;post=1&amp;subd=plsqlscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When you spend lot of your time working with data. Once in a while you come to a table where you don´t have  any primary key,  any unique index and, of course, you need one right now.</p>
<p>In my work I need to convert data from one system to another all the time and most of these comes from God knows where and you simply don´t understand how come someone could work with a table with no primary key on it and with lot of duplicated rows.</p>
<p>Let´s  transform words into picture.</p>
<p>Create a table and fill in some data with the following script.</p>
<blockquote><p>create table test (<br />
          cdempgrp integer,<br />
          cdfilial integer,<br />
          nrbem integer,<br />
          nrinc integer <br />
);</p></blockquote>
<blockquote><p>insert into test values( 1, 1, 1, null );<br />
insert into test values( 1, 1, 1, null );<br />
insert into test values( 1, 1, 1, null );<br />
insert into test values( 1, 1, 2, null );<br />
insert into test values( 1, 1, 2, null );<br />
insert into test values( 1, 1, 3, null ); <br />
insert into test values( 1, 1, 3, null );<br />
insert into test values( 1, 1, 3, null );<br />
insert into test values( 1, 1, 4, null ); <br />
insert into test values( 1, 1, 4, null );</p></blockquote>
<p> </p>
<p>As you can see there´s lot of duplicated rows. Suppose you need a diferent value for NRINC column for each NRBEM.  In  fact in this case we don´t need a different NRINC for each line.  NRINC is supposed to go from zero to n for each NRBEM.</p>
<p>That´s a problem that you will run over all the time.   And it doesn´t seem to have an easy solution.  Because when you think  you have to make an update you simply don´t know how to write your WHERE clause.</p>
<p>The following script solves this problem.</p>
<p> </p>
<blockquote><p>declare   <br />
        vint   integer;<br />
       vnrbem integer;<br />
begin<br />
       vnrbem := 0;    <br />
      for rec in ( select nrbem from test2 order by nrbem ) <br />
      loop<br />
             if rec.nrbem &lt;&gt; vnrbem then<br />
                  vint := -1;<br />
                  vnrbem := rec.nrbem; <br />
           end if;<br />
           vint := vint + 1;<br />
           update test2 set nrinc = vint <br />
           where nrbem = rec.nrbem and<br />
                       nrinc is null and <br />
                       rownum = 1;<br />
   end loop;</p>
<p>end;</p></blockquote>
<p> </p>
<p>After all. A simple solution for a big problem. Tranks to Oracle.</p>
<p> </p>
<p>That´s all for now.</p>
<p> </p>
<p>Reginaldo</p>
<p> </p>
<p> </p>
<blockquote><p> </p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/plsqlscripts.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/plsqlscripts.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/plsqlscripts.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=plsqlscripts.wordpress.com&amp;blog=5989815&amp;post=1&amp;subd=plsqlscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://plsqlscripts.wordpress.com/2008/12/29/solvingduplicatekeys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f0384e131fe66e143b44c888121ce91?s=96&#38;d=identicon" medium="image">
			<media:title type="html">reginaldorigo</media:title>
		</media:content>
	</item>
	</channel>
</rss>
