473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem in trigger

254 100+
Hi

I am new to Trigger. I write trigger to insert total no.of row of a table to another table in this way

CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW BEGIN
INSERT INTO row_count_table (after_insert) (SELECT COUNT(*) FROM main_table);

I don't know what is the wrong in this statement while I tried to end this statement with END; also.
Feb 20 '08 #1
10 8350
r035198x
13,262 8TB
Hi

I am new to Trigger. I write trigger to insert total no.of row of a table to another table in this way

CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW BEGIN
INSERT INTO row_count_table (after_insert) (SELECT COUNT(*) FROM main_table);

I don't know what is the wrong in this statement while I tried to end this statement with END; also.

There you go.
Feb 20 '08 #2
chaarmann
785 Expert 512MB
Try this:

Expand|Select|Wrap|Line Numbers
  1.  DELIMITER $
  2. CREATE TRIGGER count_row 
  3. AFTER INSERT ON main_table
  4. FOR EACH ROW INSERT INTO row_count_table SELECT COUNT(*) FROM main_table;
  5. $
Feb 20 '08 #3
mukeshrasm
254 100+
Try this:

DELIMITER $$
CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW INSERT INTO row_count_table SELECT COUNT(*) FROM main_table;
$$
I tried this as but I got this error message:

#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 '' at line 4
Feb 20 '08 #4
chaarmann
785 Expert 512MB
I tried this as but I got this error message:

#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 '' at line 4
What editor are you using to run the SQL?
And why is it saying '"' in the error message? There is no double quotation mark inside the SQL! Did you add some somewhere?
Feb 23 '08 #5
mukeshrasm
254 100+
I am using PhpMyAdmin to run the SQL. And there is no double quote in the sql but why it is giving such error that I don't know. So if you can help me and try to run this query on your own and find whether it is working or not. Ok




What editor are you using to run the SQL?
And why is it saying '"' in the error message? There is no double quotation mark inside the SQL! Did you add some somewhere?
Feb 23 '08 #6
chaarmann
785 Expert 512MB
I am using PhpMyAdmin to run the SQL. And there is no double quote in the sql but why it is giving such error that I don't know. So if you can help me and try to run this query on your own and find whether it is working or not. Ok
My query is running fine. I just copied it again from this forum and run it, just to verify there was no mistyping by accident.
I don't know about PhpAdmin, maybe it's PhpAdmin that is doing something weird with the query when sending it to mySql, like special character replacement.
I am using SQLYog. It's free. And it supports triggers. Give this program a try and see if it runs the trigger-SQL without error. I was also creating triggers with Java using JDBC and it was running fine. So if you have still problems even after using SQLYog, which supports you in a way that it writes a full trigger template for you automatically after you click a button on a table, then there is something wrong with your mySql database.
Are you sure that you are accessing the correct mySql instance? In the beginning I installed mySql 4.2 (which cannot handle triggers) and then I installed mySql 5.0.41 (which can handle triggers), but the mySql 4.2 instance was still running and I was accessing it in one of my programs unintentional, so triggers did not run, and no data seemed to be updated by normal queries. Only after I deinstalled the 4.2 instance manually, my program was automatically connecting to mySql 5.0.41 instance and then the trigger creation worked.
Feb 24 '08 #7
mukeshrasm
254 100+
My query is running fine. I just copied it again from this forum and run it, just to verify there was no mistyping by accident.
I don't know about PhpAdmin, maybe it's PhpAdmin that is doing something weird with the query when sending it to mySql, like special character replacement.
I am using SQLYog. It's free. And it supports triggers. Give this program a try and see if it runs the trigger-SQL without error. I was also creating triggers with Java using JDBC and it was running fine. So if you have still problems even after using SQLYog, which supports you in a way that it writes a full trigger template for you automatically after you click a button on a table, then there is something wrong with your mySql database.
Are you sure that you are accessing the correct mySql instance? In the beginning I installed mySql 4.2 (which cannot handle triggers) and then I installed mySql 5.0.41 (which can handle triggers), but the mySql 4.2 instance was still running and I was accessing it in one of my programs unintentional, so triggers did not run, and no data seemed to be updated by normal queries. Only after I deinstalled the 4.2 instance manually, my program was automatically connecting to mySql 5.0.41 instance and then the trigger creation worked.
I am using MySql 5.0.45 instance and I was able to run the query on PhpMyAdmin. For that I had to remove the Key Word BEGIN and DELIMITER $.
Now it works fine. And I will use SQLYog as well. '
Feb 26 '08 #8
chaarmann
785 Expert 512MB
I am using MySql 5.0.45 instance and I was able to run the query on PhpMyAdmin. For that I had to remove the Key Word BEGIN and DELIMITER $.
Now it works fine. And I will use SQLYog as well. '
I am glad you solved the issue.

Afterthoughts:
Strange - there is no word "BEGIN" written in the SQL I listed.
I suggest you copy the SQL exactly as it is next time. I mean by using copy-and-paste, and not by trying to modify your original SQL. You are only lucky that there was no other error inside, for example an invisible character. Microsoft Frontpage once inserted an invisible 0xFF character in a JSP-page of a friend, and when he tried to run the SQL inside, it always crashed with a syntax error. It nearly drove us nuts. Because it looked like "insert into", but there actually was written "insert in(0xFFF)to" when we analyzed with a hex-editor.

The "DELIMITER" is only useful if you use a frontend like SQLYog, which otherwise would stop at the first ";" detected and not send the rest of the SQL over. So you should not use it from inside your PHP or JSP.

The "BEGIN" must be closed by an "END;" and should be used if you have more than one SQL that you need to run as a trigger event. It encloses them.
Feb 26 '08 #9
crs27
40
Im getting the above same error.
The syntax for the trigger is same as above.Checked with delimiter begin and end.
The same trigger is working fine ON Windows Mysql Query Browser 1.2.12
but when i use the same on Linux EL4 its says syntax error 1064.

Awaiting for reply.
Thanks in advance
Dec 4 '08 #10
I also get that error too. The solutions which perhaps will help you are :

1. Try to look up your trigger which have been created. use command : SHOW TRIGGERS ;

2. If i'm not wrong, your trigger has been created and you can see it there.
try to drop the trigger if the trigger has been created.

3. Try to compile it again ...


and the complete code is :

DELIMITER $$

CREATE DEFINER=`root`@`localhost` TRIGGER `trig_unit_log`
BEFORE INSERT ON tbl_unit
FOR EACH ROW
BEGIN

DECLARE upd_time DATETIME;
DECLARE upd_reason VARCHAR(45);

SET upd_time = CURDATE();
SET upd_reason = 'INSERT';

INSERT INTO syslogfiles13 (UnitUID, UnitID, UnitName, Moderator, Publish, Waktu, Keterangan)
VALUES (NEW.UnitUID, NEW.UnitID, NEW.UnitName, NEW.Moderator, NEW.Publish, upd_time, upd_reason);

END;

$$
Nov 8 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dunc | last post by:
I'm new to Postgres, and getting nowhere with a PL/Perl trigger that I'm trying to write - hopefully, someone can give me some insight into what I'm doing wrong. My trigger is designed to reformat...
4
by: Hank | last post by:
I have two SQL Server 2000 machines (server_A and server_B). I've used sp_addlinkedserver to link them both, the link seems to behave fine. I can execute remote queries and do all types of neat...
1
by: BUSHII | last post by:
I have little problem and I dont have any idea how to make my trigger. I have table MyTable where I have many column with almost same name and same type (Grp1,Grp2,Grp3,Grp4...Grp50 char(1))....
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
2
by: gustavo_randich | last post by:
Hi :-) I'm porting a project from Oracle to DB2 and now I'm trying to avoid error SQL0746N in a trigger which reads the same table in which the trigger is defined. Below is Oracle's...
4
by: SUKRU | last post by:
Hello everybody. Unfortunately I am pretty new to sql-server 2000 I need some help with a Trigger I created. I created a trigger witch takes the id of the affected row and does a update on a...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
2
by: dean.cochrane | last post by:
I have inherited a large application. I have a table which contains a hierarchy, like this CREATE TABLE sample_table( sample_id int NOT NULL parent_sample_id int NOT NULL ....lots of other...
3
by: anuja pokharel | last post by:
hello, i have a problem in trigger. My trigger is CREATE OR REPLACE TRIGGER duplicate_deptno BEFORE INSERT OR UPDATE OF deptno ON DEPT1 REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.