Informix SQL - What is wrong with this simple stored procedure &| trigger syntax? - sql

IBM Informix Dynamic Server Version 11.50.FC6
I am trying to execute a simple stored procedure from within an update trigger. Together, they are used to update a field with the current timestamp when another field in the same row is updated.
Table sp_test:
id (serial int, unique, not null, primary key)
stat (char(1), not null, default="A")
add_date (date, not null, default today)
upd_date (date, null)
The stored procedure code is:
create procedure upd_row_date_proc (cid int)
update sproc_trig_rec set upd_date = current where id = cid;
end procedure;
This executes fine and creates the routine, but the trigger I am trying to implement on updates is not working.
The trigger code is:
create trigger upd_row_date_trig
update of stat on sproc_trig_rec
after (execute procedure upd_row_date_proc(id));
I've tried a bunch of syntax variations, but cannot get it to work.
I usually get my error on the ( char of the 3rd line. Here's the error code:
201: A syntax error has occurred.
Error in line 3
Near character position 0
Does anyone know what I'm doing wrong in the syntax of the trigger? Could this type of updating be defined in the creation of the table, or do I need to accomplish it by doing it the way described above?
Thanks for any help

This finally worked for me
create trigger ken_trig
update of stat on sproc_trig_rec
referencing old as ken_pre_upd
for each row (execute procedure ken_proc(ken_pre_upd.id));

Related

SQL Server stored procedure: how to return column names/values of type failures in variable?

Ambiguous thread name, I apologize. I am not new to SQL, but I'm new to coding longer stored procedures so I don't deal with variables much outside of passing through maybe a table name or returning row count, etc.
I have a stored procedure that is executing an insert from a staging table to a fact table. There are a couple type casts in the insert.
If the insert fails due to a typecast. Is there any way to return the name of the column that failed, along with what the failed value was? How would I code that? I know that Try_parse would make it so the stored procedure doesn't fail on type cast failure, but I want to be able to pass back exactly what column and value failed.
I show an example here:
Create Procedure dbo.Example_Insert
#updateUser varchar(255)
As
Begin
Insert Into dbo.Energy_Costs (Energy_Cost_Id, Project_Id, Propane_Cost_Dollars,
Electricity_Cost_Dollars, Fuel_Savings_Evaluator)
Select
Next Value For energy_cost_id,
r.project_id,
Cast(r.propane_cost_dollars As Decimal(18,2)),
Cast(r.electricity_cost_dollars As Decimal(18,2)),
#update_user fuel_savings_evaluator
From
staging_table r
return ##ROWCOUNT
end
You can use CURSOR in sql then insert one line at a time. When insert fail return value currently row error.
I hope my idea suitable with you.

Create Transaction on MariaDB to set expiry time for ip blacklist

The question I used to help build this transaction is here:
How to add 1 hour to currrent_timestamp in mysql which is the default value?
I'm trying to create a transaction in my database to fill in the allowed column in my mariadb database table blacklisted_ips. I want the allowed column to be an hour after my added column which has a default value of CURRENT_TIMESTAMP. Here is my transaction so far:
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END;
The error message I'm getting is the following:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2
Here is my table schema:
CREATE TABLE blacklisted_ips (
ip_id int(11) NOT NULL AUTO_INCREMENT,
ip_add varchar(15) NOT NULL,
added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
allowed timestamp NOT NULL,
PRIMARY KEY (ip_id)
);
In MySQL, there's an ambiguity between the ; that terminates a CREATE TRIGGER statement, and the possible ; characters that terminate individual statements in the body of the trigger.
The error you got is confusing:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2
Normally a syntax error includes the text of the statement following the position where the syntax parser got confused. But in your case, it got confused at ;, thinking it was the end of the CREATE TRIGGER statement. Therefore the error occurs at the termination; there is no text following the error as far as the parser is concerned.
The same issue affects CREATE PROCEDURE and CREATE FUNCTION.
To fix the ambiguity, MySQL client supports a builtin command to change the DELIMITER, so you can change it to something distinct from any sequence of characters that appear in the body of your routine.
DELIMITER ;;
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END ;;
Alternatively, since in your case the trigger is a single-statement trigger, you don't need a BEGIN...END block at all. This way you can skip changing the DELIMITER, because the ; that terminates your CREATE TRIGGER is the same ; that terminates the single statement of the trigger.
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
P.S.: This is documented with an example here: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

Firebird with .net driver - drop table if exists

I'm new to Firebird and I'm testing a few things to check out the differences between Fb and SQlite (and the .net driver).
I am trying to do a drop table if exists followed by the creation of a table. In Sqlite I am able to do this by:
command.CommandText = #"DROP TABLE IF EXISTS Persons; CREATE TABLE Persons (
PersonID int,
LastName text,
FirstName text,
Address text,
City text); ";
command.ExecuteNonQuery();
However in Firebird the same query fails. I've read that this is not possible to use IFs directly in Firebird SQL, so I've tried to use:
command.CommandText = #"
EXECUTE BLOCK AS
BEGIN IF EXISTS
(SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = 'Persons')
THEN DROP TABLE Persons; END CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); ";
command.ExecuteNonQuery();
But it fails also with the following error:
Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column
27
Can you please help me on this? I've tried to find more info on the web that could help me, but did not have any luck.
Firebird's SQL syntax doesn't have a drop table if exists, instead use recreate table. recreate table will try to drop the table if it exists before creating it. The syntax of recreate table is - other than recreate instead of create - the same as create table.
Your attempt to use execute block fails for two reasons:
You cannot execute two statements together as a command. If you want to execute a script of multiple statements, you'll need to execute each statement individually or use the FbScript class which will parse the script and execute the individual statements for you.
Even if you execute these statements individually, it will still fail, because PSQL (the stored procedure language used in execute block) does not allow execution of DDL. You can use execute statement to circumvent this limitation, but it is better not to do that. In that way you could also address the previous point by executing both - using execute statement - within the execute block.
Alternatively you could just drop the table unconditionally and catch (and ignore) the resulting exception.

What's wrong with my EXECUTE command?

I'm trying to make a PROCEDURE that makes it easier to store changes users make in their settings (like a server log, you know right) into a table user_settings_changelog. I finally made the PROCEDURE acceptable so my sql program (HeidiSQL) would store it. Now the problem is this: how to properly EXECUTE it. I tried this multiple times with multiple lines of code, but it seemed nothing worked. Can anyone help me out here?
The PROCEDURE query:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (name_setting VARCHAR(45),
changed_from VARCHAR(45), changed_to VARCHAR(45), username
VARCHAR(45))
BEGIN
INSERT INTO user_settings_changelog
VALUES (GETDATE(), name_setting, changed_from, changed_to,
username);
END$$
The table user_settings_changelog has 5 columns: date DATETIME, name_setting VARCHAR(45), changed_from VARCHAR(45), changed_to VARCHAR(45) and username VARCHAR(45).
The EXECUTE query:
EXECUTE setting_adjustment_log ('background','black','white','TheCoderNoob');
The error HeidiSQL gives me:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('background','black','white','TheCoderNoob')' at line 1
Maybe useful to mention: I am using a version of USBwebserver from a few years ago.
EDIT: I've been looking at the EXECUTE/CALL query in MySQL Workbench for a while, it seems like the database expects something between the name of the procedure and the given data or something. When I hover over it, it reads:
Syntax error: 'background' (single quoted text) is not a valid input at this position
First, you should always include the column names in an insert statement. (There might be a few exceptions, but if you are learning SQL, then sticks with best practices.)
Second, distinguish the input parameters from possible column names.
Third, your code looks like MySQL, so use NOW() or a similar function:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (
in_name_setting VARCHAR(45),
in_changed_from VARCHAR(45),
in_changed_to VARCHAR(45),
in_username VARCHAR(45)
)
BEGIN
INSERT INTO user_settings_changelog (date, name_setting, changed_from, changed_to, username)
VALUES (NOW(), in_name_setting, in_changed_from, in_changed_to,
in_username);
END$$
When you call the stored procedure, use call:
CALL setting_adjustment_log('background', 'black', 'white', 'TheCoderNoob');

Stored Procedures and Triggers in data base

what do Stored Procedures and Triggers in data base mean ?
how can i create Stored Procedures ?
how can i crest Triggers ?
if you have simple examples for each of these .please help :)
what i know is only about trigger which is activated if an action of(insert or delete or update ) violates the constrains specified but i don't know how to create ,so again if any have example please
Think of a Stored Procedure as a method in your code. It runs a specific set of instructions.
Stored Procedures are created to, for example, manage complex sets of data that would normally be a pain to handle along in your code.
You can create a Stored Procedure with the following instructions:
Oracle
CREATE OR REPLACE PROCEDURE P_PROCEDURE_NAME (
pParameter1 NUMBER
, pParameter2 VARCHAR2(100 Bytes)
) AS
BEGIN
-- Procedure code here...
END;
SQL Server
CREATE PROCEDURE cspProcedureName
#parameter1 int
, #parameter2 nvarchar(100)
AS
-- Procedure code here...
Oracle
As for the Triggers, they are sets of code called upon an action occuring to the related table. For instance, in Oracle, there are no INDENTITY columns such as SQL Server offers. Instead, Sequences are used along with Triggers to simulate the same. Hence, you will need to create an Oracle SEQUENCE, then the TRIGGER to update the ID field of your table.
CREATE SEQUENCE SEQ_CUSTOMERS
MINVALUE 1
MAXVALUE 65535
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER TRG_CUSTOMERS_INSERT
BEFORE INSERT
ON TBL_CUSTOMERS
FOR EACH ROW
BEGIN
:NEW.CUST_ID := SEQ_CUSTOMERS.NEXTVAL;
END;
SQL Server
A trigger example in SQL Server would be updating automatically the update datetime of a record. Consider the following:
CREATE TABLE Customers (
CustId int NOT NULL IDENTITY(1, 1) PRIMARY KEY
, CustName nvarchar(100) NOT NULL
, CreatedOn datetime DEFAULT GETDATE()
, LastUpdate datetime NOT NULL
)
GO
CREATE TRIGGER trgCustomersUpdt
AFTER UPDATE
ON Customers
AS
update Customers
set LastUpdate = GETDATE()
where CustId = inserted.Custid
GO
DISCLAIMER
This code has not been tested and may require minor changes for it to work properly against its respective RDBMS.
To sum it up, Triggers are mainly used to as illustrated here, despite there are many other possible use, such as building up an history of table changes that occured throught time, keeping all records of transactions into an history table or the like. The Stored Procedures are mainly used to perform complex database tasks where this would get too complex to do in code.