I am working with pgsql and i need to convert my mysql trigger to pgsql trigger.Pgsql query has executed successfully,but i am not able to view output.Please help me.
The code of trigger looks well. I don't see any problem. When you design trigger, then the RAISE NOTICE statement is your best friend. The often error is human error - you can set trigger on wrong table, you can try to insert to wrong table - the notification shows so all is ok.
postgres=> \sf func_trg
CREATE OR REPLACE FUNCTION public.func_trg()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'func_trg: %', new;
RETURN new;
END;
$function$
postgres=> CREATE TRIGGER xxx AFTER INSERT ON foo_table
FOR EACH ROW EXECUTE PROCEDURE func_trg();
CREATE TRIGGER
postgres=> set client_min_messages to notice;
SET
postgres=> INSERT INTO foo_table VALUES(10,20);
NOTICE: func_trg: (10,20)
INSERT 0 1
Related
i am a php beginer. i want to call a mysql procedure and display the output rows returned by the procedure. the code is below
mysql
delimiter //
create procedure get_ques_list(in p_ques_ctgr varchar(10))
begin
select ques from question_list where ques_categorey=p_ques_ctgr
order by ques;
end //
delimiter ;
php code
$result=mysql_query("CALL GET_QUES_LIST('S_001')");
while($row=mysql_result($result,0,0))
{
echo $row['ques'];
}
but it is giving warning messgge
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
but the same code works fine and display 5 rows if i use query instead of calling procedure
$result=mysql_query("select ques from question_list where
ques_categorey='S_001' order by ques");
procedure is also working fine when executed in mysql
can anyone tell where m going wrong in that??
any help is appreciated
I launch a pl/sql script by a PHP application but I notice when there is either an insert, an update or a delete in the script, the script stops. While there is no problem if I use this script directly in sqldeveloper.
Is there something particular to do in order to use insert, delete, update ?
Thanks
procedure update_nbr_execution
(dem_id in number)
IS
BEGIN
UPDATE BCN_DEMANDE_EXTRACTION
SET nombre_execution = nvl(nombre_execution,0) + 1
WHERE id = dem_id;
END;
In my php file :
$query = "BEGIN
ecrire_requete(:demande_id, :p_nom);
END;";
$stid = oci_parse($conn, $query);
$tabvars = oci_new_collection($conn,'MYTABLETYPE');
oci_bind_by_name($stid, ':p_nom', $tabvars, -1, SQLT_NTY);
oci_bind_by_name($stid, ':demande_id', $_POST['demande_id']);
oci_execute($stid, OCI_DEFAULT);
And update_nbr_execution is call by ecrire_requete.
There is a trigger involve in update_nbr_execution, a field date is automatically update with the fiel nbr_execution. May it come from the trigger ?
Edit : after isolating some part, I'm now getting this error : ORA-04088: error during execution of trigger. So it does come from the trigger which looks like this :
create or replace
TRIGGER BCN_FORMAT_NOM_FICHIER_BI
BEFORE INSERT OR UPDATE ON BCN_DEMANDE_EXTRACTION
REFERENCING NEW AS NEW
FOR EACH ROW
DECLARE
BEGIN
if inserting then
:new.FORMAT_NOM_FICHIER_DONNEES:='bcn_<nom_lot>_<id demande>_<n° version>_<description>_<date>.dat';
:new.FORMAT_NOM_FICHIER_CONTROLE:='bcn_<nom_lot>_<id demande>_<n° version>_<date>.ctr';
:new.FORMAT_NOM_FICHIER_JETON:='bcn_<nom_lot>_<id demande>_<n° version>_<date>.jet';
:new.FORMAT_NOM_FICHIER_ZIP:='bcn_<nom_lot>_<id demande>_<n° version>_<date>';
:new.CREATED_AT:=TO_CHAR(SYSDATE,'DD/MM/YY');
:new.UPDATED_AT:=TO_CHAR(SYSDATE,'DD/MM/YY');
else
:new.UPDATED_AT:=TO_CHAR(SYSDATE,'DD/MM/YY');
end if;
END;
This is probably a date format issue.
If CREATED_AT and UPDATED_AT are both dates, then = TO_CHAR(SYSDATE,'DD/MM/YY')
will convert SYSDATE to a string, and then implicitly convert the string back to a date. The implicit date conversion
depends on NLS_DATE_FORMAT, which is set by each client.
You probably have SQL Developer set to use something like DD/MM/YY, but PHP is using something else. If you just
want to remove the time from SYSDATE, you should use TRUNC(SYSDATE) instead.
I am trying to create an sql proceedure that will return the results back to the php page.
I want to be able to call the procedure as follows from the php
call procedure_name($var1)
which will run this script:
-- ---------------------------------------------------------------------------------
-- pUIGetCliStmtGenFlag
--
-- This procedure returns the status of the Trading Period:
--
-- ---------------------------------------------------------------------------------
drop procedure if exists pUIGetCliStmtGenFlag;
delimiter //
create procedure pUIGetCliStmtGenFlag(
IN pTradingPeriodMonth DATE
)
MODIFIES SQL DATA
COMMENT 'Checks if the TP has been closed'
begin
SELECT trading_period_month,
dt_end,
amt_traded_system_ccy
FROM ca_trading_period
WHERE trading_period_month=$var1
-- If amt_traded_system_ccy is NULL give the TP an open status otherwise mark as closed
IF amt_traded_system_ccy is NULL
$tpstatus='open'
ELSE
$tpstatus='closed'
end;
//
delimiter ;
I then want to be able to use $tpstatus in the rest of the php script.
I know this is simple but this is completely new to me and I cant find the correct method
You have to call the stored procedure from php and THEN get the values back, using OUT parameters of the procedure.
Please follow these instructions:
https://stackoverflow.com/a/48161/1291428
Have a script that I can copy and run in mysql shell just fine, but explodes when attempting the same script in php5 mysql_query. Part of the script:
-- sync shadow with users
drop trigger users_post_insert;
delimiter $$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end$$
delimiter ;
Raises error:
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
'delimiter $$\ncreate trigger users_post_insert after insert
on users\n\tfor each ro' at line 8
Again, have done similar executing script files with PostgresQL and Oracle scripts, so this took me off guard.
Do MySQL scripts need to be passed through a regex before being run, or what?
These are scripts that have been debugged and then applied via php to new schemas.
Might you need to define delimiter first before executing a command, and make sure you dropped an already created trigger (or just use if exists clause) try this
-- sync shadow with users
delimiter $$
drop trigger if exists users_post_insert;$$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end;$$
delimiter ;
Testing shows mysql_execute cannot accept the DELIMITER command.
To make it work break up your file into chunks, leaving out the DELIMITER lines altogether. For example,
$procs = file_get_contents( '../sql/security.sql' );
$procArr = preg_split( "/delimiter .*\n/", $procs );
Also, remove the set delimiter "$$" from the remaining chunks.
Feed each non-empty chunk into mysql_execute - it's ugly, but it works.
whats wrong with my syntax?
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = jos_dhruprofile.department
WHERE jos_users.id = jos_dhruprofile.uid
END
The syntax should be as follows:
DELIMITER $$ /* if you're not using an editor, you must change the delimiter*/
CREATE
TRIGGER ai_jos_dhruprofile_each
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid; /*<<<--- ; after every stament */
END $$ /* changed delimiter after the end */
DELIMITER ; /*make sure you set the delimiter back to the default*/
Note on the naming scheme for triggers
I'd recommend naming your trigger ai (meaning after insert) so you know when it fires on which table, rather than a meaningless name like: db_dhruniversity.trigger1.
I always use [a/b]+[d/i/u]_tablename_each as the triggername, that way I always know when the triggers fires (before/after) for which event (insert/delete/update) and on which table.
It's also good practise to document that the trigger fires on each row, hence the each on the end of the trigger name.
Note that MySQL does not support triggers that fire once per statement yet (But that might change in future).
There are no delimiters in it:
DELIMITER ||
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid;
END ||
DELIMITER;