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.
Related
I've written a simple stored procedure which uses a scalar function for 4 of the selected data.
I pass an ID which is part of the hole select as parameter of this function.
And finally that stored procedure is matching if ids are IN a Temp_Table I've created at the beginning of the stored procedure.
Problem : the stored procedure is running into an infinite loop.
I know the problem surely comes from the multiple parameter passe as Id in the function but I don't get the logic here !
I tried testing seperately and got the results as expected.
Here is the stored procedure:
ALTER PROCEDURE GetBordereauCotisations
#annee INT,
#sectionId INT
AS
BEGIN
SET NOCOUNT ON;
--Creation table temporaire pour stocker les adhérents d'une section
IF OBJECT_ID('tempdb.dbo.#table_adherent_ids', 'U') IS NOT NULL
DROP TABLE #table_adherent_ids;
CREATE TABLE #table_adherent_ids (AdherentId INT);
BEGIN
INSERT INTO #table_adherent_ids
SELECT p.RessourceId
FROM Personnes p
INNER JOIN PersonneAdherentSituations ps ON ps.RessourceId = p.RessourceId
WHERE p.SectionId = #sectionId AND ps.StatutAdhesion = 880
END
SELECT DISTINCT
pas.NumeroAdherent AS NumAdherent,
CONCAT(ppi.Nom, ' ', ppi.Prenom) AS NomPrenom,
si.Numero AS CodeSection,
'CodeTarif?' AS CodeTarif,
dbo.GetCodeTarifByTrimestrePaye(pas.RessourceId, #annee, 1) AS T1,
dbo.GetCodeTarifByTrimestrePaye(pas.RessourceId, #annee, 2) AS T2,
dbo.GetCodeTarifByTrimestrePaye(pas.RessourceId, #annee, 3) AS T3,
dbo.GetCodeTarifByTrimestrePaye(pas.RessourceId, #annee, 4) AS T4
FROM
PersonneAdherentSituations pas
INNER JOIN
PersonnePersoIdentites ppi ON pas.RessourceId = ppi.RessourceId
INNER JOIN
SectionIdentifications si ON si.RessourceId = #sectionId
INNER JOIN
PersonneAdherentFinancesVersements pfv ON pfv.SectionNumero = si.Numero
WHERE
YEAR(pfv.[Date]) = #annee
GROUP BY
pas.NumeroAdherent, ppi.Nom, ppi.Prenom, si.Numero, pas.RessourceId
HAVING
pas.RessourceId IN (SELECT tai.AdherentId FROM #table_adherent_ids tai)
BEGIN
DROP TABLE #table_adherent_ids;
END
END
GO
Here is the function:
ALTER FUNCTION GetCodeTarifByTrimestrePaye
(#adhrentId INT,
#annee INT,
#trimestre INT)
RETURNS NVARCHAR(MAX)
AS
BEGIN
-- Declare the return variable here
DECLARE #codeTarif nvarchar (MAX);
DECLARE #rowsSelected int = (SELECT CASE
WHEN #trimestre = 1 THEN 1
ELSE #trimestre - 1 END)
DECLARE #apayerAnnee int = (SELECT MAX(CONVERT(int, SUBSTRING(pfv.Apayer, 1, 4)))
FROM PersonneAdherentFinancesVersements pfv
WHERE YEAR(pfv.[Date]) = #annee
AND pfv.RessourceId = #adhrentId)
DECLARE #apayerTrimestre int = (SELECT MAX(CONVERT(int, SUBSTRING(pfv.Apayer, 7, 1)))
FROM PersonneAdherentFinancesVersements pfv
WHERE YEAR(pfv.[Date]) = #annee
AND pfv.RessourceId = #adhrentId)
SET #codeTarif = (SELECT CASE
WHEN #annee < #apayerAnnee
THEN l.Texte
WHEN #annee = #apayerAnnee AND #trimestre <= #apayerTrimestre
THEN l.Texte
ELSE
''
END
FROM PersonneAdherentFinancesVersements pfv
INNER JOIN ListeStatique l ON l.Id = pfv.CodeTarifId
WHERE pfv.IsValidated = 1
AND pfv.ValideParFede = 1
AND pfv.RessourceId = #adhrentId
ORDER BY l.Texte
OFFSET #rowsSelected ROWS
FETCH NEXT 1 ROWS ONLY)
RETURN #codeTarif;
END
GO
How could I obtain only one expected result using my scalar function?
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
$$
I've created the following stored procedure:
CREATE PROCEDURE `sp_DBR_Subbie_Cert_Input`(inPlot_ID varchar(25), inSubbie varchar(25), inCertDate date, inCertDetails varchar(50), inCertGross float ) BEGIN
SELECT inPlot_ID as 'Plot[Unit]', inSubbie as 'Subcontractor[inSub_ID]', inCertDate as 'Date[inC_Date]', inCertDetails as 'Details[inC_Det]', inCertGross as 'Gross[inC_Gross]', a.tblCert_Number as 'Prev. Cert#', a.tblCert_Gross as 'Prev Gross[inC_Prev]'
FROM hilmark.tblcertificates_j a
JOIN
(SELECT max(tblcertificates_j.tblCert_ID) as MaxCertID,
tblcertificates_j.tblCert_XID456 as MaxSubbie, tblcertificates_j.tblCert_XIDJob as MaxPlot
FROM hilmark.tblcertificates_j
GROUP BY tblcertificates_j.tblCert_XIDJob) x
WHERE x.MaxCertID=a.tblCert_ID AND x.MaxPlot=inPlot_ID AND x.MaxSubbie=inSubbie;
END $$
What I am trying to achieve is to allow a user to enter a new invoice for a supplier for a site. The query retrieves the last invoice for that supplier and that site. This works great except when this is the first invoice for that supplier on that site - nothing is returned. What I really need is a left outer join but in my code if I substitute the join with a left join I get a syntax error.
Where am I going wrong here?
I declared a variable called Max_CerT_ID and set it's value to the Cert_ID from the table or to zero if it's a completely new supplier. I then use this variable in case statements.
DROP PROCEDURE IF EXISTS sp_DBR_Subbie_Cert_Input
$$
CREATE PROCEDURE `sp_DBR_Subbie_Cert_Input`(inPlot_ID varchar(25), inSubbie varchar(25), inCertDate date, inCertDetails varchar(50), inCertGross float )
BEGIN
declare Max_Cert_Id int(11);
set Max_Cert_Id=0;
select max(y.tblCert_ID) into Max_Cert_ID from
hilmark.tblcertificates_j y
where y.tblCert_XIDJob=inPlot_ID and y.tblCert_XID456=inSubbie
group by y.tblCert_XIDJob, y.tblCert_XID456 ;
select 'dbr.colstyle',6,'%0.0F';
select 'dbr.colstyle',7,'%0.0F';
select 'dbr.colstyle',8,'%0.0F';
select inPlot_ID as 'Plot[Unit]',
inSubbie as 'Subcontractor[inSub_ID]',
inCertDate as 'Date[inC_Date]',
case
when Max_Cert_Id=0 then 1
else (select a.tblCert_Number from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)+1
end as 'Cert#',
upper(inCertDetails) as 'Details[inC_Det]',
inCertGross as 'Gross[inC_Gross]',
case
when Max_Cert_Id=0 then 0
else (select a.tblCert_Gross from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)
end as 'Prev. Gross',
case
when Max_Cert_Id=0 then inCertGross
else inCertGross-(select a.tblCert_Gross from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)
end as 'Change in WIP';
END
$$
Try writing the query as:
SELECT inPlot_ID as `Plot[Unit]`, inSubbie as `Subcontractor[inSub_ID]`,
inCertDate as `Date[inC_Date]`, inCertDetails as `Details[inC_Det]`,
inCertGross as `Gross[inC_Gross]`, c.tblCert_Number as `Prev. Cert#`,
c.tblCert_Gross as `Prev Gross[inC_Prev]`
FROM hilmark.tblcertificates_j c LEFT JOIN
(SELECT max(c.tblCert_ID) as MaxCertID, c.tblCert_XID456 as MaxSubbie,
c.tblCert_XIDJob as MaxPlot
FROM hilmark.tblcertificates_j c
GROUP BY c.tblCert_XIDJob
) cmax
WHERE cmax.MaxCertID = c.tblCert_ID AND cmax.MaxPlot = c.inPlot_ID AND cmax.MaxSubbie = c.inSubbie;
Some notes:
I doubt this really does what you want, because MaxSubbie has an indeterminate value. But you save the query works. Why do you need both comparisons?
When using table aliases, make them abbreviations for the table so the query is easier to read.
Only use single quotes for string and date constants.
DELIMITER //
DROP PROCEDURE IF EXISTS salary_ref//# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
CREATE PROCEDURE salary_ref(con int(11),IN id varchar(120),maxval int(11),minval int(11) , taxo int(11))
BEGIN
DECLARE s VARCHAR(50);
IF con = 1 THEN
SELECT `i` . * , `taxo`.`id` , `t`.`item_id` AS id, `u`.`name` AS user_name, `t`.`value` AS val
FROM (
`taxonomy` AS taxo
)
JOIN `item_taxo` AS t ON `t`.`taxo_id` = `taxo`.`id`
INNER JOIN `items` AS i ON `i`.`id` = `t`.`item_id`
INNER JOIN `users` AS u ON `u`.`id` = `i`.`created_by`
WHERE `t`.`value`
BETWEEN minval
AND maxval
AND `t`.`taxo_id` = taxo
AND `i`.`parent_tag_id` in (id);
ELSE
SELECT `i` . * , `taxo`.`id` , `t`.`item_id` AS id, `u`.`name` AS user_name, `t`.`value` AS val
FROM (
`taxonomy` AS taxo
)
JOIN `item_taxo` AS t ON `t`.`taxo_id` = `taxo`.`id`
INNER JOIN `items` AS i ON `i`.`id` = `t`.`item_id`
INNER JOIN `users` AS u ON `u`.`id` = `i`.`created_by`
WHERE `t`.`value`
BETWEEN minval
AND maxval
AND `t`.`taxo_id` = taxo
AND `i`.`parent_tag_id` in (id);
END IF;
END;
//# MySQL returned an empty result set (i.e. zero rows).
DELIMITER ;
//calling of that
call salary_ref(2,"\'36\',\'50\',\'57\'",17000000,0,7)
this is not work for me.
Following changes would solve the issues.
Change 1:
I suggest to not use same parameter names for stored procedure to represent column names of tables.
Unless you handle them properly there would arise an identifier conflict but does not seem to be there.
Change procedure signature as follows:
CREATE PROCEDURE salary_ref(
_con int(11), IN csv_id_values varchar(120),
_maxval int(11), _minval int(11), _taxo int(11)
)
Change 2:
You are trying to pass comma separated values for id field to use in where clause.
But using escape character won't solve your problem and that is not correct way of using input values.
call salary_ref( 2, '36,50,57', 17000000, 0, 7 )
The CSV value '36,50,57' can be used as is for where clause.
See the suggested Change below.
Change 3:
You can use FIND_IN_SET on CSV values instead of IN in the WHERE clause.
WHERE `t`.`value` BETWEEN _minval AND _maxval
AND `t`.`taxo_id` = _taxo
AND FIND_iN_SET( `i`.`parent_tag_id`, csv_id_values );
And, I think your procedure body is incomplete. Your IF con and ELSE are using the same SELECT statement. It is redundant, unless you change it.
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.