?
Solved

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

Posted on 2004-10-17
8
Medium Priority
?
20,489 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
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
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 150 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 300 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 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