MySQL Syntax Error in Declaration Temporary Variables - mysql

I'm working on a project and I need to pass some values to the table with stored procedure in MySQL. So I made my procedure like this.
create procedure KullanıcıKayıt(
IN kullaniciAd nvarchar(50),
IN kullaniciSoyad nvarchar(50),
IN kullaniciSifre int(10),
IN kullaniciMail nvarchar(50),
IN kullaniciTelNo int(10),
IN kullaniciAdres nvarchar(100),
IN kullaniciSehirAd nvarchar(50),
IN kullaniciSehirIlceAd nvarchar(50)
)
and these are the rest of my procedure
BEGIN
DECLARE sehirId INT DEFAULT 0;
DECLARE ilceId INT DEFAULT 0;
Select #sehirId=sehirId from sehir
where kullaniciSehirAd=sehirAd;
Select #ilceId=sehirIlceId from sehirIlce
where kullaniciSehirIlceAd=sehirIlceAd;
Insert Into kullanici(kullaniciAd, kullaniciSoyad, kullaniciSifre, kullaniciMail,
kullaniciTelNo, kullaniciAdres, kullaniciSehir, kullaniciSehirIlce) values (kullaniciAd,
kullaniciSoyad, kullaniciSifre, kullaniciMail, kullaniciTelNo, kullaniciAdres,
kullaniciSehir, kullaniciSehirIlce)
END
then I got three syntax errors. One of them is on the "0" after the sehirId
2nd of them on the DECLARE vefore ilceId
and the last one on the END.
What's wrong with my syntax?
Thanks.

You are missing a delimiter after END, add ; there as well as after your insert statement:
...
kullaniciSehir, kullaniciSehirIlce);
END;
If you are executing it in a client of some sort, you might need to add DELIMITER statements above and below your create proc statement:
DELIMITER |
create procedure KullanıcıKayıt(
...
END;
|
DELIMITER ;

Related

MySQL procedure with multi in parameters when you can pass only one

I have create MySQL procedure having multiple IN parameters. I want to call procedure with few parameters but when I leave other fields blank it shows this error:
DELIMITER $$
CREATE DEFINER=itzakeed_akeed#localhost PROCEDURE ApiKez(
IN Choice VARCHAR(100),
IN ValidKey VARCHAR(100),
IN azid INT(5),
IN amts FLOAT(50)
)
BEGIN
DECLARE GetKey VARCHAR(100);
DECLARE Balance FLOAT;
CASE WHEN Choice='KeyCheck' THEN
SELECT COUNT(id) INTO GetKey
FROM users
WHERE api_key=ValidKey;
if key is valid
IF GetKey=1 THEN
SELECT *
FROM users
WHERE key=ValidKey;
ELSE
SELECT 0;
END IF;
ELSE
SELECT "INVALID INPUT CHOICE";
END CASE;
END
$$
DELIMITER ;
No you cannot call a procedure with less parameters than what is required. However, you can pass null or empty strings and check if a parameter has a value from withing the stored procedure.

My procedure deletes all rows when I use delete statement

That's my table:
create table if not exists Ditte(nome varchar(30), luogo varchar(30), ZIP int);
That's my procedure:
delimiter //
create procedure deleteDitta(zip int)
begin
DECLARE newZIP int;
SET newZIP = zip;
DELETE from Ditte where ZIP = newZIP;
end;
//
delimiter ;
This is what I added in my table:
insert ignore into Ditte values ("Ditta1", "city1", 6828);
insert ignore into Ditte values ("Ditta2", "city2", 12345);
When I call my procedure deleteDitta and I put "12345" as parameter (like this: call deleteDitta(12345);), the procedure should deletes only the second row in table "Ditte", but it deletes all the contents of the table.
How can I fix it?
This seems to be MySQL getting confused about column names and variable names. Changing your procedure to this fixes the problem:
create procedure deleteDitta(dzip int)
begin
DELETE from Ditte where ZIP = dzip;
end;
Demo on dbfiddle

stored procedure in mysql returning null

I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//

what is the error of this stored procedure

hi i m using this stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS get_all_user_selected_children_of_preference$$
CREATE PROCEDURE get_all_user_selected_children_of_preference(
IN entity_id INT,
IN user_type INT,
IN parent_id INT
)
BEGIN
DECLARE profilepreferenceid INT;
DECLARE inpreferenceid INT;
DECLARE preference_parent_id INT;
DECLARE prefvalue VARCHAR(50);
DECLARE no_more_prefrences INT;
DECLARE element_type INT;
DECLARE preference_type INT;
DECLARE cur CURSOR for select ProfilePreferenceID,PreferenceParentID,PreferenceID,ProfilePreferenceValueAlias,ElementType,PreferenceType from xxxxx WHERE PreferenceParentID=parent_id AND EntityID=entity_id AND UserType=user_type ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_prefrences=1;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_user_selected_preference_children(
ProfilePreferenceID int(11) NOT NULL AUTO_INCREMENT,
PreferenceIDTmp INT(11),
PreferenceParentIDTmp INT(11),
ProfilePreferenceValueTmp varchar(255),
userProfileIdTmp INT(11),
elementTypeTmp INT (11),
PreferenceTypeTmp INT (11),
PRIMARY KEY (ProfilePreferenceID)
);
SET no_more_prefrences=0;
OPEN cur;
dept_loop:WHILE(no_more_prefrences=0) DO
FETCH cur INTO profilepreferenceid,preference_parent_id,inpreferenceid,prefvalue,element_type,preference_type;
IF no_more_prefrences=1 THEN
LEAVE dept_loop;
END IF;
INSERT INTO temp_user_selected_preference_children(ProfilePreferenceID,PreferenceIDTmp,PreferenceParentIDTmp,ProfilePreferenceValueTmp,userProfileIdTmp,elementTypeTmp,PreferenceTypeTmp)
VALUES (profilepreferenceid,inpreferenceid,preference_parent_id,prefvalue,entity_id,element_type,preference_type);
CALL get_all_user_selected_children_of_preference(entity_id,user_type,inpreferenceid);
END WHILE dept_loop;
CLOSE cur;
END$$
DELIMITER ;
i run it in phpmyamin , it run successfully .
then i am calling it from codeigniter
public function get_user_selected_all_sub_preferencs_of_preference($preference_id,$user_type,$profile_id)
{
$this->db->query("SET max_sp_recursion_depth=1000");
$this->db->query("CALL get_all_user_selected_children_of_preference(".$profile_id.",".$user_type.",".$preference_id.")");
$data= $this->db->query("SELECT * FROM temp_user_selected_preference_children");
$dara_arr=$data->result_array();
$this->db->query("DELETE FROM temp_user_selected_preference_children WHERE userProfileIdTmp=".$profile_id."");
if(is_array($dara_arr)&&count(array_filter($dara_arr))>0){
return $dara_arr;
}
else
{
return FALSE;
}
}
then it gives the error
A Database Error Occurred
Error Number: 1054
Unknown column 'PreferenceTypeTmp' in 'field list'
CALL get_all_user_selected_children_of_preference(65,2,1)
Filename: C:\xampp\htdocs\elephanti2\system\database\DB_driver.php
Line Number: 330
can not find what is this .
in mysql console , i describe table
please help . thanks in advance
As stated in the manual page on CREATE TABLE Syntax:
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)
Therefore, the DESCRIBE command that you ran from the MySQL command-line client refers to a different table to that which is in use by CodeIgniter.
Since the stored procedure uses CREATE TEMPORARY TABLE IF NOT EXISTS, it only creates a new table if one of the same name does not already exist on that connection: perhaps CodeIgniter previously created a table of the same name but with different columns?
You might have meant to first drop any existing table and then perform a simple create:
DROP TEMPORARY TABLE IF EXISTS temp_user_selected_preference_children;
CREATE TEMPORARY TABLE temp_user_selected_preference_children (
-- etc.

how to works on insert,update data from a table using store Procedure

if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.