SQL says I have syntax error near '' at line 3. Please help
My objective is to calculate the sum of medications' prices
Here is my SQL code from phpmyadmin:
CREATE PROCEDURE spMEDICATION_FEE(IN PatientID CHAR(9))
BEGIN
DECLARE Sum2 INT;
DECLARE Sum3 INT;
SET Sum2 = (
SELECT SUM(MPRICE)
FROM USES_EXAM INNER JOIN MEDICATION ON USES_EXAM.MID = MEDICATION.MID
WHERE PID_OUT = PatientID);
SET Sum3 = (
SELECT SUM(MPRICE)
FROM USES_TREAT INNER JOIN MEDICATION ON USES_TREAT.MID = MEDICATION.MID
WHERE PID_IN = PatientID);
IF Sum2 IS NULL THEN
SET Sum2 = 0;
IF Sum3 IS NULL THEN
SET Sum3 = 0;
SELECT Sum2+ Sum3 AS 'Total fee';
END
If you are using SQL tab of phpymadmin to run this query, you should define a delimiter instead of ;. For example, try this:
DELIMITER $$
paste your create procedure code here
$$
Related
I was creating a procedure and I came across this.
Does anyone know how to solve this?
delimiter $$
create procedure sp_cadastraAluno(
in nome varchar(150),
in chamada varchar(3),
in data date,
in ra varchar(12),
in turma varchar(50),
in cpf varchar(14))
begin
declare x int;
set x = (select * from tb_coordenador inner join tb_professor
on tb_coordenador.cd_coord = tb_professor.cd_coord
inner join prof_turma on
tb_professor.cd_prof = prof_turma.cd_prof
inner join tb_turma on
prof_turma.cd_turma = tb_turma.cd_turma
inner join tb_aluno on
tb_turma.cd_turma = tb_aluno.cd_turma where nm_turma = turma and tb_professor.cd_cpf = cpf);
insert into tb_aluno(nm_aluno, cd_chamada, dt_nascimento, cd_ra, cd_turma) values
(nome, chamada, data, ra, x);
end $$
I need to insert in tb_aluno and for this, I need to pull the code already inserted in tb_turma, but this error appears.
You can't set variable 'x' with multiple columns. try removing select * and replace with the column which you need.
I have two functions. The second function uses the output from the first function.
One is:
DELIMITER $$
DROP FUNCTION IF EXISTS fp_splitfactor;
CREATE FUNCTION fp_splitfactor_price (id CHAR(8), startdate DATE)
RETURNS FLOAT
BEGIN
DECLARE splitfactor FLOAT;
SELECT IFNULL(EXP(SUM(LOG(f.p_split_factor))),1) INTO splitfactor
FROM fp_v2_fp_basic_splits AS f
WHERE f.fsym_id = id AND f.p_split_date > startdate AND f.p_split_date < NOW();
RETURN splitfactor;
END$$
DELIMiTER ;
Second one is:
DELIMITER $$
DROP FUNCTION IF EXISTS fp_splitadjprice;
CREATE FUNCTION fp_splitadjprice (id CHAR(8), startdate DATE)
RETURNS FLOAT
BEGIN
DECLARE splitfactor FLOAT;
DECLARE splitadjprice FLOAT;
DECLARE spinofffactor FLOAT;
SET splitfactor = 1.0;
SELECT fp_splitfactor(id, startdate) INTO splitfactor;
SELECT (p_price * splitfactor) INTO splitadjprice
FROM fp_v2_fp_basic_prices
WHERE fsym_id = id AND p_date = startdate;
RETURN splitadjprice;
END$$
DELIMITER ;
I then try to exectute a query as the following:
SELECT
p.fsym_id,
b.p_co_sec_name_desc AS Company_Name,
b.region AS Region,
p.p_date,
p.p_price AS Unadjusted_Price,
fp_splitadjprice(p.fsym_id,p_date) AS Adjusted_Price
FROM
fp_v2_fp_basic_prices p
LEFT JOIN (
SELECT r2.region, b2.p_co_sec_name_desc, b2.fsym_id
FROM fp_v2_fp_sec_coverage b2
LEFT JOIN sym_v1_sym_region r2 ON b2.fsym_id = r2.fsym_id
WHERE r2.region = "EUR") b
ON b.fsym_id =p.fsym_id
So basically my query calls the second function, which then calls the first function in order to return a value to the query. The execution is extremely slow though, but I do not understand why that is the case?
I found out that the slow execution was entire due to MySQL workbench not handling large datasets well. Once I migrated everything to BigQuery on Google Cloud everything worked perfectly.
STAY AWAY FROM CALLING FUNCTIONS ON LARGE DATASETS IN MySQL Workbench!
I am working for a little film database with php and mysql. And i got a problem when I want to create a stored procedure, that the local variable don't work in the case i want to use it.
delimiter //
### showFilm###
create procedure showFilm( in id INTEGER )
BEGIN
DECLARE tmp_Value varchar(50);
IF ( id = 0) THEN SET tmp_Value = "";
ELSE SET tmp_Value = concat('where = ', id);
END IF;
select f.Titel, f.Jahr, f.Bewertung, f.altersfreigabe, f.Beschreibung, f.Trailer, f.Dateipfad, f.Titelbild, f.Speichermedium, r.Vorname, r.Nachname, r.Land, r.Bild
from film as f
inner join regisseur as r
on r.idRegisseur = f.idFilm tmp_Value;
END;
//
I have created stored procedure in MySQL in Window-7. it run successfully on windows. But when I switch to Ubuntu it gives error in the stored procedure. On windows I am using SQLyog for creating stored procedure. On Ubuntu, I run the SQL script and call that stored procedure but it gives error. Below is my stored procedure.
DELIMITER $$
USE `adserver`$$
DROP PROCEDURE IF EXISTS `getDaypartTimeDetail`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getDaypartTimeDetail`
(currentDate DATE,noOfdays INT,cityId BIGINT)
BEGIN
DECLARE i INT;
DECLARE dateCnt INT;
SET dateCnt = 0;
DROP TEMPORARY TABLE IF EXISTS OnlyDate;
DROP TEMPORARY TABLE IF EXISTS AdvScheduleData;
CREATE TEMPORARY TABLE OnlyDate(dday DATE);
CREATE TEMPORARY TABLE AdvScheduleData(dday BIGINT,daypartId INT,totalFile BIGINT,totalDur BIGINT);
/* Generate Dates */
WHILE(dateCnt < noOfdays) DO
SET i = 1;
INSERT INTO OnlyDate(dday) VALUES (DATE_ADD(currentDate, INTERVAL dateCnt DAY));
SET dateCnt = dateCnt + 1;
END WHILE;
/* Insert all dayparts for all dates */
INSERT INTO AdvScheduleData (dday, daypartID) SELECT (UNIX_TIMESTAMP(dday)*1000), id FROM OnlyDate, daypart;
/* Update total files and duration */
UPDATE AdvScheduleData SET
TotalFile = (SELECT COUNT(advt_id)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = AdvSch.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday),
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday);
SELECT * FROM AdvScheduleData;
END$$
DELIMITER ;
The output I get in Ubuntu is
mysql> call getDaypartTimeDetail('2012-08-13',5,30534); ERROR 1054
(42S22): Unknown column 'ADVSCH.advt_id' in 'on clause'
Table name and aliases are case sensitive in Ubuntu.
Hence, this reference is giving an error:
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
Change it to AdvSch.advt_id
I receive the following error. But I havent typed count in my stored procedure so why is it giving this error?
CALL updateproposalStatus(1,5) Error Code: 1136. Column count doesn't match value count at row 1
STORED PROCEDURE:
CREATE DEFINER=`root`#`localhost` PROCEDURE `updateProposalStatus`(IN decision INT, IN x INT)
BEGIN
DECLARE adv_id varchar(30);
DECLARE std_id varchar(30);
DECLARE topic varchar(255);
select
a.id INTO adv_id
from
rp_proposal p
inner join rp_adviser a on p.rp_adviser_id = a.id
where p.proposal_id=x;
select
s.id INTO std_id
from
rp_proposal p
inner join rp_student s on p.rp_student_id = s.id
where p.proposal_id=x;
select
p.title INTO topic
from
rp_proposal p
where p.proposal_id=x;
UPDATE rp_proposal_status
SET state_rp_controller =decision
WHERE rp_proposal_id = x;
IF decision = 1 THEN
INSERT INTO rp_indpstudy VALUES (topic,adv_id,std_id);
END IF;
END
It's the column-count (number of columns) that it is complaining about, not about a column named count.
Most likely culprit is the insert statement at the end - make sure that it is consistent with rp_indpstudy's schema.