Home » SQL & PL/SQL » SQL & PL/SQL » Insert inside a function gives null in varchar column
Insert inside a function gives null in varchar column [message #43030] Tue, 03 June 2003 11:23 Go to next message
Mark Riddoch
Messages: 4
Registered: June 2003
Junior Member
I have an interesting problem that I hope somebody out there can help me with. I have a function that inserts a row into a table. The row gets inserted, but one of the columns is always null regardless of the value I try to insert. I have tried inserting fixed values as well as parameters to the function, nothing ever works. I have even tried putting an update in the function after the insert, but this fails also. I know the insert can work because I have another function that inserts rows into the same table with no problems.

I have included the function definition below
FUNCTION ADDPARAMETER (
EVENTID IN NUMBER,
PARVALUE IN VARCHAR2
)
RETURN NUMBER
is
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
DECLARE
LOGID NUMBER;
PARAMNUM NUMBER;
PARAMID NUMBER;
VALUE VARCHAR(255);

BEGIN

-- Get the LOG_ID of the EVNET we are adding parameters to.
SELECT LOG_ID
INTO LOGID
FROM ATHOA.EVENT_EVENTS
WHERE EVENT_ID = EVENTID;

-- Get the EVENT_PARAMETER_ID of the next parameter
SELECT COUNT(*) + 1
INTO PARAMNUM
FROM ATHOA.EVENT_VALUES
WHERE EVENT_ID = EVENTID;

SELECT EVENT_PARAMETER_ID
INTO PARAMID
FROM ATHOA.EVENT_PARAMETERS
WHERE LOG_ID = LOGID AND PARAMETER_ORDER = PARAMNUM;

VALUE:= PARVALUE;
-- Finally insert the parameter
INSERT INTO ATHOA.EVENT_VALUES
(
EVENT_ID,
EVENT_PARAMETER_ID,
EVENT_VALUE
)
VALUES
(
EVENTID,
PARAMID,
VALUE
);
COMMIT;
RETURN PARAMNUM;
END;
END;

And the table definition is....

create table ATHOA.EVENT_PARAMETERS
(
EVENT_PARAMETER_ID NUMBER,
LOG_ID NUMBER NOT NULL,
PARAMETER_ORDER NUMBER NOT NULL,
PARAMETER_NAME VARCHAR(255) NOT NULL,
PARAMETER_MASK NUMBER,
PRIMARY KEY(EVENT_PARAMETER_ID)
);

The function is itself part of a package, however I have other functions in the same package that work.

What seems really odd to me is that the first two columns get inserted correctly.

Hope somebody can help
Thanks
Mark.
Re: Insert inside a function gives null in varchar column [message #43031 is a reply to message #43030] Tue, 03 June 2003 12:40 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Mark, I would suggest simplifying your function to something like:

function addparameter
  (p_event_id    in athoa.event_values.event_id%type,
   p_event_value in athoa.event_values.event_value%type)
  return athoa.event_parameters.parameter_order%type
is
  pragma autonomous_transaction;
 
  v_parameter_order     athoa.event_parameters.parameter_order%type;
  v_event_parameter_id  athoa.event_parameters.event_parameter_id%type;
begin
  if p_event_id is null then
    raise_application_error(-20000, 'p_event_id is null');
  elsif p_event_value is null then
    raise_application_error(-20000, 'p_event_value is null');
  end if;
 
  select event_parameter_id, parameter_order
    into v_event_parameter_id, v_parameter_order
    from athoa.event_parameters
   where log_id = (select log_id
                     from athoa.event_events
                    where event_id = p_event_id)
     and parameter_order = (select count(*) + 1
                              from athoa.event_values
                             where event_id = p_event_id);
   
  insert into athoa.event_values
    (
    event_id,
    event_parameter_id,
    event_value
    )
    values
    (
    p_event_id,
    v_event_parameter_id,
    p_event_value
    );
 
  commit;
 
  return (v_parameter_order);
end;


Since the third value you are inserting is coming directly from the parameter, the only way it can be NULL is if you pass in a NULL value. I also added some typing of variables/parameters.
Re: Insert inside a function gives null in varchar column [message #43037 is a reply to message #43031] Wed, 04 June 2003 03:43 Go to previous messageGo to next message
Mark Riddoch
Messages: 4
Registered: June 2003
Junior Member
Thanks for the suggestion, unfortunately this version also suffers the same problem. The parameters are definately not null, but null is being inserted into the table.

SQL> select athoa.athevent.addparameter(0, 'Testing with new version of addparameter') from dual;

ATHOA.ATHEVENT.ADDPARAMETER2(0,'TESTINGWITHADDPARAMETER2')
----------------------------------------------------------

SQL> select * from athoa.event_values where event_value is null;

EVENT_ID EVENT_PARAMETER_ID
---------- ------------------
EVENT_VALUE
--------------------------------------------------------------------------------

0 1

SQL>

Again the EVENT_ID and EVENT_PARAMETER_ID are correctly inserted, but the EVENT_VALUE is NULL.
Re: Insert inside a function gives null in varchar column [message #43042 is a reply to message #43037] Wed, 04 June 2003 12:19 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Mark, it is impossible for the function version that I posted to insert a NULL value for the second parameter. The validation at the beginning tests for a NULL value and raises an exception that would bypass the insert.

Is there a before-row trigger on this table that might be manipulating the values?
Re: Insert inside a function gives null in varchar column [message #43054 is a reply to message #43042] Thu, 05 June 2003 04:26 Go to previous messageGo to next message
Mark Riddoch
Messages: 4
Registered: June 2003
Junior Member
Todd,

I agree with you that the function should not be able to insert a NULL, but it does. There are no triggers on the table at all. Indeed I can insert non-null values into the table when executing the SQL statements outside of this function or with another function in the same package. It just seems to be this function that has the issue. The main difference here to those that work are the set of selects before the insert happens. In particular the select that finds the next value parameter_order by counting those already added. In fact removing

SELECT COUNT(*) + 1
INTO PARAMNUM
FROM ATHOA.EVENT_VALUES
WHERE EVENT_ID = EVENTID;

from the query and setting PARAMNUM to a fixed value lets me insert non-null values in this function. I have tried changing the count(*) to count particular columns, but that didn't help either.

Thanks for your help

Mark
Re: Insert inside a function gives null in varchar column [message #43056 is a reply to message #43054] Thu, 05 June 2003 11:36 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Mark, let's take a step back here. In the function that I supplied, there is an input parameter p_event_value. At the very beginning of the function, we test for a NULL value for this parameter. If it is NULL, an exception is raised and the insert never happens. The parameter is an IN parameter - it's value cannot be modified within the function - and it is used directly in the insert statement.

There is no way that it is inserting a NULL value. Of course, by "it", I mean the code that I provided - if it has since changed, then all bets are off.

I would really like to work with you here to find out what is happening, but it seems to me that maybe I'm not quite seeing the whole picture here. It might help if you could show the exact current code and a sample SQL session that shows the call to the function and the resultant insert.
Re: Insert inside a function gives null in varchar column [message #43057 is a reply to message #43054] Thu, 05 June 2003 12:05 Go to previous message
Mark Riddoch
Messages: 4
Registered: June 2003
Junior Member
Thanks for the help Todd. In starting from scratch I found what the problem was. There was a side effect from an earlier execution of a stored procedure that added the NULL row.

Sorry if I wasted your time.
Previous Topic: SQL script help required - loading records
Next Topic: How to SELECT data from the table with user-defined type through DATABASE LINK remotely
Goto Forum:
  


Current Time: Mon Aug 04 08:19:41 CDT 2025