The trigger defined below does not work in sqlite3.
CREATE TRIGGER 'delete_expired' BEFORE INSERT ON 'visitor' BEGIN DELETE FROM visitor WHERE 'create_at' <= date('now', '-6 day'); END
But, this does... Something might be wrong with the conditions. Can anybody point me out? Thanks in advance
CREATE TRIGGER 'delete_expired' BEFORE INSERT ON 'visitor' BEGIN DELETE FROM visitor; END
If you need to quote table names and column names, use double quotes, not single quotes.
CREATE TRIGGER 'delete_expired'
BEFORE INSERT ON visitor
BEGIN DELETE FROM visitor WHERE create_at <= date('now', '-6 day');
END;
Single quotes usually denote a literal string.
sqlite> select 'create_at' from visitor;
create_at
In an interactive session . . .
sqlite> create table visitor (create_at timestamp);
sqlite> CREATE TRIGGER 'delete_expired'
...> BEFORE INSERT ON visitor
...> BEGIN DELETE FROM visitor WHERE create_at <= date('now', '-6 day');
...> END;
sqlite> insert into visitor values ('2014-01-01');
sqlite> select * from visitor;
2014-01-01
sqlite> insert into visitor values ('2014-11-06');
sqlite> select * from visitor;
2014-11-06
Related
I am getting 0099 error while creating below trigger:
create trigger AuditTrigger1
before update, insert
on MPUZNTAB
for each row
declare
begin
insert into AuditTable1
(ZNCODE, DES , SHDES, WhenChanged)
values
(:new.ZNCODE,:new.DES,:new.SHDES, getdate())
end;
Please recomment changes
You missed a semicolon at the end of the insert statement
You should write before update OR insert
If getdate() is not a user defined function, you should use SYSDATE instead, to get the current time
I'm not sure the :new values are correct. Are they fields of the table MPUZNTAB?
This code should work:
Create Or Replace Trigger AuditTrigger1
Before Update Or Insert On MPUZNTAB
For Each Row
Declare
Begin
Insert Into AuditTable1
(ZNCODE,
DES,
SHDES,
WhenChanged)
Values
(:new.ZNCODE,
:new.DES,
:new.SHDES,
SYSDATE);
End;
I'm trying to insert a parent and child at the same time.
My idea is to insert the parent, get the id using SELECT last_insert_rowid() AS [Id] and use this id to insert the child
I can get each part of this working independently but not as a whole. This is what I currently have:
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
The above works - so far so good. Now I want to use the result of this in the child insert. This is what I have:
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES( 1, 2, SELECT Id FROM (
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
);
I get this error:
near "SELECT": syntax error:
Can anyone point me in the right direction?
You can't use INSERT in SELECT statement. You should first insert and then use last inserted id:
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT last_insert_rowid()));
Since you want to insert many records with parent ID, here is a workaround:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE IF NOT EXISTS temp(id integer);
DELETE FROM temp;
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO temp SELECT last_insert_rowid();
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT id FROM temp LIMIT 1));
.............
COMMIT;
DROP TABLE temp;
Or you can create a permanent table to this effect.
That SQLite.Net PCL driver assumes that you use the ORM: inserting an object will automatically read back and assign the autoincremented ID value.
If you're using raw SQL, you have to manage the last_insert_rowid() calls yourself.
Your idea is correct, but you have to do everything in separate SQL statements:
BEGIN; -- better use RunInTransaction()
INSERT INTO Parent ...;
SELECT last_insert_rowid(); --> store in a variable in your program
INSERT INTO Child ...;
...
END;
(SQLite is an embedded database and has no client/server communication overhead; there is no reason to try to squeeze everything into a single statement.)
I am using oracle 10g.
I want to delete all the records from every table of a schema (user) with child records present.
I have tried to disable all the constraints present in every table and then try to delete the records.
But it took the whole day to do so.
Do we have a better way to do it?
I have tried this
BEGIN
FOR i IN (SELECT table_name FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'delete from' || '"'||i.table_name||'"';
END LOOP;
COMMIT;
END;
but exceptions comes as child records are present.
I want something like this.
BEGIN
FOR i IN (SELECT table_name FROM user_cons_columns)
LOOP
for j in (select column_name from i.table_name)
loop
EXECUTE IMMEDIATE 'alter table "'||i.table_name||'"'||' disable constraint "'||j.column_name||'"';
end loop;
END LOOP;
COMMIT;
END;
Please help me out a little here.
You can use data pump, export your schema:
expdp system/s
directory=backup_dir
CONTENT=METADATA_ONLY
dumpfile=YOUR_SCHEMA_HEREmetaddl.dmp
schemas=YOUR_SCHEMA_HERE
logfile=YOUR_SCHEMA_HERE.$Date.log
then drop your schema:
drop user YOUR_SCHEMA_HERE cascade;
and then import it:
impdp system/s
directory=backup_dir
dumpfile=YOUR_SCHEMA_HEREmetaddl.dmp
logfile=imp_log_of_meta.log
Try executing the following and then delete all tables which are having child records.
You could change the query to enable constraints upon deletion of all records.
BEGIN
FOR c IN ( SELECT c.owner, c.table_name, c.constraint_name
FROM user_constraints c, user_tables t
WHERE c.table_name = t.table_name
AND c.status = 'ENABLED'
AND constraint_type = 'R'
ORDER BY c.constraint_type DESC)
LOOP
DBMS_UTILITY.exec_ddl_statement (
'alter table "'
|| c.owner
|| '"."'
|| c.table_name
|| '" disable constraint '
|| c.constraint_name);
END LOOP;
END;
/
I want to insert time value to SQLite. I searched about functions, modifiers, timestrings but I could not achieve to my aim. When I write my query, this does not record the '.323', only records '08:25:01'. I want to record '08:25:01.323'. My query is:
insert into table_name (column_name) values (time('08:25:01.323'))
I'm waiting for your help..
Store the time as a string and only parse it whe necessary.
sqlite> create table t (t text);
sqlite> insert into t values ('08:25:01.323');
sqlite> select * from t;
08:25:01.323
sqlite> select t, time(t) from t;
08:25:01.323|08:25:01
I am a beginner in Oracle. I created the table type as like as follows:
TYPE metertable IS TABLE OF pseb.metermaster.meterid%type;
I dnt know how to insert the value into that table type. I need to store the whole resultset of the following query into the table type.
select distinct(meterid)
from pseb.consumerfeedermetermapper
where feederid in (select distinct (fm.FeederID)
from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
where fm.substationid=v_v_type
and cfm.feederid=fm.feederid
and cfm.FeederID>0)
and meterid >0
order by meterid;
Help me to do that.
Use BULK COLLECT to select the data into a variable of that type:
declare
mt metertable;
begin
select distinct(meterid)
bulk collect into mt
from pseb.consumerfeedermetermapper
where feederid in (select distinct (fm.FeederID)
from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
where fm.substationid=v_v_type
and cfm.feederid=fm.feederid
and cfm.FeederID>0)
and meterid >0
order by meterid;
-- Now use mt...
end;
I had the same issue, and this code help me :
SET SERVEROUTPUT ON
DECLARE
TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;
l_tab t_bulk_collect_test_tab;
l_cursor SYS_REFCURSOR;
BEGIN
-- Way 1
OPEN l_cursor FOR 'SELECT * FROM bulk_collect_test';
FETCH l_cursor
BULK COLLECT INTO l_tab;
CLOSE l_cursor;
DBMS_OUTPUT.put_line('Dynamic FETCH : ' || l_tab.count);
-- Way 2
EXECUTE IMMEDIATE 'SELECT * FROM bulk_collect_test' BULK COLLECT INTO l_tab;
DBMS_OUTPUT.put_line('Dynamic EXECUTE: ' || l_tab.count);
END;
/
http://www.dba-oracle.com/plsql/t_plsql_dynamic.htm
declare
type tab_type is table of consumerfeedermetermapper%rowtype;
tab_t tab_type;
begin
select distinct(meterid) bulk collect into tab_t
from pseb.consumerfeedermetermapper
where feederid in (select distinct (fm.FeederID)
from pseb.feedermaster fm,pseb.consumerfeedermetermapper cfm
where fm.substationid=v_v_type
and cfm.feederid=fm.feederid
and cfm.FeederID>0)
and meterid >0
order by meterid;
end;
You can use the above code