cancel
Showing results for 
Search instead for 
Did you mean: 

SA 12: Incomprehensible error in Stored Procedure ?

2,518

Hi All,

SA 12.0.1.4134

I want to create a procedure with the text:

CREATE PROCEDURE "DBA"."test"(in nID integer)
BEGIN

 insert into dba.TEST(IDENTITY, SUBITEM)
 values(nID, 3), (default, nID);
 commit;

END

When you try to save this process getting an error message:

The procedure 'test (DBA)' could not be modified in the database.
Invalid value for INSERT near 'nID' on line ???
SQLCODE: -1710
SQLSTATE: 42000

Q: What kind of error (in the help, it is not described) and why is it happening ?

VolkerBarth
Contributor
0 Kudos

FWIW, it's not documented for v12.0.1 but for v16 and above, see here. Can't tell why it's happening, to me the INSERT statement looks good. For further tests I would use nID as a connection variable and test the INSERT statement without the surrounding stored procedure...

ThiagoReis
Participant
0 Kudos

Very awkward, I have tested with 2 inserts and it works fine:

CREATE PROCEDURE "DBA"."test"(in nID integer)
BEGIN

 insert into dba.TEST(IDENTITY, SUBITEM)
 values(nID, 3);

 insert into dba.TEST(IDENTITY, SUBITEM)
 values(default, nID);

 commit;

END
ThiagoReis
Participant
0 Kudos

It looks like INSERT of multiple rows only accepts values.

I ran this code below on iSQL and I got the same error:

create variable @i integer;
set @i=1;
insert into dba.TEST(IDENTITY, SUBITEM) values(@i, 1), (@i, 2);

BUG?

Accepted Solutions (0)

Answers (2)

Answers (2)

MarkCulp
Participant

Thiago is correct in his comment above: when there are multiple value tuples in the values clause then only constant values are allowed, so as a result variables are not allowed. I am unsure why there is this restriction. I have sent a note to the doc team to add a note to the VALUES clause section in the INSERT statement documentation.

[Update] After some more scanning of the code I see that host variables are also allowed in the list of values of the value row constructor when there are multiple values specified.

0 Kudos

Syntax

INSERT INTO T ()
VALUES (), (), ();

documented for SA 12.0.1 Look in the help section "INSERT INTO statement, syntax" in the example at the end of description.

Q: So it's all the same mistake that this request does not work with the variable or not ?

VolkerBarth
Contributor
0 Kudos

FWIW, the doc sample with a table with columns all having defaults does work:

create table T_Test(
   col1 int default autoincrement primary key,
   col2 int default 2,
   col3 int default 3);

insert T_Test() values (), (), ();

select * from T_Test;

returns

col1,col2,col3
1,2,3
2,2,3
3,2,3

.

As Mark has pointed out, the restriction (which I would call a bug...) does only apply to multi-row insert and local (or connection-specific) variables, such as

begin
   declare nInt int = 4;
   create variable varInt int = 5;
   insert T_Test values (default, nInt, nInt); -- works
   insert T_Test values (default, varInt, varInt); -- works
   insert T_Test values (default, nInt, nInt), (default, nInt, nInt); -- fails with SQLCODE -1710
   insert T_Test values (default, varInt, varInt), (default, varInt, varInt); -- fails with SQLCODE -1710
end;
VolkerBarth
Contributor
0 Kudos

FWIW, while the restriction seems to hold as Mark has explained, you might use an INSERT SELECT instead of an INSERT with multiple rows if you want to insert multiple rows with variables, such as

begin
   declare nInt int = 6;
   insert T_Test
   select null, nInt, nInt
   union all 
   select null, nInt, nInt;
end;