Oracle - return newly inserted key value - stored-procedures

We have a table with a Primary Key that gets populated on insert by a trigger on the table - the trigger gets the next sequence number from a sequence we created for the table and uses that for the value of the key on insert. Now we would like to be able to return that value in our insert procedure (PL\SQL), similar to select ##scope_identity in SQL Server. I have been googling all day and basically come up with nothing - anyone been successful with this before?
Thanks

I don't know if it works with triggers but the RETURNING clause may be what you're looking for:
INSERT INTO my_table (col_1, col_2)
VALUES ('foo', 'bar')
RETURNING pk_id INTO my_variable;

I think you are looking for a Callable Statement. Here's javadoc if you are trying to get to it from Java.

why not just return the_sequence.currval ?

Related

How to handle an autoincremented ID in HANA from a SAPUI5-application?

in my SAPUI5-Application I got the following function that takes the data and creates an entry in my HANA DB:
onCreateNewCustomer: function(){
var oEntry = {};
oEntry.NAME = this.byId("name").getValue();
oEntry.CITY = this.byId("city").getValue();
oEntry.PHONE = this.byId("phone").getValue();
oEntry.ID = this.byId("id").getValue();
// Post data to the server
this.getOwnerComponent().getModel("CustomerModel").create("/Customer", oEntry, null);
this.byId("createCustomer").close();
//location.reload();
}
The creating process works and my entries get saved. In the next step I wanted to implement my table in HANA in that way, that the ID of the entries will be autoincremented so the user does not have to enter it. I used the following command to create my table:
create column table TABLE
(ID bigint not null primary key generated by default as IDENTITY,
FIRSTNAME nvarchar(30))
That worked, table is created. The problem now is, if I use the code above without providing the ID, the following error is logged by the console:
The following problem occurred: HTTP request failed 400,Bad Request,The serialized resource has an missing value for member 'ID'.
The entry does not get saved in my DB. If I execute the following SQL-Statements in my HANA Workbench without providing the ID, it works:
insert into TABLE (FIRSTNAME) values (‘David’);
insert into TABLE (FIRSTNAME) values (‘Mike’);
insert into TABLE (FIRSTNAME) values (‘Bobby’);
So I did some searching on google but did not find a proper solution on how to do this. My goal is that the entry gets saved and the ID is provided by my HANA DB without providing it from my SAPUI5 Application.
Probably you are using ODataV2 XSODATA implementation which does not support auto-increment. The possible solution here is to use a database sequence and then with a separate request get a value from it and use it in OData create statement.
Did you try creating a new record by commenting out the below code like ?
oEntry.ID = this.byId("id").getValue();
With identity fields, if you provide the value you have to explicitly identify that you are providing the column value. Otherwise, just omit the columnn name and value from the INSERT command.

Grails findById( null ) returning "random" result

I found a very strange behavior in our grails application today that i want to share with you.
We are using grails 2.3.11 on mysql 5.1.48.
We had a DomainObject.findById( id ) in one of your Controller actions.
We failed to check the id for a null value so DomainObject.findById( null )
would be called when no id is passed as an argument.
Normally DomainObject.findById( null )
will return null but there is a special condition that will yield other results!
If the controller action called before that inserted a new record in the database (lets call it Object B), regardless of the domain object stored, the DomainObject.findById( null ) will find the DomainObject with the same Id the Object B got on insert.
So when the controller action called before saved anything the findById(null) will return a row. And that row will have the same id the last inserted element got.
I am totally aware that using findById(null) is not the desired way to do it but I was quite shocked about the results it yielded. But returning any seemingly "random" result seems very strange to me.
I also want to note that DomainObject.get(null) will not suffer from this problem.
Anybody else witnessed this?
There is an active Jira pointing in this direction: https://jira.grails.org/browse/GRAILS-9628 but its not really describing this issue.
We don't really support passing null as an argument to a dynamic finder like that. Dynamic finders have explicit support for querying by null. Instead of DomainClass.findByName(null) you would call DomainClass.findByNameIsNull(). If you have a reference that may or may not be null, instead of passing that as an argument to a dynamic finder, the code can almost always be made cleaner by writing a criteria query or a "where" query that has a conditional in it.
I hope that helps.
Thx for your information scot.
I have further details. This behaviour is also altered by the underlying database.
While mysql suffers from this, maria-db (a mysql clone) does not!
So what happens is bound to the underlying database system.
That should not happen to an abstraction layer ....

stored procedure which return fields unless it's used by code via datatableadapter

I have got an issue to retreive stored procedure result ( Table ) into a data table,
when i try to run my procedure from sql server , it's working, same as testing it from a dataset ( I have used a datatableadapter which invoque my stored procedure.
but when i try to get my stored procedure via code, it return an empty datatable.
here is what my query look like (it contain a lot of field so i tried to resume all,
CREATE TABLE #TemporaryTable(Code_Suivi smallint IDENTITY(1,1), DATECREATION date, NOMPROJET varchar(20), ProducedHours decimal(10,1), Accumulation)
insert into #TemporaryTable ( DATECREATION , NOMPROJET , ProducedHours )
SELECT query -- it will insert into #TemporaryTable what I have selected
to avoid getting an empty I have to not work with insert into... which is impossible for me, have any one encountered this befoure??
I have used another way to get it work, by avoiding datatable adapter, and using sqldatasource to invoque a select query

Default value for Yes/No field

Struggling with an extremely easy issue, however, due to the lack of a more specific error message from ADO, I can't figure it out.
I need to add a new column in a table using SQL, the column type is a YESNO, however, I also want to set it's default value to TRUE|YES or whatever the brilliant engineers thought to name it ... this is what I have:
ALTER TABLE TABLENAME
ADD COLUMN VISIBLE YESNO DEFAULT YES; /* the engine complains, 1 is also not ok, true is not ok, what is OK? */
If I remove everything after default the SQL will be executed as expected...
Thank you!
EDIT:
Just in case someone else hits this "wall", here's my final SQL:
ALTER TABLE TABLENAME
ADD COLUMN VISIBLE YESNO -1;
You need to execute against a connection to use Default, eg:
s = "ALTER TABLE TABLE1 ADD COLUMN VISIBLE YESNO DEFAULT true"
CurrentProject.Connection.Execute s
-1 is fine, too. If you want to display a checkbox, you will need VBA.

YII + beforeSave() + getting max value from a table row

i'm struggling to get my beforesave in Yii Framework working this way:
when the user sends the form, beforesave() should fetch the highest number in a column called 'order' and insert a value of order+1 into current 'order' field.
After a few hours spent here reading posts i managed to compile this thing:
public function beforeSave()
{
if (parent::beforeSave())
{
if($this->isNewRecord)
{
$criteria->select='max(order) as myMaxOrder';
$get_it= new CActiveDataProvider(get_class($this),
array('criteria'=>$criteria,));
$got_it=$get_it->getData();
$whatweneed=$got_it[0]['myMaxOrder'];
$this->order=(int)$whatweneed+1;
}
return true;
}
else
return false;
}
The code gets the MAX from 'order' but i really did't know how to deal properly with the YII's getData() method, so i var_dumped it and saw that what i was looking was there but i still don't know how to access this value apart from doing
$whatweneed=$got_it[0]['myMaxOrder'];
Could you tell me how to do it right?
If you set up your database so that the Order id is the Primary Key (it should already be anyway), just set it to "Auto Increment". If Auto Increment is set on your Primary Key (id), then when you save() a Model in Yii without an id it will automatically save it with an id one higher than the max. You won't need to do anything in beforeSave() at all! It is free functionality.
But perhaps I am totally misunderstanding your question. Maybe this is not an auto-incrementing primary key column for some reason. In that case, something like this should work (assuming your model is Order and you column is also, for some reason, called "order"):
$maxOrderNumber = Yii::app()->db->createCommand()
->select('max(order) as max')
->from('order')
->queryScalar();
$this->order = $maxOrderNumber + 1;
Good luck!

Resources