Q: Get ancestors with a node in mysql - php

Here is the reference link that I used to create procedures: https://dba.stackexchange.com/questions/7147/find-highest-level-of-a-hierarchical-field-with-vs-without-ctes/7161#7161?newreg=88de047579d242c98b8cfe0e17f0330f
Procedures are working well with the example. But not working in my own database. Seems I do something wrong while creating procedures.
Here is the procedure for my db:
DELIMITER $$
DROP FUNCTION IF EXISTS `demo`.`GetParentIDByID` $$
CREATE FUNCTION `demo`.`GetParentIDByID` (GivenID INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE rv INT;
SELECT IFNULL(user_by,-1) INTO rv FROM
(SELECT user_by FROM users WHERE id = GivenID) A;
RETURN rv;
END $$
DELIMITER ;
Sql query for the following procedure is working fine. It is basically getting parent_id of the node:
SELECT id,GetParentIDByID(id) FROM users;
Issue occurs when I am try to get Ancestors. Here is the Procedure that I am using:
DELIMITER $$
DROP FUNCTION IF EXISTS `demo`.`GetAncestry` $$
CREATE FUNCTION `demo`.`GetAncestry` (GivenID INT) RETURNS VARCHAR(1024)
DETERMINISTIC
BEGIN
DECLARE rv VARCHAR(1024);
DECLARE cm CHAR(1);
DECLARE ch INT;
SET rv = '';
SET cm = '';
SET ch = GivenID;
WHILE ch > 0 DO
SELECT IFNULL(user_by,-1) INTO ch FROM
(SELECT user_by FROM users WHERE id = ch) A;
IF ch > 0 THEN
SET rv = CONCAT(rv,cm,ch);
SET cm = ',';
END IF;
END WHILE;
RETURN rv;
END $$
DELIMITER ;
SQL query:
SELECT id,GetAncestry(id) FROM users;
Phpmyadmin getting stuck, if I am using this query. It never returns me anything nor an error or success

Related

How to retrieve data using primary key (INT) by store Procedure in sql?

I've created a store procedure to fetch data. The sql code is below. when i'm using another column(varchar) its working .. but when i'm trying to use by primary key (int) the below code is not working.
Create PROCEDURE ABC
(in #ID int)
as
Begin
SELECT *
FROM class
where id = #ID
END
DECLARE #ID INT;
SET #ID = 26;
CALL ABC(#ID)
This is database
Lots if mistakes here try
drop procedure if exists p;
delimiter $$
Create PROCEDURE p
(in inID int)
Begin
SELECT *
FROM users
where id = inID;
END $$
delimiter ;
SET #ID = 1;
CALL p(#ID)

MySQL Verify all colums and add if not exist

I try to check if all the column exist in a MySql ddb. I use this answer MySQL add column if not exist but i have a 1064 error. Anyboby can helps me ?
Thanks
foreach($source_bdd as $each_column)
{
$source_column = explode('***',$each_column);
$nullable = $source_column[4] == 'YES' ? 'DEFAULT NULL' : 'NOT NULL';
$verify_and_update_column .= '
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_'.$source_column[0].''.$source_column[1].' $$
CREATE PROCEDURE Alter_Table_'.$source_column[0].''.$source_column[1].'()
BEGIN
DECLARE '.$source_column[0].''.$source_column[1].'_count INT;
SET '.$source_column[0].''.$source_column[1].'_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "'.$source_column[0].'" AND
COLUMN_NAME = "'.$source_column[1].'");
IF '.$source_column[0].''.$source_column[1].'_count = 0 THEN
ALTER TABLE '.$source_column[0].'
ADD COLUMN '.$source_column[1].' '.$source_column[9].' '.$nullable.';
END IF;
END $$
CALL Alter_Table_'.$source_column[0].''.$source_column[1].'() $$
DELIMITER ;
';
}
Here is an extract of the query and the error at the end :
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_1 $$
CREATE PROCEDURE Alter_Table_table_1_column_1()
BEGIN
DECLARE table_1_column_1_count INT;
SET table_1_column_1_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_1_column_1_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_1_column_1() $$
DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_2 $$
CREATE PROCEDURE Alter_Table_table_1_column_2()
BEGIN
DECLARE table_1_column_2_count INT;
SET table_1_column_2_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_1_column_2_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_1_column_2() $$
DELIMITER ;
….
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_25_column_12 $$
CREATE PROCEDURE Alter_Table_table_25_column_12()
BEGIN
DECLARE table_25_column_12_count INT;
SET table_25_column_12_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_25_column_12_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_25_column_12() $$
DELIMITER ;
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 'DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_1 $$
CREATE PROC' at line 11064

trigger error with null output

here is my trigger:
create trigger wyplacamy before insert on `skladki_wyplaty`
for each row
BEGIN
SELECT p.wysokosc_skladki, p.ilosc_miesiecy INTO #wysokosc_skladki, #ilosc_miesiecy
FROM skladki_aktualne p
WHERE p.pesel = NEW.pesel AND p.id_ubezpieczenia = NEW.id_ubezpieczenia;
SET NEW.wyoskosc_wyplaty = #wysokosc_skladki / #ilosc_miesiecy *75;
END
$$
This works, but the iutpput of "wyoskosc_wyplaty" is NULL, even wysokosc_skladki = 8000, and ilosc_miesiecy = 8.
You need to declare the variables before using them.
delimiter $$
create trigger wyplacamy before insert on `skladki_wyplaty`
for each row
BEGIN
DECLARE var_wysokosc_skladki INT;
DECLARE var_ilosc_miesiecy INT;
SELECT p.wysokosc_skladki, p.ilosc_miesiecy INTO var_wysokosc_skladki, var_ilosc_miesiecy
FROM skladki_aktualne p
WHERE p.pesel = NEW.pesel
AND p.id_ubezpieczenia = NEW.id_ubezpieczenia;
SET NEW.wyoskosc_wyplaty = var_wysokosc_skladki / var_ilosc_miesiecy * 75;
END
$$

How I call a Stored Procedure in AdoDB & MySQL?

MySQL Procedure
DELIMITER $$
DROP procedure `verifica_nf` $$
CREATE PROCEDURE `verifica_nf` (IN _id_servidor_val BIGINT, IN _nr_periodo_val INT)
BEGIN
DECLARE nota INT;
DECLARE falta INT;
-- Declare variables used just for cursor and loop control
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
-- CURSOR--
DECLARE NF_CUR CURSOR FOR
-- //////////////////////////////////////////////////////////////// --
-- // VERIFICA SE E PERIODO ATIVO // --
-- //////////////////////////////////////////////////////////////// --
SELECT
DISTINCT
a.nr_notafinal,
cr.nr_faltas
FROM
pmc_servidor s
INNER JOIN pat_avaliacao a ON s.id_servidor = a.id_servidor
INNER JOIN pat_avaliacaoetapa ae ON a.id_avaliacao = ae.id_avaliacao
INNER JOIN pat_cronograma cr ON s.id_servidor = cr.id_servidor
AND cr.nr_periodo = a.nr_periodo
WHERE
s.id_servidor = _id_servidor_val AND
a.nr_periodo = _nr_periodo_val;
-- Declare 'handlers' for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN NF_CUR;
the_loop: LOOP
FETCH NF_CUR INTO nota, falta;
IF no_more_rows THEN
LEAVE the_loop;
END IF;
SELECT nota, falta;
END LOOP the_loop;
END;
$$
PHP Script
<?php
require_once("inc/con.php");
$db = ADONewConnection('mysqli');
$db->debug=1;
$db->PConnect("localhost","root","xxxxx","database");
$id = '1197614';
$period = 1;
$stmt = "CALL verifica_nf('$id','$periodo')";
$rs = $db->Execute($stmt);
echo $rs;
?>
The PHP script returns nota and falta value of database variables, but returns too the name of
database variables. Like this:
(mysqli): CALL verifica_nf('1197614','1')
nota,falta 10000,1
How can I get the values in a PHP variable?

Pass mysql query containing mysql function to PHP mysql_query

I have this code
DELIMITER $$
DROP FUNCTION IF EXISTS `GetNextID` $$
CREATE FUNCTION `GetNextID`() RETURNS INT DETERMINISTIC
BEGIN
DECLARE NextID INT;
SELECT MAX(articleID) + 5 INTO NextID FROM table_article;
RETURN NextID;
END$$
DELIMITER ;
INSERT INTO table_article ( articleID, articleAlias ) VALUES ( GetNextID(), 'TEST' );
executed OK in phpMyAdmin, but it fails when i pass this query to mysql_query PHP function/
Me guess this is because of the function and semi-colons. What do i do?
DELIMITER is not a MySQL keyword: it is a reserved word parsed by clients (like mysql, phpMyAdmin etc.) which allows splitting the queries.
You should split it manually and submit the three queries:
DROP FUNCTION IF EXISTS `GetNextID`
,
CREATE FUNCTION `GetNextID`() RETURNS INT DETERMINISTIC
BEGIN
DECLARE NextID INT;
SELECT MAX(articleID) + 5 INTO NextID FROM table_article;
RETURN NextID;
END
and
INSERT INTO table_article ( articleID, articleAlias ) VALUES ( GetNextID(), 'TEST' )
in three separate calls to the database.
you have DECLARE NextID INT; and RETURN NextID; and another line with ; inside the DELIMITER $$ deceleration.
my advice is stop using $$ as a delimiter

Categories