Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectable store procedure return inserted values - join not working in FB3 [CORE6449] #6682

Closed
firebird-automations opened this issue Nov 18, 2020 · 5 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Tomas Beran (berant)

I have stored procedure where I insert data to table and returning primary key of inserted data.
When I write select * from PROCEDURE P left outer join TABLE T on http://T.PK = http://P.PK then in Firebird 3.0.6 T is null, but in 2.5 T containg inserted data

When I create sql query in the same transaction to TABLE T - data is returned

example:

set term ^;
create table TEST_TABLE (
PKID integer,
TEXT varchar(10),
constraint TEST_TABLE_PKID primary key (PKID))^

create or alter procedure TEST_PROCEDURE (text varchar(10))
returns (PKID integer)
as
begin
select coalesce (max(PKID), 0) + 1
from TEST_TABLE
into :pkid;

insert into TEST_TABLE (PKID, TEXT) values (:pkid, :text);

suspend;
end
^
set term ;^

this query in FB 3.0.6 return null but in 2.5.9 return Hi
select T.TEXT
from TEST_PROCEDURE('Hi') P
left outer join TEST_TABLE T on T.PKID = P.PKID;

@firebird-automations
Copy link
Collaborator Author

Commented by: @aafemt

As designed. Query cannot see its own changes in tables. Otherwise insert-select on the same table would be endless (as it was in version 2.5 which was a bug).

@firebird-automations
Copy link
Collaborator Author

Commented by: @mrotteveel

Your way of generating IDs is not safe under concurrent updates: it will generate identical ids. Instead use an identity column (or a sequence + trigger), together with INSERT ... RETURNING ...

@firebird-automations
Copy link
Collaborator Author

Commented by: @sim1984

This is not a bug, but the correct behavior that can be tested in other DBMSs. The cursor should not see its changes. Earlier versions did not work correctly.

Never use stored procedures with side effects in JOIN queries. Optimizer paths are not confusing, and the result of such queries is not predictable.

@firebird-automations
Copy link
Collaborator Author

Modified by: @AlexPeshkoff

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Won't Fix [ 2 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant