Wondering if there is a way to insert a row into a table from another, with exception of one column?
This is of course easy with a limitied amount of columns, but gets k
using Oracle
DECLARE
CURSOR CUR_D IS
SELECT *
FROM TABLE
WHERE id = some id;
ROW_D CUR_D%ROWTYPE;
BEGIN
OPEN CUR_D;
FETCH CUR_D INTO ROW_D;
CLOSE CUR_D;
ROW_D.column := 'some data';
INSERT INTO some table
VALUES ROW_D;
END;
/
zim RDBMS will allow it. All fields matching by fieldname will obtain values from the source, unless overridden by a let statement. eg.
`add all details from details where account_month = 20190101 let level = 2 increase = base * 1.1 total = increase * amount`
This would for all detail records for the specified accounting month, add a level 2 record with all field values matching the source set, except these assigned/calculated amounts: level is 2. increase is original base times 1.1. total is new increase (original base times 1.1) times the amount. If you switch the order of assignment of increase and total, then total will use the source/original increase in the calculation.
You can use temporary tables
CREATE TEMPORARY TABLE temp_table AS (SELECT * FROM MyTable WHERE ...);
UPDATE temp_table SET column='Value' WHERE ...;
ALTER TABLE temp_table DROP column_name;
INSERT INTO MyDestinationTable SELECT * FROM temp_table;
For SQL Server, the syntax would be:
insert into TargetTable
(col1, col2, col3, ...)
select getdate(), col2, col3, ...
from SourceTable
SQL Server can generate a column list for you. Right click the table name, and choose Script as -> Select -> To New Window
.
assuming that your select has attributes uniquely defining the result set wrt to the contents of your target table before insertion, you can apply the following 2 steps:
Insert into target_table
select *
from source_table
where yada yada yada
and characteristic_yada
;
update target_table
set col1 = current date
where characteristic_yada
;
commit;
make sure to issue both commands inside the same transaction as shown. also be aware that characteristic_yada
must be aplicable to source and target table alike and that the suitability of characteristic_yada
needs to be checked before each application of the statements unless they refer to pks/aks of the taregt table
Create a VIEW with required number for columns.
Assume Tbl1 has 4 columns. Create a view with required columns. Here Col1 has been excluded.
CREATE VIEW V1 AS
SELECT col2, col3, col4
FROM TBL1
Use the VIEW for Inserting. Col1 value for TBL2 will be current date, for other columns, the values will be from view V1 ie., col2, col3, col4 from TBL1
INSERT INTO TBL2
SELECT GETDATE(), *
FROM V1
This way you dont need to specify all the columns each time.
Hope it helps