MARIADB and routine : error on declaring routine - sql

I use HeidiSQL to make a stored procedure into my MariaDB DB.
Here's my DB.
MariaDB 10.5, centos 7, HeidiSQL 10.3.0.5771. I do not understand the error message.
CREATE TABLE HostName (
ID_HostName INT NOT NULL AUTO_INCREMENT,
Name TEXT,
Platform TEXT,
PRIMARY KEY (ID_HostName)
);
CREATE TABLE UserName (
ID_UserName INT NOT NULL AUTO_INCREMENT,
SAMAccountName TEXT,
FirstName TEXT,
GivenName TEXT,
PRIMARY KEY (ID_UserName)
);
CREATE TABLE EventData (
ID_EventData INT NOT NULL AUTO_INCREMENT,
ObjectType TEXT,
AccessList TEXT,
ObjectName TEXT,
MessageFull TEXT,
ID_UserName INT NOT NULL,
ID_HostName INT NOT NULL,
FOREIGN KEY (ID_UserName) REFERENCES UserName(ID_UserName),
FOREIGN KEY (ID_HostName) REFERENCES HostName(ID_HostName),
PRIMARY KEY (ID_EventData)
);
CREATE TABLE Event (
ID_Event INT NOT NULL AUTO_INCREMENT,
Heure TEXT,
Provider TEXT,
Code TEXT,
Action TEXT,
OutCome TEXT,
Niveau TEXT,
ID_EventData INT NOT NULL,
FOREIGN KEY (ID_EventData) REFERENCES EventData(ID_EventData),
PRIMARY KEY (ID_Event)
);
Here's my stored procedure, i would like to fill database with JDBC (Logstash jdbc output).
DELIMITER $$
CREATE PROCEDURE `LOGTOMDB`(
IN `in_Name` TEXT,
IN `in_Platform` TEXT,
IN `in_SAMAccountName` TEXT,
IN `in_ObjectType` TEXT,
IN `in_AccessList` TEXT,
IN `in_ObjectName` TEXT,
IN `in_MessageFull` TEXT,
IN `in_Heure` TEXT,
IN `in_Provider` TEXT,
IN `in_Code` TEXT,
IN `in_Action` TEXT,
IN `in_Outcome` TEXT,
IN `in_Niveau` TEXT
)
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''
BEGIN
IF NOT EXISTS ( SELECT Name FROM HostName WHERE Name = in_Name ) THEN
BEGIN
INSERT INTO HostName (Name, Platform) VALUES (in_Name, in_Platform);
END;
IF NOT EXISTS ( SELECT SAMAccountName FROM UserName WHERE SAMAccountName = in_SAMAccountName ) THEN
BEGIN
INSERT INTO UserName (SAMAccountName) VALUES (in_SAMAccountName);
END;
INSERT INTO EventData (ObjectType, AccessList, ObjectName, MessageFull, ID_UserName, ID_HostName) VALUES( in_ObjectType, in_AccessList, in_ObjectName, in_MessageFull, ID_UserName, ID_HostName );
INSERT INTO Event (Heure, Provider, Code, Action, Outcome, Niveau, ID_EventData) VALUES( in_Heure, in_Provider, in_Code, in_Action, in_Outcome, in_Niveau, ID_EventData );
END $$
DELIMITER ;
When i save my procedure, i get this error
Can you help me?
Regards.

You need to END your IF statements
DELIMITER $$
CREATE PROCEDURE `LOGTOMDB`(
IN `in_Name` TEXT,
IN `in_Platform` TEXT,
IN `in_SAMAccountName` TEXT,
IN `in_ObjectType` TEXT,
IN `in_AccessList` TEXT,
IN `in_ObjectName` TEXT,
IN `in_MessageFull` TEXT,
IN `in_Heure` TEXT,
IN `in_Provider` TEXT,
IN `in_Code` TEXT,
IN `in_Action` TEXT,
IN `in_Outcome` TEXT,
IN `in_Niveau` TEXT
)
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''
BEGIN
IF NOT EXISTS ( SELECT Name FROM HostName WHERE Name = in_Name ) THEN
INSERT INTO HostName (Name, Platform) VALUES (in_Name, in_Platform);
END IF;
IF NOT EXISTS ( SELECT SAMAccountName FROM UserName WHERE SAMAccountName = in_SAMAccountName ) THEN
INSERT INTO UserName (SAMAccountName) VALUES (in_SAMAccountName);
END IF;
INSERT INTO EventData (ObjectType, AccessList, ObjectName, MessageFull, ID_UserName, ID_HostName) VALUES( in_ObjectType, in_AccessList, in_ObjectName, in_MessageFull, ID_UserName, ID_HostName );
INSERT INTO Event (Heure, Provider, Code, Action, Outcome, Niveau, ID_EventData) VALUES( in_Heure, in_Provider, in_Code, in_Action, in_Outcome, in_Niveau, ID_EventData );
END $$
DELIMITER ;

Related

How to put argument (table name) of function?

I am using PostgreSQL 14.4 . My script
-- 0.
DROP TABLE IF EXISTS tenant;
CREATE TABLE tenant
(
id smallint primary key,
company_tax_code character varying(14),
period character varying(16), -- 2021070420220705
created timestamp with time zone
);
CREATE OR REPLACE FUNCTION set_id_tenant()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from tenant) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER trigger_insert_without_id_tenant
BEFORE INSERT
ON tenant
FOR EACH ROW
EXECUTE PROCEDURE set_id_tenant();
CREATE INDEX tenant_idx ON tenant (id);
COMMIT;
--------------------------------------------------------------------------------
-- 4.
DROP TABLE IF EXISTS account_object_bank_account;
CREATE TABLE account_object_bank_account
(
id smallint,
account_object_id smallint not null,
bank_account character varying(64),
bank_name character varying(128),
bank_id smallint,
sort_order smallint,
bank_branch_name character varying(256),
province character varying(128),
tenant_id smallint,
PRIMARY KEY (id, tenant_id),
CONSTRAINT fk_tenant FOREIGN KEY (tenant_id) REFERENCES tenant (id)
);
CREATE OR REPLACE FUNCTION account_object_bank_account_setId()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from account_object_bank_account where tenant_id = new.tenant_id) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER account_object_bank_account_trig_insertWithoutId
BEFORE INSERT
ON account_object_bank_account
FOR EACH ROW
EXECUTE PROCEDURE account_object_bank_account_setId();
CREATE INDEX account_object_bank_account_idx ON account_object_bank_account (id, tenant_id);
COMMENT ON TABLE public.account_object_bank_account IS 'Bảng lưu tài khoản ngân hàng của khách hàng';
COMMENT ON COLUMN public.account_object_bank_account.id IS 'PK';
COMMENT ON COLUMN public.account_object_bank_account.account_object_id IS 'FK';
COMMENT ON COLUMN public.account_object_bank_account.bank_account IS 'Số TK ngân hàng';
COMMENT ON COLUMN public.account_object_bank_account.bank_name IS 'Tên ngân hàng';
COMMIT;
--------------------------------------------------------------------------------
-- 5.
DROP TABLE IF EXISTS account_object_belong_to_group;
CREATE TABLE account_object_belong_to_group
(
id smallint,
account_object_id smallint,
account_object_group_id smallint,
tenant_id smallint,
PRIMARY KEY (id, tenant_id),
CONSTRAINT fk_tenant FOREIGN KEY (tenant_id) REFERENCES tenant (id)
);
CREATE OR REPLACE FUNCTION account_object_belong_to_group_setId()
RETURNS trigger AS
$$
DECLARE
BEGIN
new.id = (select coalesce(max(id), -32769) from account_object_belong_to_group where tenant_id = new.tenant_id) + 1;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER account_object_belong_to_group_trig_insertWithoutId
BEFORE INSERT
ON account_object_belong_to_group
FOR EACH ROW
EXECUTE PROCEDURE account_object_belong_to_group_setId();
CREATE INDEX account_object_belong_to_group_idx ON account_object_belong_to_group (id, tenant_id);
COMMIT;
--------------------------------------------------------------------------------
I want re-use this custom function, then put argument of function is table name:
account_object_bank_account_setId()
account_object_belong_to_group_setId()
to
set_id( table_name )
argument sample: account_object_bank_account, account_object_belong_to_group.
How to do?
update your 2 triggers with set_id('table_name')
and use TG_ARGV[] to retrieve the parameter in the trigger function
https://www.postgresql.org/docs/14/plpgsql-trigger.html

SQL Trigger Wont Generate Primary Key

I am trying to create a table that logs all inserts in the author table. Here is the author table, and the Audit_log Table:
CREATE TABLE Author(AuthorID INT PRIMARY KEY NOT NULL,
last_name CHAR(20),
first_name CHAR(20));
CREATE TABLE Audit_Log(Action_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
table_name Char(40),
action_name Char(6),
Date_Time DATETIME DEFAULT CURRENT_TIMESTAMP);
Here is the simple trigger.
DELIMITER $$
CREATE TRIGGER Author_Trigger AFTER INSERT
ON Author
FOR EACH ROW BEGIN
INSERT INTO Audit_Log VALUES('Author', 'INSERT', CURRENT_TIMESTAMP);
END $$
DELIMITER ;
However, when I cause the trigger to occur by inserting into the author table, it says that the columns do not match row 1. How come my Primary key does not get auto generated, despite having the AUTO_INCREMENT Constraint?
How can I get this trigger to generate the primary key?
Basically MySQL thinks that 'Author' is being inserted as the PRIMARY KEY (Action_ID). What you can do to avoid this is specify wich attribute correspond to wich element of the INSERT query. This should work :)
DELIMITER $$
CREATE TRIGGER Author_Trigger AFTER INSERT
ON Author
FOR EACH ROW BEGIN
INSERT INTO Audit_Log(table_name, action_name, Date_Time)
VALUES('Author', 'INSERT', CURRENT_TIMESTAMP);
END $$
DELIMITER ;
For the value to autoincrement, since you do not have defined the data you must insert and place the autoincremental, it does not take it, so the first value is null and through this null it will autoincrement.
DELIMITER $$
CREATE TRIGGER Author_Trigger AFTER INSERT
ON Author
FOR EACH ROW BEGIN
INSERT INTO Audit_Log VALUES(null,'Author', 'INSERT', CURRENT_TIMESTAMP);
END $$
DELIMITER ;

SQLite - No such column when column exists

I have a table (trackedinfo) inside my database that has the following columns (columns obtained by running PRAGMA table_info(trackedinfo);)
The problem is that even though the column sendok exists, when running a query on the database with that field, it throws an error.
Example queries:
SELECT * FROM trackedinfo WHERE sendok IS NULL;
SELECT sendok FROM trackedinfo;
Error:
SQLITE_ERROR: SQL error or missing database (no such column: sendok)
But, if I run a query selecting all of the fields, it brings me the info regarding sendok:
Here is the CREATE command of the database:
CREATE TABLE trackedinfo
(
id INTEGER PRIMARY KEY,
date_time_start TEXT,
date_time_end TEXT,
tracked_name TEXT,
tracked_origin TEXT,
tracked_maker TEXT,
tracked_version TEXT,
tracked_type TEXT,
sendok TEXT,
tracked_id TEXT
);
EDIT
It also happens with the column tracked_id
EDIT 2
Info that I got by executing .schema trackedinfo
CREATE TABLE IF NOT EXISTS "trackedinfo" ("id" INTEGER PRIMARY KEY, "date_time_start" TEXT, "date_time_end" TEXT, "tracked_name" TEXT, "tracked_origin" TEXT, "tracked_maker" TEXT, "tracked_version" TEXT, "tracked_type" TEXT, "sendok " TEXT, "tracked_id " TEXT);
The problem was that I had an space at the end of the name of the columns, solved the problem by deleting such spaces.
Before:
CREATE TABLE IF NOT EXISTS "trackedinfo" ("id" INTEGER PRIMARY KEY, "date_time_start" TEXT, "date_time_end" TEXT, "tracked_name" TEXT, "tracked_origin" TEXT, "tracked_maker" TEXT, "tracked_version" TEXT, "tracked_type" TEXT, "sendok " TEXT, "tracked_id " TEXT);
After:
CREATE TABLE IF NOT EXISTS "trackedinfo" ("id" INTEGER PRIMARY KEY, "date_time_start" TEXT, "date_time_end" TEXT, "tracked_name" TEXT, "tracked_origin" TEXT, "tracked_maker" TEXT, "tracked_version" TEXT, "tracked_type" TEXT, "sendok" TEXT, "tracked_id" TEXT);

audit trigger postgres

I want to store my old records in a table when something has changed. I tried to do it with this documentation. But i still get a error. The error is: relation serie doesn't exist.
The original table:
CREATE TABLE "Terra_nub_v1"."Serie"
(
uuid uuid NOT NULL, -- t.b.v. pakbon
serie_nr integer NOT NULL,
aantal_slijpplaten integer,
producent text,
locatie text,
klaar boolean,
onderzocht boolean,
gerapporteerd boolean,
opmerking text,
verwijzing_hoofdrapportage text,
verwijzing_overige_rapportages text,
onderzoeksgebied polygon,
CONSTRAINT "Serie_pkey" PRIMARY KEY (serie_nr)
)
The archive table:
CREATE TABLE "Terra_nub_v1".serie_history
(
uuid uuid NOT NULL,
serie_nr integer NOT NULL,
aantal_slijpplaten integer,
producent text,
locatie text,
klaar boolean,
onderzocht boolean,
gerapporteerd boolean,
opmerking text,
verwijzing_hoofdrapportage text,
verwijzing_overige_rapportages text,
onderzoeksgebied polygon,
datumtijd_wijziging timestamp without time zone,
aangepast_door text,
CONSTRAINT serie_history_pkey PRIMARY KEY (serie_nr)
)
I make the trigger function this way:
CREATE OR REPLACE FUNCTION "Terra_nub_v1".serie_history_trigger()
RETURNS trigger AS $BODY$
BEGIN
IF (TG_OP ='UPDATE') THEN
INSERT INTO Terra_nub_v1.serie_history SELECT OLD.*,NOW(), USER ;
RETURN OLD;
ELSEIF (TG_OP = 'DELETE') THEN
INSERT INTO Terra_nub_v1.serie_history SELECT OLD.*,NOW(), USER ;
RETURN OLD;
END IF;
RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql VOLATILE;
Create Trigger serie_history_trigger
BEFORE UPDATE OR DELETE ON Terra_nub_v1.serie
FOR EACH ROW EXECUTE PROCEDURE process_emp_audit();
I m using postgres
Greetings

ON CONFLICT IGNORE - How to insert data into a dupes table if there is a conflict instead of ignoring

I have this query:
#db.execute("CREATE TABLE IF NOT EXISTS programs(programId INTEGER PRIMARY KEY NOT NULL
AUTOINCREMENT, programName TEXT NOT NULL, episodeName TEXT, UNIQUE(programName,
episodeName) ON CONFLICT IGNORE)")
Instead of ignoring this, what I'd like it to do is to insert the the data into a "Conflicts" table if there is a conflict in any one of my tables. How do I set up for this scenario? Obviously Id have to have the conflicts table created but what next?
Thanks
Are you looking for this?
Tables
CREATE TABLE programs
(
programId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
programName TEXT NOT NULL,
episodeName TEXT,
UNIQUE(programName, episodeName) ON CONFLICT IGNORE
);
CREATE TABLE program_dupes
(
programId INTEGER,
programName TEXT,
episodeName TEXT
);
Trigger
CREATE TRIGGER tg_programs_insert
BEFORE INSERT ON programs
FOR EACH ROW
BEGIN
INSERT INTO program_dupes (programId, programName, episodeName)
SELECT programId, NEW.programName, NEW.episodeName
FROM programs
WHERE programName = NEW.programName
AND episodeName = NEW.episodeName;
END
Here is SQLFiddle demo