简体   繁体   中英

How to grab an ID of an existing record from one table and insert it along with other records into another table?

On a summary page, there is an Edit button next to each record.

On this screen, a client asks that we add an Amend Record button next to each record.

Please see screenshot.

在此处输入图像描述

The client does not want existing record modified. Rather, to create an amended version of the original record and insert into another table.

The question is how do we grab the ID of this existing record and insert it along with the newly created data into another table?

The idea is so we can query the two tables and display both existing and amended version of the existing record side by side.

    <label>Existing activities for next year: </label>
    <input id="btnta" type="button" value="Amend" name="btnta" /><br>
    <textarea id="taskActivities" class="longText" name="taskActivities" maxlength="4999"></textarea>
    <br>
    <div id="dvta" style="display: none">
    <label>Amended activities for next year: </label> <br>
    <textarea id="amended_taskActivities" class="longText" name="amended_taskActivities" maxlength="4999"></textarea>
    </div>
    <br>

The attached screenshot shows two textboxes, one at the top is existing record and the empty textbox is for newly amended version of the existing record.

Any ideas how to handle this?

I am not a PHP guy - thank you in advance for your assistance.

I believe you are approaching this the wrong way and have produced an XY Problem .

What you should be doing is just updating the rows in your table as necessary and let the database handle the work for you using row versioning .

Most modern databases support row versioning , it's a ratified part of the SQL standard.

SQL Server implements this using temporal tables . I use this on some chosen tables in various production systems to solve exactly your issue, ie, update rows in tables but keep the previous version(s) of those same rows. This makes troubleshooting, tracking and reporting very easy.

In SQL Server you can implement this as follows:

First, create a dedicated schema for the versioning - I use history as I refer to them as history tables:

create schema history

Next alter your table to add (optionally hidden) start and end dates to track the validity of each row:

alter table dbo.TableName add
    ValidFrom datetime2 generated always as row start hidden constraint DF_TableNameSysStart default sysutcdatetime(),
    ValidTo datetime2 generated always as row end hidden constraint DF_TableNameSysEnd default convert(datetime2, '9999-12-31 23:59:59.9999999'),
period for system_time (ValidFrom,ValidTo);

then enable the automatic system versioning. SQL Server will create a table with the name you specify, here we are giving it the same name under the history schema.

alter table TableName set (system_versioning = on (history_table = history.TableName));

and also define a clustered index

create clustered index [ix_TableName] on [History].TableName ([ValidTo], [ValidFrom]) 
    with (data_compression = page, drop_existing=on) on History;

Finally once in place you can query your main table completely normally to get the current version of each row, or make use of some built-in additional syntax to query a row's previous versions.

select * from TableName for system_time all
where Id=123 
order by ValidTo

This will effectively given you a union of the specified row with its previous version(s)

There's more functionality provided by other commands, or you can query the history table directly.

There is also maintenance to consider (how much history to keep? Keep X previous rows, X months history? etc) and how to control this depends on your version of SQL Server, the newest version provides additional features here.

Read more about system versioned temporal tables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM