Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-17
8
Medium Priority
?
20,488 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

610 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