archive one table date in another table with archive date in Oracle - sql

i have one table test it has 10 column with 20 rows.
I need to move this data to archive_test table which has 11 column (10 same as test table plus one column is archive date).
when i tried to insert like below its shows error because number of column mismatch.
insert into archive_test
select * from test;
Please suggest the better way to do this.Thanks!

Well, obviously you need to supply values for all the columns, and although you can avoid doing so you should also explicitly state whic value is going to be inserted into which column. If you have an extra column in the target table you either:
Do not mention it
Specify a default value as part of its column definition in the table
Have a trigger to populate it
Specify a value for that column.
eg.
insert into table archive_test (col1, col2, col3 ... col11)
select col1,
col2,
col3,
...
sysdate
from test;

assuming that archive_date is the last column:
INSERT INTO archive_test
SELECT test.*, sysdate
FROM test

Related

How to delete all columns except first five?

I have a table with approximately 90 columns and want to delete all after the 5th. How to delete all columns except first five?
Given that you only want to keep a few columns, the option with least code would be to make a new table with those columns only. You should do this in a transaction to avoid losing data.
Example with two columns:
ALTER TABLE TableName RENAME TO TmpTableName;
CREATE TABLE TableName(Col1 INTEGER, Col2 INTEGER);
INSERT INTO TableName(Col1, Col2) SELECT Col1, Col2 FROM TmpTableName;
DROP TABLE TmpTableName;
Before version 3.35.0, SQLite did not support removing columns, so this was the only possible option.

SQL Insert existing/duplicate row into table but change only one column value?

I have Audit table with more than 50 columns and need to insert desired row (duplicate) by changing just one column value (column CreatedDate, set value to GETDATE()). I know this can be achieved by INSERT INTO SELECT * FROM but as there is more han 50 columns, code would seem messy:
INSERT INTO Audit_Table (Col1, Col2, .....CreatedDate, ......, Col50, ...ColN)
SELECT (Col1, Col2, .....GETDATE(), ......, Col50, ...ColN) FROM Audit_Table
WHERE Audit_Table.Id = Desired_Id
If i shouldn't change CreatedDate column value, it would be very simple:
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
Is there any other way to duplicate row and change only one/desired column value?
You can insert the record into a temporary table, update the CreatedDate column to GETDATE(), then insert it into the Audit_Table.
No. There is no way to say * except column_foo in SQL.
The workaround would be to generate the
SELECT
col1
, col2
, [...]
, coln
FROM foo;
statement (or parts of it) by querying the database's system catalogue for the column names in their order. There is always a table with all tables and a table with all columns.
Then, make sure you put the necessary commas in the right place (or remove them where you don't need them, or generate the comma in all rows of the report but the first - by using the ROW_NUMBER() OLAP function and evaluating whether it returns 1 or something else). Finally, edit the right date column, by replacing it with CURRENT_DATE or whatever your database uses for the current day.
Good luck -
Marco
You can build upon your existing idea. Just duplicate the row (I assume, you have an auto-incrementing primary key column) and then in a separate query update the time i.e.
Do this :
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
And then :
UPDATE Audit_Table SET CreatedDate = GETDATE() WHERE primaryKeyID = newPrimaryKeyID
Hope this helps!!!
try below as reference
you can use below statement to copy the all rows,
mysql> insert into newstudent select * from students;
you can use below statement to copy the specific row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248;
you can use below statement to copy the either of the row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248 or id=1249;
use limit clause also along with this

How to select data and insert those data using single sql?

I want to select some data using simple sql and insert those data into another table. Both table are same. Data types and column names all are same. Simply those are temporary table of masters table. Using single sql I want to insert those data into another table and in the where condition I check E_ID=? checking part. My another problem is sometime there may be any matching rows in the table. In that time is it may be out sql exception? Another problem is it may be multiple matching rows. That means one E_ID may have multiple rows. As a example in my attachment_master and attachments_temp table has multiple rows for one single ID. How do I solve those problems? I have another problem. My master table data can insert temp table using following code. But I want to change only one column and others are same data. Because I want to change temp table status column.
insert into dates_temp_table SELECT * FROM master_dates_table where e_id=?;
In here all data insert into my dates_temp_table. But I want to add all column data and change only dates_temp_table status column as "Modified". How should I change this code?
You could try this:
insert into table1 ( col1, col2, col3,.... )
SELECT col1, col2, col3, ....
FROM table2 where (you can check any condition here on table1 or table2 or mixed)
For more info have a look here and this similar question
Hope it may help you.
EDit : If I understand your requirement properly then this may be a helpful solution for you:
insert into table1 ( col-1, col-2, col-3,...., col-n, <Your modification col name here> )
SELECT col-1, col-2, col-3,...., col-n, 'modified'
FROM table2 where table1.e_id=<your id value here>
As per your comment in above other answer:
"I send my E_ID. I don't want to matching and get. I send my E_ID and
if that ID available I insert those data into my temp table and change
temp table status as 'Modified' and otherwise don't do anything."
As according to your above statements, If given e_id is there it will copy all the columns values to your table1 and will place a value 'modified' in the 'status' column of your table1
For more info look here
You can use merge statement if I understand your requirement correctly.
Documentation
As I do not have your table structure below is based on assumption, see whether this cater your requirement. I am assuming that e_id is primary key or change as per your table design.
MERGE INTO dates_temp_table trgt
USING (SELECT * FROM master_dates_table WHERE e_id=100) src
ON (trgt.prm_key = src.prm_key)
WHEN NOT MATCHED
THEN
INSERT (trgt.col, trgt.col2, trgt.status)
VALUES (src.col, src.col2, 'Modified');
More information and examples here
insert into tablename( column1, column2, column3,column4 ) SELECT column1,
column2, column3,column4 from anothertablename where tablename.ID=anothertablename.ID
IF multiple values are there then it will return the last result..If not you have narrow your search..

Delete Trigger getdate() returns year 1906

I want to set a date/time stamp on deleted records but I'm getting strange results. When this trigger fires it sets the date to 1906!
I've tried using the GETDATE() function in the query with an UPDATE also on the table properties with a default value. It works fine for records I input manually into the DELETED_RECORDS table but any record that's moved in caused by an actual deletion has a 1906 date. I'm not sure what's going on
ALTER TRIGGER [dbo].[Backup_TT_Deleted_Records]
ON [dbo].[tblLLS_TT]
FOR DELETE
AS
BEGIN
INSERT INTO dbo.tblLLS_TT_DELETED_RECORDS
SELECT *
FROM Deleted
UPDATE dbo.tblLLS_TT_DELETED_RECORDS
SET Deleted_DTTM = GETDATE()
WHERE dbo.tblLLS_TT_DELETED_RECORDS.Deleted_DTTM IS NULL
END
Also with the above trigger if I enter a record on the DELETED_RECORDS table leaving the Delete_DTTM null then delete a record causing the trigger to fire it will fill in that record correctly, but the record(s) deleted coming into the DELETED_RECORDS table will again have a date of 1906. I also noted the date increments one day per each record.
Any idea what might be going on here?
Any other test you want me to perform let me know and I'll post the results. This one is strange. Thanks!
Your insert statement is as follows
INSERT INTO dbo.tblLLS_TT_DELETED_RECORDS
SELECT *
FROM Deleted
You are neither using an explicit column list for the INSERT nor in the SELECT.
You say
I also noted the date increments one day per each record.
My guess is that the columns in the source aren't being inserted into the columns in the destination that you think they are.
It you are deleting a batch of rows with an IDENTITY column value between 2191 - 2555 and inserting that int value into a datetime column you will get an implicit cast to a date in 1906 (the integer is treated as representing number of days since 1st Jan 1900)
I'm assuming that the source column is likely IDENTITY as that would explain the ascending nature of the dates. To resolve this try specifying both column lists explicitly. Also it is important to note that the equivalent source and destination columns must be in the same ordinal positions. SQL Server won't match them up on the basis of column name so for example.
INSERT INTO dbo.tblLLS_TT_DELETED_RECORDS
(col1,
col2,
col3)
SELECT col1,
col2,
col3
FROM Deleted
Also you don't need the separate UPDATE statement either you can just do this in the INSERT
INSERT INTO dbo.tblLLS_TT_DELETED_RECORDS
(col1,
col2,
col3,
Deleted_DTTM)
SELECT col1,
col2,
col3,
getdate()
FROM Deleted
If you execute, what value do you get?
SELECT GETDATE();
Perhaps is misconfigured server time. You could also make the default value of the column "Deleted_DTTM" takes its default system date, then you can skip the last update statement.

Referencing another column in DEFAULT definition in SQL Server 2005

I want to define a column in my table with following requirements:
The column should be insertable. If the value is provided in the INSERT statement, then it should be inserted.
If the column is not referenced in the INSERT statement, then it should be set to the sum of two other columns.
Because of the first requirement, I cannot user computed columns, since they are not insertable. Because of the second, I cannot use DEFAULT, because it doesn't allow referencing other columns in the definition. What other options do I have?
BTW, the column should be NOT NULL.
Here you go, I'm demonstrating this with an example schema since you've not provided your real table/column names.
Table:
CREATE TABLE test
(
id INT NOT NULL PRIMARY KEY IDENTITY, --made up key
col1 INT, --first column to add, wasn't sure if this was nullable or not
col2 INT, --second column to add, wasn't sure if this was nullable or not
col3 INT NOT NULL --this is the column to optionally insert into
)
Here is the trigger definition:
CREATE TRIGGER demo
ON test
INSTEAD OF INSERT
AS
INSERT INTO test (col1,col2,col3)
SELECT inserted.col1,
inserted.col2,
CASE
WHEN inserted.col3 IS NULL THEN COALESCE(inserted.col1, 0) + COALESCE(inserted.col2, 0)
ELSE inserted.col3
END
FROM inserted
Basically it replaces any insert statement done on the table with the one in the trigger, so I check using the inserted temporary table to see if the value that is trying to be inserted into our non-nullable optional column, col3, is NULL. If it is, I replace it with the addition of col1 and col2 (I'm coalescing with zero as you didn't mention if the two source columns are nullable or not).
You can then run insert statements which either include it or not, despite the fact col3 is not nullable:
INSERT INTO test(col1,col2)
SELECT 12, 31
GO
INSERT INTO test(col1, col2, col3)
SELECT 1, 2, 89
GO
Results are:
ID COL1 COL2 COL3
------------------
1 12 31 43
2 1 2 89
If the trigger wasn't there, you could have got an error trying to run that first insert statement, telling you it couldn't insert NULL into col3.
Notice also that the second insert statement that specifies a value has not been replaced by the addition, as requested.
Here's a working SQL Fiddle.