Solved

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

Posted on 2004-10-17
8
20,480 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

919 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

13 Experts available now in Live!

Get 1:1 Help Now