How to reset the table values in AX 2012? - record

Can I use like this to reset the table variable in AX?
I have tried with the below piece of code:
Table_Name.RecId=0;

You can use the .clear() method that is available on all tables to clear/reset all table fields, including the RecId field.
Table_Name.clear();

Have you considered deleting the record instead?
Table_Name.delete();
The field RecId identifies the record and cannot meaningful be reset.

Clear is to erase field values of te object in order to rehuse the object in a loop. Delete has a direct impact into SQL Database.

Full reset for all data fields and behaviour features:
TableName = null;
see feature example. TableName.clear(); clears only data fields.

Related

How to update a sql record text, by replacing a string for various text?

I have to update a table record where I am replacing some old email ids with a new id.
I am at the moment doing replace like
replace(replace(replace( column, 'abd#xyz.com', 'new#email.com),'old#re.com', 'new#email.com'),'asda#sdfsd.f', 'new#email.com')
Is there any simple way to do it?
I am not sure why a column would contain an email and other information. So perhaps this does what you want:
update t
set column = 'new#email.com'
where column in ('abd#xyz.com', 'old#re.com', 'asda#sdfsd.f');
If the column contains additional information, I would recommend that you fix your data model rather than trying to make a broken data model work.

Best way to modify a DB Table from a work area in ABAP

I want to find out the best solution for modifying a DB table from work area in a loop.
There are several ways to achieve that, first;
LOOP AT itab INTO wa.
wa-flag = 'X'.
MODIFY zblabla FROM wa.
ENDLOOP.
and field symbol;
LOOP AT ITAB ASSIGNING <WA>.
<WA>-flag = 'X'.
ENDLOOP.
Or, should I modify the DBtable from whole internal table ?
modify zblabla from it.
I'm not sure which one is a better approach for less than 50 entries. (I also want to know which one is better with much more entries. )
Thanks.
Your first example (the one with LOOP AT ITAB ASSIGNING <WA>) does not actually change the database. It only changes the data in memory. However, you can afterwards do UPDATE zblabla FROM TABLE itab. which sends the whole table back to the database at once.
When every single line of the table changed, this is far faster than changing every line individually. But when only a few lines of the table are actually different, this is quite wasteful and it will likely be faster to update only the lines which actually changed using MODIFY.
Another option which can sometimes be used to update database content without even loading it into the application server is the UPDATE database_table SET field = value WHERE condition command.
UPDATE zblabla SET flag = 'X'.
Would set the flag to 'X' for every single row in the table right on the database without even having to SELECT anything.
In your example it would be by far the fastest method, but in the real world you rarely have problems which are so trivial.
UPDATE ... SET ... WHERE ... should also be preferred over MODIFY for single entries when the table you have is very wide and you didn't change most of it because it allows you to specify the fields you want to change. You also must use it when you don't got your data through a SELECT * but instead only queried individual fields.
First modify the data in the loop using a field symbol
LOOP AT ITAB ASSIGNING <WA>.
<WA>-flag = 'X'.
ENDLOOP.
i would suggest you to use UPDATE over MODIFY.
UPDATE DBTAB from TABLE ITAB.
If you want to use Modify statement to update the DB.
MODIFY DBTAB from ITAB.
modify a internal table using transporting and where clauses to specify which fields will be altered using the whole key of the table and then using the internal table change the transparent table (database table).
wa_X-field1 = "something". "where wa_X is of the same type of the table e.g. has the same "fields.
wa_X-field2 = "other something".
MODIFY it_tableX FROM wa_X TRANSPORTING field1 field2 WHERE key1 = wa_X-key1 "(first key)
key2 = wa_X-key2. "(last key)
MODIFY tableX FROM TABLE it_tableX.
This is usually better because it increases performance and assures that you only alter the lines of the table that you mean to change.
Hope this helps.

How to deselect values in multivalue lookup field using a query

I want to delete all the data of a single field. This field is a ''multipled value lookup field'';
I tried this query in the SQL design tab:
UPDATE _myDatabase SET [_myDatabase].mycolumn= Null;
but it's not working (although it works for a any non-lookup fields). Any idea how to solve this?
Because you are using a multi value field, you must treat the field as a separate table, which it actually is under the hood.
So you use a DELETE statement:
DELETE _myDatabase.mycolumn.Value FROM _myDatabase

Refreshing values in a TClientDataset after update

I got a TClientDataset that contains data from several tables. When I apply updates on this dataset it might get out of sync.
I'll give you one example:
In the table to update i got an id called "Client_id". The clientdataset also contains a value "Client_name" that is fetched from a "Client" table and displayed in the GUI.
Then I change the "Client_id" in my table and do an apply updates on the table the "Client_name" field in my dataset suddenly is out of sync. This is naturally because the clientdataset has not been refreshed.
Now I could do a clientdataet.refresh on the afterpost event, but then the cursor on the dataset jumps to the first record, and I loose my pointer to the updated record.
Anyone got a clue on how to solve this?
You should give RefreshRecord a try.
set poPropogateChanges for your provider and assign any new field values in AfterUpdateRecord event handler

query a table not in normal 3rd form

Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().