MySQL Trigger Syntax Error union two/three tables - mysql

I wrote following code:
create trigger money after update on `things`
for each row
begin
Select #c1=sum(`thing_cost`) from `things`
UNION
Select #c2=sum(`salary`) from `dude_base`
Update `current` set `curr_cash`=#c1*#c2/100
end;
$$
Table "things" has got:
id1 (PK)
name
thing_cost
Table dude_base has got:
id2 (PK)
salary
name, etc. irrevelant
Table current has got:
id1 (FK)
id2(FK)
curr_cash
I got following error:
#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 'Update `current` set `curr_cash`=#c1*#c2/100; END' at line 7
Any help?

I think you should have a semicolon ; like
Select #c2=sum(`salary`) from `dude_base`;
to end this query because UPDATE is another query
// edit
try it like this
SET #c1 = (SELECT sum(`thing_cost`) from `things`);
SET #c2 = (SELECT sum(`salary`) from `dude_base`);
UPDATE `current` SET `curr_cash` = #c1 * #c2 / 100

Related

SQL Trigger solve

CREATE TRIGGER Print
Before UPDATE ON employ
FOR EACH ROW
WHEN (NEW.Employe_ID>0)
DECLARE
salary int;
BEGIN
salary:= :NEW.salary-:OLD.salary;
dbms_output.put('Old salary:'||:OLD.salary);
dbms_output.put('New salary:'||:NEW.salary);
dbms_output.put_line('Difference'||salary);
END;
/
Shows
error 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 'WHEN (NEW.Employe_ID>0)
DECLARE
salary int' at line 4
I have a table name employe which has 4 columns Employe_ID, E_name,
Department_ID,
salary
What is the problem with this sql and how can I solve it?
I don't get why you added the condition on employee_id > 0, but in case you actually need it
delimiter //
CREATE OR REPLACE TRIGGER print BEFORE UPDATE ON employee
FOR EACH ROW
IF ( NEW.employe_id > 0 ) THEN
SELECT NEW.salary - OLD.salary INTO #delta;
END IF;
//
delimiter ;
Insert and update your data, then read #delta
insert into employee select 1,'Me',1,2400;
update employee set salary = 2800 where id = 1;
Select #delta;
I don't think it's possible to automatically output #delta to the screen as it would be on Oracle using dbms_output.put_line().

Issue with stored procedure and COUNT IF statement

So I need to make a stored procedure called AddComment, that will add a comment to my Comments table , then add an entry into the Commenters table if that commenters name does not exist.
DELIMITER //
CREATE PROCEDURE AddComment(Name VARCHAR(60), Title VARCHAR(60), Comments VARCHAR(60))
BEGIN
INSERT INTO Comments(Name, Title, Comments)
VALUES (Name, Title, Comments);
DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
WHERE Name = Name;
IF name_count = 0
THEN INSERT INTO Commenters(Name)
VAlUES(Name);
ELSEIF name_count = 1
THEN INSERT IGNORE Commenters(Name)
VALUES(Name);
END IF;
END;
//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version fo
r the right syntax to use near 'DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
^ Is the error I'm getting. I keep tweaking my code trying to figure it out, but nothing is working.
You need a cursor to use syntax like that, but you can use SET:
SET name_count = (SELECT COUNT(*) ...);
Or better yet, since you don't actually use the value, eliminate the variable entirely:
IF NOT EXISTS (SELECT * FROM ...) THEN

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

Mysql sql error #1064

check the manual that corresponds to your MySQL server version for the right syntax to use near: 'SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed' at line 2
my code:
DROP TRIGGER IF EXISTS newsmonitoringrawdata.TNTInsertionTrigger;
CREATE TRIGGER TgtInsertionTrigger BEFORE INSERT ON tnews
FOR EACH ROW BEGIN
SET #isRecordCreated =
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
IF #isRecordCreated>0 THEN
UPDATE capacity
SET MsgNumber=MsgNumber+1,
size=size+NEW.MSize
WHERE
server=NEW.server
AND
feed=NEW.feed
and
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
ELSE
INSERT INTO capacity(server, feed, streamdt, msgNumber, size)
VALUES
(
NEW.server,
NEW.feed,
date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i:00'),
1,
NEW.size
);
END IF;
END
SET #isRecordCreated = (
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i')
);
You have to add parantheses () around the query.

Mysql stored procedure syntax

I am trying to write simple mysql stored procedure and it seems that I can't get it right, so far I have
delimiter //
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
begin
declare dbArtist varchar(50);
delcare dbRecord varchar(50);
set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));
set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
begin
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
end
if not exists (select * from Records as R inner join Artists as A on R.ArtistId=A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) then
begin
INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ((select artistid from artists where artistname=dbArtist),_record);
set dbRecord=_record;
end
end
but I get syntax error in line 4:
#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 'dbRecord varchar(50);
set dbArtist = (select artistname from artists where lowe' at line 4
this message error was returned to me by phpMyAdmin, can anyone tell me why do I get an error?
edit: modified version, still not good
delimiter //
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
begin
declare dbArtist varchar(50);
declare dbRecord varchar(50);
set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));
set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
begin
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
end
if not exists
(select * from Records as R inner join Artists as A on R.ArtistId = A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist)
then
begin
INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ( (select artistid from artists where artistname=dbArtist) ,_record);
set dbRecord=_record;
end
end
now error in line 14 and 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 'if not exists (select * from Records as R inner join Artists as A on R.ArtistId' at line 14
The problem is that you misspelled DECLARE:
delcare dbRecord varchar(50);
UPDATE: For your next error, the problem is your illegal use of NOT EXISTS.
Within a stored procedure the proper approach is to count the existing rows, and then conditionally insert a value if the count is 0.
Something like this:
SELECT COUNT(*)
INTO #v_row_count
FROM Artists
WHERE LOWER(artistname)=LOWER(_artist);
IF (#v_row_count = 0)
THEN
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
END IF;
P.S. To avoid poor performance on on your select query, you should consider using a collation that is not case-sensitive so you don't need to apply the LOWER() function to the artistname column.