HTML

Random musings in IT

Friss topikok

Címkék

21 days (14) addon (3) Alfresco (1) analysis (1) android (6) angel investment (1) angularjs (2) ant (1) architect (2) architecture (2) Atoll (1) awstats (1) axis (1) axis2 (1) beta (1) blog (4) blogging (2) bluetoth keyboard (1) bofh (1) boxee (1) breakfast (1) build (1) cassandra (1) CGI (1) challenge (10) challenge cup (1) chinese (4) chromium (1) CMS (1) compaq contura aero (1) conference (1) consulting (1) continous-integration (2) Dark Data (1) DB2 (5) Debian (1) developer (1) development (1) document outliner (1) driver (1) Eclipse (3) ECM (2) education (1) EJB (1) ejb-client (1) emulator (1) etcd (1) experience (1) facebook (1) female (1) FileNet (1) firefox (2) freeplane (17) freeplaneGTD (17) fun (8) Geronimo (1) getting things done (1) gitlab (1) gnome (1) gtd (15) habit (3) hack (1) hacking (1) hdmi to vga (1) hibernate (1) HR (2) Hungary (1) I18N (1) IBM (1) in (1) Information Lifecycle Governance (1) interview (1) invitel (1) issues (1) It (2) J2ME (1) java (11) javascript (3) java security (1) JAX-WS (1) JBoss (1) JSF (1) kernel (1) Keyboard (1) layout (1) lighttpd (1) Linux (11) LXC (1) macro (1) maven (3) meetup (1) mercury (1) microservice (1) mindweb (5) motorola vip1910-9 (1) movie (1) MQ (1) mw3030r (1) natty (1) nginx (1) node.js (1) nodejs (1) nosql (1) OpenUP (1) Oracle (1) overheat (1) php (1) plugin (1) PrimeFaces (1) project (5) project management (1) RCP (1) recruiter (2) regexp (1) release (11) reporting (1) retrospective (2) rootkit (1) rss (1) RUP (1) script (1) server (1) shared library (1) SharePoint (1) SOA (1) spam (2) spellchecker (1) SQL (1) SSL (1) startup (2) stb (1) story (1) swing (1) SWT (2) tablet (4) tapermonkey (1) tech-beer (1) test (1) thoughts (1) timelapse (1) tomcat (1) toys (1) translate (2) typescript (1) ubuntu (1) ui (1) unified-process (1) University (1) usb (1) VirtualBox (2) virtualization (2) web-service (2) webcam (1) WebSphere (2) wicket (1) widget (1) Windows 8.1 (1) wordpress (3) X11 (1) Címkefelhő

Our rookie had a simple task on the SQL lab of the university he attends. Take a column in table A and copy it to table B, while adding a column id to B from a sequence C.

This seems like a matter of a single relatively simple SQL statement, and so thought their teacher. The solution we all thought to be simple looks like:

INSERT INTO B (ID, COL) VALUES (SELECT C.nextval, COL FROM A)

Nice and easy. This seems that Oracle only evaluates a the sequence operations in a statement once, thus when you take the next value from a sequence you will always get the same value within the statement. This will either result in a constraint violation (if you have one defined) or table A's ID column filled with the same number.

So solutions like:

INSERT INTO B (ID, COL) VALUES (SELECT (SELECT C.nextval FROM DUAL), COL FROM A)

would not work either, as the expression is still evaluated once.

I see two solutions to this really simple problem, either create a trigger on table B for this excercise, which I consider an overkill, or use a LOOP. Oracle states on it's documentation, that the loop content on every iteration.

3 komment

A bejegyzés trackback címe:

https://itworks.blog.hu/api/trackback/id/tr395522331

Kommentek:

A hozzászólások a vonatkozó jogszabályok  értelmében felhasználói tartalomnak minősülnek, értük a szolgáltatás technikai  üzemeltetője semmilyen felelősséget nem vállal, azokat nem ellenőrzi. Kifogás esetén forduljon a blog szerkesztőjéhez. Részletek a  Felhasználási feltételekben és az adatvédelmi tájékoztatóban.

lajos 2007.04.27. 21:45:52

The solution: :D INSERT INTO B SELECT C.nextval, COL FROM A

admin 2007.04.28. 09:03:46

For DB2 it works properly as well. create table a (col char(10)) create table b (id int, col char(10)) create sequence c insert into a values('aa'),('bb'),('cc') select next value for c, a.col from a shows the required 1 aa 2 bb 3 cc and insert into b select next value for c, a.col from a inserts the values in the table correctly.

vonscwahrtz 2007.05.02. 17:22:48

it's funny lajos, I was trying to do this but always used the sytax containing the word "values"... of course it didn't work :-)
süti beállítások módosítása