Solved

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

Posted on 2004-10-17
8
20,476 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now