views:

1367

answers:

3

I'm new to oracle and having a problem with one of my SQL Queries.

There are 2 Users: User1 and User2:

     Tab1               Tab2
    --------            -------- 

    EmpNo               EmpNo
    EmpName             EmpName
    ContactNo           Salary
    Location    

User2 has all privileges in User1.Tab1, and there is no foreign key relationship between the two tables.

The Problem:

I wanted to add a column in tab2 "NameDesignation" And I wanted to insert the value in this column after checking the following condition:

WHEN User1.Tab1.EmpNo = User2.Tab2.EmpNo THEN
   INSERT INTO Tab2 VALUES (&designation)

I really have no idea how to do this, and was hoping for a little help. Any thoughts?

+12  A: 

try this:

update user2.tab2.empno t2
set NameDesignation= &designation
where exists (select ''
              from user1.tab1 t1
              where t1.empno=t2.empno)

(statement updated to match the edited question)

Edwin
+30  A: 
UPDAT UZRTOO.TABTOO.IMPKNOW TEATOO
SETT DEZIGNASHUN IZ EQL DOTZ
WHAR IS HVING (SLEKT NUTHN
                FRUM UZRWON.TABWON TEAWON
                WAR IS HVING TEAWON.IMPKNOW IZ EQL TEATOO.IMPKNOW)

Update

A word of explanation; the original phrasing of the question was full of text-message-style abbreviations, and included a LOLCAT photo. So, in good fun, it seemed OK to translate the SQL to SQLOLCODE. No offense intended to anyone.

joel.neely
+1 for translation
iAn
of course this makes no sense now that the original question has been edited. To see why this is here check revisions of the Q: http://stackoverflow.com/revisions/585090/list
Jeff Atwood
@Jeff Atwood: Even as much as I struggled to let the question stand, if its owner wanted help, it needed to be edited. I still upvoted the answer, though. :-D
George Stocker
@Gortok: Fine job pulling this question from the grave. Now where can I vote on your work... ;-)
Jon Ericson
hey, that /list page is pretty spiffy... lots quicker than paging through the edits. Nice!
Mark Harrison
A: 

You would need a set of triggers,

After insert or update:

CREATE OR REPLACE TRIGGER tab1_after_changed
AFTER INSERT OR UPDATE
    ON tab1
    FOR EACH ROW

BEGIN
    DELETE FROM User2.Tab2 WHERE EmpNo=:NEW.EmpNo;
    INSERT INTO User2.Tab2(EmpNo,EmpName,NameDesignation) 
        VALUES (:NEW.EmpNo,:NEW.EmpName, (SELECT DesignationName FROM Designation where DesignationID=:NEW.DesignationID));
END;

I just imagined a table with Designation (DesignationID number, DesignationName varchar2(xx)), and Tab1 having DesignationID(number).

Osama ALASSIRY