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

Getting computed value after insert/update/delete. #181

Closed
richardpauljones opened this issue Mar 27, 2019 · 6 comments
Closed

Getting computed value after insert/update/delete. #181

richardpauljones opened this issue Mar 27, 2019 · 6 comments
Assignees
Milestone

Comments

@richardpauljones
Copy link

richardpauljones commented Mar 27, 2019

SQL Server seems to have the following Syntax for returning the computed values after an insert.

INSERT INTO “tableInsertOSX” (“a”, “b”)
OUTPUT INSERTED.a,INSERTED.b <-------- need this to return values from an insert.
VALUES (‘apricot’, ‘3’)

Suggest adding a new property to kquery.insert .prevalues

UPDATE "tableInsert2OSX"
set b='3'
OUTPUT INSERTED.b <----- need this to return values from update.
WHERE a = 'apricot'

Suggest adding a new property to kquery.update .prewhere

DELETE FROM "tableUpdateOSX"
OUTPUT DELETED.b <----- need this to return valued from a a delete
WHERE "tableUpdateOSX"."b" = '2'

Suggest adding a new property to kquery.delete .prewhere

This should follow the same sort of structure as suffix.

@richardpauljones richardpauljones changed the title Getting computed value after insert. Getting computed value after insert/update/delete. Mar 28, 2019
@kilnerm
Copy link
Contributor

kilnerm commented Apr 24, 2019

@richardpauljones, Sorry it's taken so long to get back to you on this.

Can you help me understand what values are being returned in these cases? I am trying to work out whether there is similar function in the existing plugins.

@richardpauljones
Copy link
Author

richardpauljones commented Apr 24, 2019

So the normal behaviour of SQL server for an insert/update/delete is just to return the number of rows affected. This is different in PostgreSQL, where the default behaviour is to return the record you have just inserted/updated/deleted.

To get the same behaviour you need to added the OUTPUT statement as I have shown in my examples above.

I found this issue while trying to run the PostgreSQL test project against my experimental ODBC database driver. The test that breaks is func testInsertID() {

@kilnerm
Copy link
Contributor

kilnerm commented Apr 24, 2019

I believe the default behavior in PostgreSQL is to just return the number of rows affected. In the case where you are doing an insert and have the returnID property set to true then the buildQuery function in PostgreSQLConnection will add a suffix to the built query to indicate we want the id column returning.

If the positioning of the OUTPUT clause is not important I would suggest mirroring this approach in your plugin.

If the positioning is important then it becomes a little more difficult. You could either patch the clause into the built query or perhaps implement a build method that accepts the query instance and generates the desired output.

@richardpauljones
Copy link
Author

Thanks for that.

Yes the positioning of the Output statement is important.

@kilnerm
Copy link
Contributor

kilnerm commented Jul 3, 2019

@richardpauljones Do you require any further assistance on this issue?

@kilnerm kilnerm self-assigned this Jul 3, 2019
@kilnerm kilnerm added this to the 2019.14 milestone Jul 3, 2019
@kilnerm
Copy link
Contributor

kilnerm commented Jul 16, 2019

I am going to mark this issue as closed. If you require anything further please reopen.

@kilnerm kilnerm closed this as completed Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants