Search code examples
sqloracle-databasejava-stored-procedures

Java Stored proc giving an error


I am trying to create a stored proc the following way:

DROP procedure IF EXISTS plus1inout
/;
CREATE procedure plus1inout (IN arg int, OUT res int)
BEGIN ATOMIC
    set res = arg + 1;
END
/;

I am getting the following error:

Msg 156, Level 15, State 1.
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1.
Incorrect syntax near the keyword 'IN'. (Line 3)

Solution

  • Your syntax isn't valid in sql server.

    For checking the existence use object_id function

    IF OBJECT_ID('plus1inout', 'P') IS NOT NULL
        DROP PROCEDURE plus1inout
    

    For input parameter you don't have to mention IN keyword. For OUTPUT parameter use the keyword OUT at the end. Also parameters starts with @ in Sql Server

    CREATE PROCEDURE Plus1inout (@arg INT,
                                 @res INT output)
    AS
      BEGIN
          SET @res = @arg + 1;
      END 
    

    In SQL SERVER 2016 they introduced the syntax you have used for checking the existence of a procedure

    DROP procedure IF EXISTS plus1inout