Solved

how to use an IF statement in oracle's INSERT syntax like mssql server?

Posted on 2004-10-17
8
20,484 Views
Last Modified: 2011-08-18
hi,
I am using mssql with oledb,
I have this sql text like:

;===========================================
IF (select count(*) from table_user where userid=1700))=0
INSERT INTO table_user (userid,username)
VALUES (1700,'azsd')
;===========================================

now our database moved to oracle 9i,
I use the "if" like this,sure its only occur an oledberror,nothing inserted.

;============================================
IF NOT EXISTS(select userid from table_user where userid=1700) THEN
INSERT INTO table_user (userid,username)
VALUES (1700,'azsd')
END IF
;============================================

I have another idea plan to use when state ments to insert the exists user into an other temp table but i think my methods were so ugly.

how can I do an check before insert in one query?

thx.
0
Comment
Question by:azsd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:RaisinJ
ID: 12332805
Decalre another variable...

@User_ID    Table_User.UserID%Type;

SELECT NVL(UserID,0)
INTO @User_ID
FROM Table_User
WHERE User_ID = 1700

IF @User_ID = 0
    INSERT INTO Table_User (UserID, UserName)
    VALUES (1700,'@YourUserName');
END IF;
0
 

Author Comment

by:azsd
ID: 12332862
thx your reply

I only can using query command,store procedure/function not suitable for my programing library....
it seems kololedb don't have that feather....

seems your solution are only suite in procedure,in sql plus it said:

@User_ID    table_user.UserID%Type;
*
Line 1 occured error:
ORA-00900: invalied SQL statements....

0
 
LVL 15

Expert Comment

by:ishando
ID: 12335098
The @ symbols is used in MS SQL Server to identify variables.
In Oracle the @ symbol is used as a short hand for the 'start' command which executes script files.

Try the above solution without the @ symbols.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:RaisinJ
ID: 12335105
Sorry, in your variable declaration, you cannot use the "@" symbol.  If you did, try doing it without the "@" symbol.
0
 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 50 total points
ID: 12336681
Oracle does not have an insert unless command natively, so you will need to use some form of PL/SQL code to do this.  Going by your comment that you cannot use stored procedures/functions, I would suggest an anonymous PL/SQL block.

The way it would be implemented would be dependent on what would be the normal execution of the code.  If you can put a unique constraint/index on the userid column (going by what you are trying to do I think this should be there anyway), then simply doing the insert and handling a duplicate value on index exception would be simply:

DECLARE
    p_userid   TABLE_USER.userid%TYPE   := 17;
    p_username TABLE_USER.username%TYPE := 'me';
BEGIN
    INSERT INTO TABLE_USER (userid, username)
    VALUES (p_userid, p_username);
EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
        -- ignore...
        NULL;
END;

However, the other option would be to do similar to what was provided previously, however, with the syntax correction, and the removal of the NVL (which would cause it to never work):

DECLARE
    p_userid   TABLE_USER.userid%TYPE   := 18;
    p_username TABLE_USER.username%TYPE := 'me';
    l_tmp NUMBER;
BEGIN
    SELECT COUNT(*)
    INTO   l_tmp
    FROM   TABLE_USER
    WHERE  userid = p_userid;

    IF (l_tmp = 0) THEN
        INSERT INTO TABLE_USER (userid,username)
        VALUES (p_userid, p_username);
    END IF;
END;
0
 
LVL 6

Accepted Solution

by:
izblank earned 100 total points
ID: 12340938
MERGE INTO TABLE_USER T
   USING (SELECT CAST(1700 as NUMBER) userid, CAST('azsd' AS VARCHAR2(200)) username
           FROM dual) S
   ON (T.userid = S.userid)
   WHEN MATCHED THEN UPDATE SET T.username=t.username
   WHEN NOT MATCHED THEN INSERT (T.userid,T.username)
   VALUES (S.userid, S.username);
0
 

Author Comment

by:azsd
ID: 12345500
yes,by little modify,these two nice script works!
0
 

Author Comment

by:azsd
ID: 12345514
thx all yours,
I am going to modify my codes now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question