what is the error of this stored procedure - mysql

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.

Related

MySQL Stored function to return name on id input

I have this table
I want to create stored function that takes empno and returns its manager (boss) name. (one employee is boss of other employee)
this is the code i tried but it returned error
MYSQL said: The query was empty
CREATE FUNCTION MANAGER1(ENO int) RETURNS varchar DETERMINISTIC
BEGIN
DECLARE MANAGER_NAME varchar(MAX);
Select name into MANAGER_NAME where boss = ENO;
RETURN varchar MANAGER_NAME
DELIMITER $$ ;
Check once again with following query for creating stored function:
DELIMITER //
CREATE FUNCTION getManager1(eno int(11))
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE manager_name varchar(250);
Select name into manager_name FROM employees where id = eno;
return manager_name;
END //
DELIMITER ;
In your stored procedure statement, END tag is missing and table name is also missing in SELECT statement. I have executed query mentioned here. Please match with your query and check what is missing in your query posted in question.

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//

MySql stored procedure no data fetched

SQLFiddle
I need to loop throught my cursor and insert new values in another table.
I have two tables:
CREATE TABLE peoples
(
id int auto_increment primary key,
name varchar(20),
enabled varchar(1)
)
/
INSERT INTO peoples
(name, enabled)
VALUES
('john', 'F'),
('jane', 'T')
/
CREATE TABLE test
(
id int,
logins int
)
/
I need to select all peoples enabled = T and insert a new entry in my second table 'test'. I created a stored procedure:
CREATE PROCEDURE sp_a()
BEGIN
DECLARE p int;
DECLARE done INT DEFAULT FALSE;
DECLARE peoples_cur CURSOR FOR select p.id from peoples p where p.enabled='T';
OPEN peoples_cur;
REPEAT
FETCH peoples_cur INTO p;
IF NOT done THEN
INSERT INTO test
(id, logins)
VALUES
(p, '999');
END IF;
UNTIL done END REPEAT;
CLOSE peoples_cur;
END;
/
But i got the error:
No data - zero rows fetched, selected, or processed: CALL sp_a()
SQLFiddle
Simple way:
DROP PROCEDURE IF EXISTS sp_a;
DELIMITER $$
CREATE PROCEDURE `sp_a`()
BEGIN
INSERT INTO test (logins)
SELECT COUNT(id) AS l FROM peoples WHERE enabled = 'T';
END$$
CALL sp_a();

MySQL Syntax Error in Declaration Temporary Variables

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 ;