Simple Trigger Error?! PL/SQL: ORA-00933: - php

I am attempting to create a trigger that will insert values into an audit table for approval by an admin user. The trigger will insert new values that are added into a consultant table into this audit table.
I have re-jigged the trigger a lot but cannot seem to bypass the compilation error! I assume it's something small?
DROP TABLE MyAuditTable;
CREATE TABLE MyAuditTable (
audit_id INTEGER NOT NULL,
new_name VARCHAR2 (30),
new_postcode VARCHAR2 (20),
status VARCHAR2 (15),
CONSTRAINT pk_MyAuditTable PRIMARY KEY ( audit_id )
);
DROP sequence MySeq;
Create sequence MySeq MINVALUE 1 MAXVALUE 9999999 INCREMENT BY 1 START WITH 1;
drop trigger MyTrigger;
create trigger MyTrigger
after insert on my_consultant_table
for each row
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
)
from my_consultant_table;
end;
/
ERROR: PL/SQL: ORA-00933:
So the audit table should now have the newly inputted data from the consultant table, which contains name and postcode attributes. Another trigger will fire so that when the status is changed, these changes are permitted.
Thank You!

The line
from my_consultant_table
Is unnecessary.
Should be:
insert into MyAuditTable values (MySeq.nextval, :new.con_name, :new.con_postcode, 'Pending');

This ORA-00933 denotes a syntax error in your trigger declaration :
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
)
from my_consultant_table;
end;
The trailing from my_consultant_table does not make sense, just remove it and you should be fine :
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
);
end;

Try this:
begin
insert into MyAuditTable
select
MySeq.nextval,
:new.con_name,
:new.con_postcode,
'Pending'
from dual;
end;
One Tip: When u create a sequence and always use from 1 to 9999999.... anc increment one by one.
Just use
create sequence owner.table nocache;

Related

Adding dynamical options to PHP populated Dropdown from SQLServer Database

need some input
i know how to populate a php dropdown list from MSSQLServer Datebase and save the selected value in the datebase, but i want to add new dynamical entrys in the dropdown menu and save them into the database. Here is the code:
some help?
I would recommend doing all this in the SQL level, try this
it is modified from a stored procedure I use in a similar fashion
Create Procedure [dbo].[MergeStatus](
#StatusID int,
#Status Varchar(8000)
)
AS
BEGIN
set nocount on;
Merge [Master.dbo.Status] as Target
using (select #StatusID As ID) As source
On
(Target.StatusID = source.ID)
When Matched Then
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID ) -- we have a match ID is Greater than 0 so insert into new tabke
When Not Matched Then
Insert (
#Status -- no match add new to Master.dbo.Status
)
Values(
#Status
);
IF #StatusID IS NULL or #StatusID = 0 -- our ID is not waht we expected so we need to insert
BEGIN
SET #Id = CAST(SCOPE_IDENTITY() as [int]);--getting ID od record we just inserted
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID )
END
END

MySQL Trigger that writes to multiples tables

I have a trigger that I cannot get to work properly.
I have at table called _WorkOrderDump where data is input via a form. There is a trigger on this table that inserts some of the data into a table called _RefTable. The second part of this trigger is to insert some of the data into another table called __XClient.
I cannot get the second insert into the __XClient table to work, it does not insert any data into this table. The first part of the trigger inserting into Ref table is however working perfectly.
What is wrong with my query?
Thanks for any help provided!
CREATE TRIGGER WorkOrderTrigger
AFTER INSERT on _WorkOrderDump
BEGIN
Declare EntryDate DATETIME;
Declare RefIDDump INT;
SET EntryDate = CURDATE();
Set RefIDDump = LAST_INSERT_ID();
CASE WHEN (EntryDate) > 0
THEN INSERT INTO `_RefTable`( `WOID`, `ClientID`, `ProjectScope`, `ProjectID`, `ClientRef`, `SiteID`, `SiteName`, `RegionID`, `SiteAddressLine`, `SiteAddressSuburb`, `SiteAddressState`, `SiteAddressPostcode`, `SiteAddressCountry`, `SiteContactName`, `SiteContactMobile`, `SiteContactPhone`, `SiteContactEmail`, `RefEntryDate`, `CreatedBY`)
VALUES
( new.WOID, new.ClientID, new.ProjectScope, new.ProjectID, new.ClientRef, new.SiteID, new.SiteName, new.RegionID, new.SiteAddressLine, new.SiteAddressSuburb, new.SiteAddressState, new.SiteAddressPostcode, new.SiteAddressCountry, new.SiteContactName, new.SiteContactMobile, new.SiteContactPhone, new.SiteContactEmail, EntryDate, new.CreatedBY);
WHEN (new.ClientID) = 1
THEN INSERT INTO `__XClient`( `RefID`, `ITContactName`, `ITContactPhone`, `ITContactEmail`, `XScope32`, `XScope43`, `XScopeMediaPlayer`, `XScopeInstallDate`, `XScopeInstallTime`, `XScopeComments`)
VALUES
( RefIDDump, new.Dump_1, new.Dump_3, new.Dump_2, new.Dump_4, new.Dump_5, new.Dump_6, new.Dump_7, new.Dump_8, new.Dump_9);
END CASE;
END

calling mysql procedure using codeigniter [Error: result consisted more than one row]

I have table status and status_stage . After insert in status table, I want to add it to the status_stage table. I have this procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `addStatus`(status VARCHAR(45), stage int (8))
BEGIN
DECLARE status_id INT DEFAULT NULL;
INSERT INTO status (status)
VALUES (status);
SELECT id INTO status_id FROM status WHERE status = status;
INSERT INTO stage_status (stage,status)
VALUES (stage,id);
END
And I call that function with this:
$result= $this->db->query("call addStatus('$status', $stage)");
return ($result->num_rows()>0);
When I tried it it gives me this Error:
Result consisted of more than one row
Your SELECT ... INTO statement got more than one result because of your WHERE clause with status = status, this equals forever
you need to change the param name to distinguish it from the table column name (eg. status_param)

How to Parse PHP Searilize data in Mysql Stored Procedure & Looping it

I have below data in Mysql Column (storing all data in serialize form - with comma separated into column) and i want to get/fetch this column data in Mysql stored procedure and want to loop for each data and insert into another trans table.
So if my data like below then i want to insert 7 record in trans table.
{"FormBuilderId":"5","vAnswer":"Develeop"},
{"FormBuilderId":"15","vAnswer":"Search Engine"},
{"FormBuilderId":"13","vAnswer":"10-15"},
{"FormBuilderId":"6","vAnswer":"Tester entered"},
{"FormBuilderId":"1","vAnswer":"Female"},
{"FormBuilderId":"14","vAnswer":"Moon.jpg"},
{"FormBuilderId":"12","vAnswer":"TV,dancing and modeling"}
My table structure & data is like below in table:
CREATE TABLE IF NOT EXISTS `idea_history` (
`iIdeaHistoryId` int(11) NOT NULL AUTO_INCREMENT,
`iApplicationId` tinyint(11) unsigned DEFAULT '0',
`tAnswerData` text COMMENT 'all answer data store here in json format',
`dAddedDate` datetime NOT NULL,
PRIMARY KEY (`iIdeaHistoryId`),
KEY `iApplicationId` (`iApplicationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='history comes here' AUTO_INCREMENT=57 ;
--
-- Dumping data for table `idea_history`
--
INSERT INTO idea_history (
iIdeaHistoryId,
iApplicationId,
tAnswerData,
dAddedDate
)
VALUES
(
53,
2,
'{"FormBuilderId" : "2","vAnswer":"Environmental Group"},{"FormBuilderId" : "11","vAnswer":"Satelite"},{"FormBuilderId" : "3","vAnswer":"HB"},{"FormBuilderId" : "4","vAnswer":"Dev"},{"FormBuilderId" : "7","vAnswer":"HB"},{"FormBuilderId" : "8","vAnswer":"Balaji Satellite"},{"FormBuilderId" : "10","vAnswer":""}',
'2014-07-05 19:20:56'
),
(
54,
1,
'{"FormBuilderId":"5","vAnswer":"Hello krishna|kanth double"},{"FormBuilderId":"15","vAnswer":"Website"},{"FormBuilderId":"6","vAnswer":"need to check"},{"FormBuilderId":"13","vAnswer":"20-25"}',
'2014-07-05 19:20:56'
),
(
55,
2,
'{"FormBuilderId":"11","vAnswer":"comapnay"},{"FormBuilderId":"8","vAnswer":"here am|chw "},{"FormBuilderId" : "10","vAnswer":""},{"FormBuilderId":"9","vAnswer":"Business"}',
'2014-07-05 19:20:56'
) ;
I will pass iIdeaHistoryId in stored procedure and it will fetch value of tAnswerData field and part this value and insert into another trans table.
Could you please guide what i have to change in stored procedure ?
you can do it like this
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1) INTO arg_1;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1) INTO arg_2;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1) INTO arg_3;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1) INTO arg_4;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
or if you want to do it in store procedure add your data here
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans()
BEGIN
DECLARE arg_1,arg_2,arg_3,arg_4 VARCHAR(100);
DECLARE finished INTEGER DEFAULT 0;
DECLARE cursor_name CURSOR FOR YOUR_SELECT_statement;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN cursor_name;
my_loop: LOOP
FETCH cursor_name INTO your_variable;
IF finished = 1 THEN
LEAVE my_loop;
END IF;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1)
INTO arg_1 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1)
INTO arg_2 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1)
INTO arg_3 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1)
INTO arg_4 FROM your_table;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
END LOOP my_loop;
CLOSE cursor_name;
END$$
DELIMITER ;
for one column you use this
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans(IN in_iIdeaHistoryId INT)
BEGIN
DECLARE arg_1 VARCHAR(100);
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(`tAnswerData`,' ',''),'{"',-1),'":"',1)
INTO arg_1 FROM `idea_history` WHERE iIdeaHistoryId=in_iIdeaHistoryId;
INSERT INTO trans VALUES (arg_1);
END$$
DELIMITER ;
and call it like this
CALL `trans`(53);
This looks like JSON. Use php function json_decode() to store the json data into an array and then you can use foreach loop on that array. Inside the loop you can place condition and when its met, you could use insert query to store it in your database.
Does this help you?
If you can explain a bit further what you're trying to do exactly and what are your condition, then i Can probably give you a better answer.

How to optimise this temporary table query?

I have written a stored procedure that takes comma separated value as input and another value. I traverse values from comma separated value and then run a query for each value. now I need to return result so I thought to store results in temporary table and then selected values from that temporary table.But it is taking too long.
for query with 163 comma separated values it is taking 7 seconds.
for query with 295 comma separated values it is taking 12 seconds.
Here is procedure:-
DELIMITER $
create procedure check_fbid_exists(IN myArrayOfValue TEXT, IN leaderID INT(11) )
BEGIN
DECLARE status TINYINT(1);
DECLARE value INT(11);
DECLARE pos int(11);
CREATE TEMPORARY TABLE fbid_exists_result (userID int(11), status tinyint(1));
WHILE (CHAR_LENGTH(myArrayOfValue) > 0)
DO
SET pos=LOCATE( ',', myArrayOfValue);
IF pos>0 THEN
SET value = LEFT( myArrayOfValue,pos-1 );
SET myArrayOfValue= SUBSTRING( myArrayOfValue,pos+1 );
ELSE
SET value = myArrayOfValue;
SET myArrayOfValue='';
END IF;
SELECT EXISTS(SELECT 1 FROM users_followings WHERE UserID=value and LeaderUserID=leaderID LIMIT 1) INTO status;
insert into fbid_exists_result VALUES(value,status);
END WHILE;
SELECT * FROM fbid_exists_result ;
DROP TEMPORARY TABLE IF EXISTS fbid_exists_result ;
END$

Categories