Move rows to another table - sql

I want move rows from one table to another (in order to move unused data to historic storage).
How to do this in most clever way?
I found such solutions but looks like it is not working for Oracle dialect
INSERT dbo.CustomersInactive (
CustomerID,
FirstName,
LastName
) SELECT
CustomerID,
FirstName,
Lastname
FROM (
DELETE dbo.CustomersActive
OUTPUT
DELETED.CustomerID,

This solution seems working:
DECLARE
TYPE CustomerSet IS TABLE OF CustomersActive%ROWTYPE;
inactive CustomerSet;
BEGIN
delete from CustomersActive returning CustomerID,FirstName,Lastname bulk collect into inactive;
FOR i IN inactive.FIRST .. inactive.LAST LOOP
insert into CustomersInactive values (inactive(i).CustomerID,inactive(i).FirstName,inactive(i).Lastname);
END LOOP;
END;

I hope this is the case you need:
--init objects
create table active_cust
(cust_id integer,
name varchar2(100 char)
);
create table inactive_cust as
select *
from active_cust
where 1=2;
--init data
insert into active_cust values (1, 'Przemo');
insert into active_cust values (2,'Pan Miecio');
insert into active_cust values (3,'Pan Franio');
insert into inactive_cust values (3,'Pan Franio');
--merge active and inactive
merge into inactive_cust dest
using (select * from active_cust) srce
on (srce.cust_id = dest.cust_id)
when not matched then insert values
(srce.cust_id, srce.name )
--here specify conditions on which customer is being
--accounted as inactive
/*where srce.some_status_date < sysdate - 100 */
;--only two rows merged as we have >Pan Franio< already in a list of inactive customers!
--now as we have all inactive customers in inactive_cust table, delete from active_cust where id is present in inactive_cust
delete from active_cust ac
where ac.cust_id in (select cust_id
from inactive_cust);
drop table active_cust;
drop table inactive_cust;

Related

Using sequence while inserting data into 2 tables at same time

I am trying to insert data using select statement. The table which I am inserting is having foreign key and it is sequence ID. How do I accomplish this? Because if I insert the sequence key in associated table first then how do I get the list of all the sequence ID to insert into the table.
Please note I am using insert with select statement so is there way to accomplish this without using cursor?
I think you can extract sequence value and then re-use it as many times as you want:
DECLARE #NextValue INT
SELECT #NextValue = NEXT VALUE FOR MySequence
SELECT NextValue = #NextValue
INSERT INTO PrimaryTable(PK_ID) VALUES (#NextValue);
INSERT INTO SecondaryTable(FK_ID) VALUES (#NextValue);
Here what I have tried.
DECLARE #MyTabVaR TABLE
(
FOREIGNKEY_ID INT,
COMMON_COL INT
);
INSERT INTO #MyTabVaR
SELECT NEXT VALUE FOR DBO.MY_SEQ,COMMON_COL FROM another_table2
INSERT INTO actual_table
SELECT FOREIGNKEY_ID FROM #MyTabVaR
INSERT INTO another_table
SELECT * FROM copy_table C
LEFT JOIN actual_table A
ON C.COMMON_COL=A.COMMON_COL
WHERE A.FOREIGNKEY_ID IS NOT NULL

I need to create 3 rows in Student_Fee table when 1 row is inserted in Student table using trigger

Following is the trigger
Create TRIGGER [dbo].[Student]
ON [dbo].[Student]
After INSERT
AS
BEGIN
Insert Into Student_Fee([StudentID],[InstID],[PersonID],[FeeSubmiteTime],[FeeMsg],[Type])
Select NULL,3,PersonID,getdate(),'Student submitted on','Student' from INSERTED
END
I need to create 3 rows in Student_Fee table when 1 row is inserted in Student table.First row in Student_fee must have StudentID Null and for other two rows student ID is filled obtained from previous table. Also, Feemsg should be different for the 3 rows. It is text and could be any value. And for type there are two types Student and Admin and the types are also not fixed. They can vary while inserting rows.
How can I do that by using trigger?
here you go :
CREATE TABLE Temp1abhari (
id INT identity(1, 1)
,number INT
);
CREATE TABLE Temp2abhari (
id INT
,number INT
);
CREATE TRIGGER TTemp1abhari ON Temp1abhari
FOR INSERT
AS
BEGIN
INSERT INTO Temp2abhari
VALUES (
NULL
,1
);
INSERT INTO Temp2abhari
SELECT ID
,2
FROM Inserted;
INSERT INTO Temp2abhari
SELECT ID
,3
FROM Inserted;
END

Inserting multiple rows in temp table without loop

This question has already been asked several times but the solution is not working for me. I don't know why.
Actually i am trying to create a temp table in sql query where i am inserting some records in temp table using select into but everytime it returns empty row:
here is what i am trying:
Create Table #TempTable
(
EntityID BIGINT
)
INSERT INTO #TempTable (EntityID)
SELECT pkEntityID FROM Employee WHERE EmpID = 45
Select * from #TempTable
Corresponding to 45 , there are 10 rows in Employee table. IS it like I have to do something else or a loop like structure here as we can only insert one row in a table at once?
This has been stated in the comments, all of which i up-voted, but to answer your question... there isn't anything else you have to do. There clearly isn't an EmpID = 45 in your source table. Here's a reproducible example:
Declare #Employee Table (pkEntityID bigint, EmpID int)
insert into #Employee (pkEntityID, EmpID)
values
(32168123,45),
(89746541,45),
(55566331,45),
(45649224,12)
Create Table #TempTable
(
EntityID BIGINT
)
INSERT INTO #TempTable (EntityID)
SELECT pkEntityID FROM #Employee WHERE EmpID = 45
Select * from #TempTable
drop table #TempTable
Have you accidentally also created the Employee table in the master database and you are currently connected to the master database?

Insert into a Informix table or update if exists

I want to add a row to an Informix database table, but when a row exists with the same unique key I want to update the row.
I have found a solution for MySQL here which is as follows but I need it for Informix:
INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE name="A", age=19
You probably should use the MERGE statement.
Given a suitable table:
create table table (id serial not null primary key, name varchar(20) not null, age integer not null);
this SQL works:
MERGE INTO table AS dst
USING (SELECT 1 AS id, 'A' AS name, 19 AS age
FROM sysmaster:'informix'.sysdual
) AS src
ON dst.id = src.id
WHEN NOT MATCHED THEN INSERT (dst.id, dst.name, dst.age)
VALUES (src.id, src.name, src.age)
WHEN MATCHED THEN UPDATE SET dst.name = src.name, dst.age = src.age
Informix has interesting rules allowing the use of keywords as identifiers without needing double quotes (indeed, unless you have DELIMIDENT set in the environment, double quotes are simply an alternative to single quotes around strings).
You can try the same behavior using the MERGE statement:
Example, creation of the target table:
CREATE TABLE target
(
id SERIAL PRIMARY KEY CONSTRAINT pk_tst,
name CHAR(1),
age SMALLINT
);
Create a temporary source table and insert the record you want:
CREATE TEMP TABLE source
(
id INT,
name CHAR(1),
age SMALLINT
) WITH NO LOG;
INSERT INTO source (id, name, age) VALUES (1, 'A', 19);
The MERGE would be:
MERGE INTO target AS t
USING source AS s ON t.id = s.id
WHEN MATCHED THEN
UPDATE
SET t.name = s.name, t.age = s.age
WHEN NOT MATCHED THEN
INSERT (id, name, age)
VALUES (s.id, s.name, s.age);
You'll see that the record was inserted then you can:
UPDATE source
SET age = 20
WHERE id = 1;
And test the MERGE again.
Another way to do it is create a stored procedure, basically you will do the INSERT statement and check the SQL error code, if it's -100 you go for the UPDATE.
Something like:
CREATE PROCEDURE sp_insrt_target(v_id INT, v_name CHAR(1), v_age SMALLINT)
ON EXCEPTION IN (-100)
UPDATE target
SET name = v_name, age = v_age
WHERE id = v_id;
END EXCEPTION
INSERT INTO target VALUES (v_id, v_name, v_age);
END PROCEDURE;

Stored Procedure Insert (Select and Values)

I'm looking to have a stored procedure that will:
run through Table A and retrieve all IDs.
insert into Table B all IDs (loop) but also static values which aren't found in Table A.
How do I approach this?
CREATE OR REPLACE PROCEDURE TEST AS
BEGIN
select ID from TABLE A;
INSERT INTO TABLE B
(
created_date,
created_by,
ID
)
VALUES ('sysdate', '1', 'RESULTS FROM SELECT QUERY');
END TEST;
Not sure how to merge static data ('sysdate' and '1') with results from a query.
No need for 2 separate queries. This should work with INSERT INTO SELECT:
INSERT INTO TABLEB
(
created_date,
created_by,
ID
)
SELECT 'sysdate', '1', id
FROM TABLEA