My trigger is failing to create and I cant understand why - mysql

I'm trying to create this trigger and I get this 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 'case where artcStackId = new.artcStackId' at line 7
Here is the failing trigger
SET new.artcPublicId =
case
when new.artcCountry = 'US' then concat(91,new.artcStackId)
when new.artcCountry = 'UK' then concat(92,new.artcStackId)
when new.artcCountry = 'CA' then concat(93,new.artcStackId)
else concat(11,new.artcStackId)
end case
where artcStackId = new.artcStackId
Here is the trigger that works
SET new.artcPublicId =
case
when new.artcCountry = 'US' then concat(91,new.artcStackId)
when new.artcCountry = 'UK' then concat(92,new.artcStackId)
when new.artcCountry = 'CA' then concat(93,new.artcStackId)
else concat(11,new.artcStackId)
end
when I add those extra bits, it'll fail.

There is no reference to your where condition.i think it should be like where new.artcStackId = new.artcStackId

Your first trigger has a syntax error:
end case
should be just
end
ie remove the trailing "case"
Edit:
I just noticed you have a where condition: where artcStackId = new.artcStackId.
You can't do that. You can only modify the current row, referenced by the new. syntax.
You must remove the where clause altogether, because you may only update the row being inserted/updated.

Related

SQL Field Update Syntax Issues with complex selection logic

The query below works fine and is what I need.
I want to add a new keyword onto "Keywords" e.g.
UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')
However no matter where I place this in the logic below, I get a syntax error.
The web examples I have seen and in Stackoverflow are for simple
Update ... WHERE .... examples.
SET #StartDate = '2016-03-01';
SET #EndDate = '2016-03-31';
SELECT
bugs_activity.bug_id,
bug.status_whiteboard AS Whiteboard,
bug.keywords AS Keywords,
bug.bug_status,
bug.resolution,
SUM(CASE WHEN fd.name = 'bug_status' AND (bugs_activity.added = 'VERIFIED' OR bugs_activity.added = 'CLOSED') THEN 1 ELSE 0 END) AS ClosedCount,
MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'VERIFIED' THEN bug_when ELSE NULL END) AS verifiedDate,
MIN(CASE WHEN fd.name = 'bug_status' AND bugs_activity.added = 'CLOSED' THEN bug_when ELSE NULL END) AS closedDate
FROM bugs_activity
INNER JOIN bugs bug
ON bugs_activity.bug_id = bug.bug_id
INNER JOIN fielddefs fd
ON bugs_activity.fieldid = fd.id
WHERE
(bugs_activity.bug_when BETWEEN '2015-09-01' AND #EndDate)
AND (Keywords LIKE '%Region:Europe%')
AND NOT (Keywords LIKE '%Report:DevProcess%')
GROUP BY bug_id
HAVING
ClosedCount > 0
AND (
(verifiedDate IS NOT NULL AND verifiedDate >= #StartDate)
OR (verifiedDate IS NULL AND (closedDate IS NOT NULL AND closedDate >= #StartDate))
)
Additional info from questions:
Linqpad SQL talking to MySQL DB
From linqpad - 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 'UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess')' at line 20
I removed GroupBy line, same error.
CONCAT is what a web search tells me How to prepend a string to a column value in MySQL?
In the selected "bugs" I want to "UPDATE bugs SET bug.keywords = CONCAT(bug.keywords, ', Report:DevProcess') "
Bugzilla has historically for a long time used multiple keywords. It is what it is whether good or bad.
== 31/05/2016 update ==
I simplified the query and got past the syntax error, however no update. I confirmed account had DB write access by using the read account which produced an access denied error.
-- this shows the one record
SELECT bug_id
FROM bugs
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')
-- this runs without an error but shows no records updated
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess')
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')
The simpler syntax does "work", i.e. no syntax error however the write did not work initially. This turned out to be because the keyword schema required the keyword to be pre-defined. Adding the keyword in resulting in the record being updated.
-- this runs without an error but shows no records updated
UPDATE bugs SET bugs.keywords = CONCAT(bugs.keywords, ', Report:DevProcess')
WHERE (bugs.bug_status = 'VERIFIED') AND (bugs.status_whiteboard LIKE '%Leiden%') and (bugs.keywords LIKE '%Region:Europe%') AND NOT (bugs.keywords LIKE '%Report:DevProcess%')

Cannnot get IF ELSE IF statement to work

I'm trying to get it so that when a user logs in, a trigger checks a specific column. If the column value is 'No', I want it to set it as 'Ask', and if it is already 'Ask', I want it to set it to 'Yes'. Nothing I try seems to work. Most recently I tried:
IF( new.eula_accepted = 'No' ) THEN SET new.eula_accepted = 'Ask';
else if( new.eula_accepted = 'Ask' ) THEN SET new.eula_accepted = 'Yes';
end if
The error it gives me is "MySQL said: #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 3". If I try just this:
IF( new.eula_accepted = 'No' ) THEN SET new.eula_accepted = 'Ask';
end if
THAT works just fine, but I really need it to do both. Any help would be appreciated, I'm getting frustrated beyond belief.
Your IF syntax is wrong. Use ELSEIF instead of ELSE IF:
IF new.eula_accepted = 'No' THEN SET new.eula_accepted = 'Ask';
ELSEIF new.eula_accepted = 'Ask' THEN SET new.eula_accepted = 'Yes';
END IF;

mySQL UPDATE failed. What is wrong with this mySQL UPDATE query?

here's the generated query:
UPDATE namelist
SET 'submitterName' = 'Jim'
,'actorName' = 'dingle'
,'setYear' = '1103'
,'country' = 'tanata'
,'blink' = 'on'
,'crush' = 'on'
,'initialize' = 'on'
,'entered' = 'on'
,'stuck' = 'on'
,'catapult' = 'on'
,'ruck' = 'on'
WHERE id = 31
it generates this (less than helpful) 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 (query snippet) at line 1
for the life of me, i can't spot where the problem is. yes, column names match, yes
TIA for helping out.
WR!
You have used single quotes before and after columns in the query, replace those single quotes with backquotes.
So the query like
UPDATE namelist
SET `submitterName`='Jim',
`actorName`='dingle',
`setYear`='1103',
`country`='tanata',
`blink`='on',
`crush`='on',
`initialize`='on',
`entered`='on',
`stuck`='on',
`catapult`='on',
`ruck`='on'
WHERE id=31;
user ` instead of '
like this
UPDATE namelist SET `submitterName`='Jim',`actorName`='dingle',`setYear`='1103',`country`='tanata',`blink`='on',`crush`='on',`initialize`='on',`entered`='on',`stuck`='on',`catapult`='on',`ruck`='on' WHERE id=31

Mysql script update with if statement and case

Why Can't I use or why does it encounters an error. I'm trying to use a script as such as the one below. I'm a newbie when it comes to mysql and I tried creating scripts like this but I get the errr like the below
#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 '1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.statu' at line 4
Here's my script
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount =
CASE WHEN dogdetails.dogcount IS NOT 1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.status = 'Checked'
WHEN dogdetails.dogcount = 1 THEN dogs.pto = pto - dogdetails.total, dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Is there somethng I'm missing or overlooked?
remember that you have to use the keyword 'end' after your case statements and to always return a value. The IS NOT keyword is also incorrect when comparing numbers. Here's a version of your query that should work:
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount = CASE WHEN dogdetails.dogcount != 1 THEN dogs.dogcount = dogcount-dogdetails.total else dogs.dogcount end,
dogs.pto= case WHEN dogdetails.dogcount = 1 THEN pto - dogdetails.total else pto end,
dogdetails.status = 'Checked'
WHERE dogdetails.id=4

mysql if then statement not working?

Am I blind, or what is wrong with my query?
select
STRCMP( message, 'LogMessage') = 1
from
LogEntries;
works fine. However
select
IF STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF
from
LogEntries;
returns:
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 'STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF from
LogEntries' at line 2
What is wrong with this statement?
You might want to use CASE WHEN:
SELECT
CASE WHEN STRCMP( message, 'LogMessage') = 1 THEN 'bla' END AS your_column
FROM
LogEntries;
when the condition is true it will return 'bla' otherwise, since there's no else part, it will return NULL.