VishalMistry
8th April 2016, 14:28
Hi all,
i have two tables with similar structures. what i want is when data is inserted into one table, the record should be inserted into another table also.
I know it can be done by assigning field values from source to target table and performing db.insert followed by commit.transaction.
Anybody can guide if can we copy the record directly without individual field assignment ?
I have a little bit idea about record buffers but do not know how to use it practically.
Please guide.
Vishal
mark_h
8th April 2016, 15:55
Not sure this will help - but assuming the records for the two tables are correct. Basically I am writing into the same table. I store off the record buf, modify a few fields and insert. You can do the same thing with different tables. For example I will copy a baan table and then add fields to the bottom. In that case I can do something like rcd.mytable(like tpppc999) = rcd.baantable(like tpppc270). Then I just set the additional fields and do an insert. Of course it is all predicated on the similarity of the two tables. May or may not work for you.
string record.buf(1) based
db.set.to.default(ttpppc270)
rcd.ttpppc270 = record.buf
tpppc270.cprj = tppro603.nprj
tpppc270.sern = get.next.tpppc270.sern(tppro603.nprj)
tpppc270.cspa = tppro603.csp1
tpppc270.quan = total.quan * somemultiplier | Quantity
tpppc270.amoc = total.amoc * somemultiplier | Total Amount
tpppc270.loco = somelogin
db.insert(ttpppc270,db.retry)
commit.transaction()
break
function store.tpppc270.record.buffer()
{
if not record.len then
db.row.length(ttpppc270, record.len)
alloc.mem(record.buf, record.len)
endif
record.buf = rcd.ttpppc270
}
Ajesh
8th April 2016, 19:44
What you can do is
For example if tdsls912 is the table in which the record is created and tdsls812 is the table in which you need to create a similar record. You select tdsls912 and then bind a long id to the recordset, copy it to the default record buffer of tdsls912 and then select tdsls812 for update, copy the record buffer of tdsls912 to tdsls812 and then copy the record buffer to fields.
This entire operation can be performed in the after.save.object section of tdsls912.
PS: I havent tried it but you can give it a try.
function extern long after.save.object(long mode)
{
long tdsls912_id,tdsls812_id,ret.val
string rcd.tdsls912,rcd.tdsls812
select tdsls912.*
from tdsls912
where tdsls912._index1 = {:hold.orno}
selectdo
tdsls912_id = db.bind("ttdsls912")
db.columns.to.record(tdsls912_id)
select tdsls812.*
from tdsls812 for update
where tdsls912._index1 = {:hold.orno}
selectempty
tdsls812_id = db.bind("ttdsls812")
rcd.tdsls812 = rcd.tdsls912
db.record.to.columns(tdsls812_id)
dal.new("tdsls812",ttdsls812,ret.val,db.retry,TRUE)
endselect
endselect
if ret.val = 0 then
commit.transaction()
endif
}
sam291091
9th April 2016, 06:16
Hi Ajesh,
Can you please explain me how value comes in rcd.tdsls912 Variable. As per your code you did not assign value in Variable rcd.tdsls912 and you just direct assign value in rcd.tdsls812( rcd.tdsls812 = rcd.tdsls912 ).
Can you please explain me your code?
Ajesh
9th April 2016, 07:13
Hi Ajesh,
Can you please explain me how value comes in rcd.tdsls912 Variable. As per your code you did not assign value in Variable rcd.tdsls912 and you just direct assign value in rcd.tdsls812( rcd.tdsls812 = rcd.tdsls912 ).
Can you please explain me your code?
rcd.tppmmmccc (i mentioned rcd.ppmmmccc by mistake) is the default string buffer for any Table. You need not mention it in the db.bind. But if you want a different string buffer or a specific string buffer, you need to mention it in db.bind.
db.bind()
Syntax:
function long db.bind (string table_name(9) [, ref string buffer(.)] [, long comp_nr])
Description
This creates a pointer to a specified table. It returns a table ID that you use in other database calls to identify the table. The pointer is to a table with a particular company number and record buffer. You can create additional pointers to the same table by calling the function with a different company number and/or record buffer.
Arguments
string table_name(9) The table name.
[ref string buffer(.)] This optional argument specifies the record buffer to be used for the table. If you omit this argument, or if you specify an empty string, the default record buffer is used (that is, rcd.tppmmmxxx).
[long comp_nr] This optional argument specifies a company number for the table. The default company is the company of the user. If you include this argument, you must also include the buffer argument. So, if you want to specify a company other than the default company but want to use the default record buffer, specify the default buffer or an empty string in the buffer argument.
Return values
0 Error.
>0 Success; pointer is returned.
Context
This function is implemented in the porting set and can be used in all script types.
Note
When you create more than one pointer to a table, you must use a different record buffer for each one.
Example
long tcmcs001_id
if (switch.to.company(200) > 0) then
tcmcs001_id = db.bind("ttcmcs001")
db.first(ttcmcs001_id)
endif
...
db.first(ttcmcs001)
So it should be rcd.ttdsls912 or rcd.ttdsls812 instead of rcd.tdsls912 or rcd.tdsls812.
sam291091
9th April 2016, 07:25
Hi Ajesh,
I got it and thanks for explanation