Unix Technical Forum

Mysql Triggers

This is a discussion on Mysql Triggers within the MySQL forums, part of the Database Server Software category; --> Can someone explain why this will not work, or how I can make it work? I am trying to ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:29 AM
nospam-pcartier@atlashosting.com
 
Posts: n/a
Default Mysql Triggers

Can someone explain why this will not work, or how I can make it work?

I am trying to set a column name as a variable, but MYSQL will display:
ERROR 1054 (42S22): Unknown column 'Col' in 'field list'

So the variable Col is not being set somehow, is there another way to do
this?

Thanks everyone!
-Paul C

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:29 AM
nospam-pcartier@atlashosting.com
 
Posts: n/a
Default Re: Mysql Triggers

Sorry, forgot to list the trigger duh..

delimiter //
CREATE TRIGGER mytrigger
AFTER INSERT ON test
FOR EACH ROW

BEGIN

DECLARE Col CHAR(55);

IF NEW.type = 0 THEN
SET Col = 'news';
ELSE
SET Col = 'pictures';

END iF;


UPDATE trigger_test SET Col = Col + 1 WHERE `userid` = NEW.id;

END;//
delimiter ;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:29 AM
ZeldorBlat
 
Posts: n/a
Default Re: Mysql Triggers

On Oct 30, 11:51 am, nospam-pcart...@atlashosting.com wrote:
> Sorry, forgot to list the trigger duh..
>
> delimiter //
> CREATE TRIGGER mytrigger
> AFTER INSERT ON test
> FOR EACH ROW
>
> BEGIN
>
> DECLARE Col CHAR(55);
>
> IF NEW.type = 0 THEN
> SET Col = 'news';
> ELSE
> SET Col = 'pictures';
>
> END iF;
>
> UPDATE trigger_test SET Col = Col + 1 WHERE `userid` = NEW.id;
>
> END;//
> delimiter ;


You can't use dynamic column names that way. Try something like this
instead:

IF NEW.type = 0 THEN
UPDATE trigger_test SET news = news + 1 WHERE `userid` = NEW.id;
ELSE
UPDATE trigger_test SET pitcures = pictures + 1 WHERE `userid` =
NEW.id;
END IF;


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:29 AM
nospam-pcartier@atlashosting.com
 
Posts: n/a
Default Re: Mysql Triggers

Thank you, I Had a feeling that was the case.

-Paul

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:50 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com