Why I get error message when transfer INSERT method to Function

don_julian

I get error message which a little bit confusing me and have no idea what I made wrong here

FUNCTION ADD_TOKEN(p_userID IN INT,p_token IN VARCHAR2,p_type IN VARCHAR2 )
    RETURN SYS_REFCURSOR IS 
    rc  SYS_REFCURSOR;
    /*TokenExists*/
BEGIN
  INSERT INTO user_notifications_token (userId, token, type) 
    VALUES (p_userID, p_token , p_type) ON DUPLICATE KEY 
       UPDATE token = p_token, created_date = SYSTIMESTAMP;
END ADD_TOKEN;

I get error message

Error(51,3): PL/SQL: SQL Statement ignored

I put this function inside PACKAGE where I store all my function (arround 50 functions) Does anyone know what is wrong here ? Where did I made mistake ?

MT0

Oracle does not support the SQL Server syntax INSERT ... ON DUPLICATE KEY UPDATE .... Use a MERGE statement instead.

FUNCTION ADD_TOKEN(
  p_userID IN user_notifications_token.userID%TYPE,
  p_token  IN user_notifications_token.token%TYPE,
  p_type   IN user_notifications_token.type%TYPE
) RETURN SYS_REFCURSOR
IS 
  rc  SYS_REFCURSOR;
BEGIN
  MERGE INTO user_notifications_token dst
  USING (
    SELECT p_userID AS userID,
           p_token AS token,
           p_type AS type
    FROM   DUAL
  ) src
  ON ( src.userId = dst.userID )
  WHEN MATCHED THEN
    UPDATE SET token = src.token, type = src.type
  WHEN NOT MATCHED THEN
    INSERT ( userId, token, type ) VALUES ( src.userId, src.token, src.type );

  -- Do stuff
  
  RETURN rc;
END ADD_TOKEN;

Then if you do:

DECLARE
  rc SYS_REFCURSOR;
BEGIN
  rc := package_name.ADD_TOKEN( 1, 'ABC', 'A' );
  rc := package_name.ADD_TOKEN( 1, 'DEF', 'D' );
END;
/

The table will contain:

USERID | TOKEN | TYPE
-----: | :---- | :---
     1 | DEF   | D   

db<>fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why I get error message that onVideoSelect is not a function?

Why do I get an error message when using an observeEvent with this function that works fine when not wrapped in an observer?

Why do I get an error message of NoneType object is not callable when I try to call this function?

Why do I get the error message "undefined is not a function" when trying to cancel a promise on press?

Why i get error when want insert to table in sql server?

Why do I get an error when create a function but not when a variable?

Why do i get an an error on paint method when the parameters are correct?

Why do I get a compile error when calling start method?

Flutter: Why do I get an error when using the setState() method?

Why do I get error 424 when calling a function?

Why do I get a syntax error when creating a PostgreSQL function?

Why I get an error when onClick function is activated?

Why am I getting this error message when I am using NSTimer to run function every x seconds?

Why do I get syntax error for "INSERT INTO"?

Why do I get a message saying "Object is not a function" when I try to do an apply?

Why do I get an undefined local variable or method error when using a constant, but not when using a method?

Why does audio.buffered.end(0) get an error message when I try to get buffered time

Python/Terminal - Why am I getting this error message when I try to get user input

I get this error when I insert a new post in the db "Call to a member function load() on null"

Why when passing a method as argument to another method do I get 'Function name must be a string'

Why do I get error message Cast to ObjectId failed for value \"[Function: ObjectId]"?

Why do I get an error when running a method from a jsp page, but not from a main method?

Why do I get an error message when trying to assign a rect to a surface?

Why do I get an error message when installing Microsoft.EntityFrameworkCore through NuGet

Why do I get an error message plotting indicating infinite values when there are none?

Why do I get an error message pointing to Inf values when trying to plot counts over time in R?

Why I get ERROR_INVALID_FUNCTION ( winapi error 1 ), when I execute GetFileSizeEx?

Why I get 'Resource leak: '<unassigned Closeable value>' ' error even when I use close() method in Java?

Why do I get a black screen when I insert a subview?