سؤال

I am playing with db2 stored procedures and I am struggling in understading the following concept. When I create a stored procedure like the simple below

create or replace procedure test()
begin
insert into mytable(a) values ('a');
insert into mytable(a) values ('b');
insert into mytable(a) values ('c');
end

I can see that the mytable gets filled during this creation. I was expecting to use 'call test()' to insert my data, but I can see it is not what I thought. What I am doing wrong here or it does work like this?

To avoid this odd behavior I found that I need to put everything on the same line and I have what I expect i.e. no data inserted when I call any "create or replace procedure"

create or replace procedure test() begin insert into mytable(a) values ('a'); insert  into mytable(a) values ('b');  insert into mytable(a) values ('c');     end
هل كانت مفيدة؟

المحلول

The problem is the termination character for each instruction.

For example, if you have a script file called test.sql with inserts and a stored procedure definition, then your defintion will be:

test.sql

insert into mytable (a) values ('a')@
create procedure test ()
  begin
    insert into mytable (a) values ('b');
  end@

And you will execute the script via this command

 db2 -td@ -f test.sql

The @ sign indicates the termination character (option -td@ to indicate the Termination character Delimiter). Note that this is different that ; (semi colon) because the ; is used as termination character inside a routine (stored procedure, udf, trigger).

As you can see, the 'a' value will be inserted in the first insert. The 'b' value will be inserted from the stored procedure.

In order o be sure what you inserted, ou can query the DB2 catalog in order to see the defined stored procedures, and the 'bodies'.

نصائح أخرى

You are using the -t option, which specifies that ; is the statement terminator.

You can't use a semicolon as the statement terminator when writing procedures or other blocks of code, because the whole procedure declaration needs to be considered as a single statement. DB2 is breaking it up into multiple statements based on semicolons.

Set the statement terminator to something else using the -tdX option. I use ! as my standard statement terminator in DB2, and it works well for me (it is not used in DB2 syntax except for !=, which can be replaced by <>).

Making this change should fix your problem.

I am surprised that putting it on a single line made it work, though, as I wouldn't expect that to make any difference (there is an option to automatically treat lines as statement terminators, but you don't have it on).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top