1

Is there a way to check if a table exists on database MySQL using Stored Procedure?

This is the SP, I expected when the table exists the variable value titem_id return 1 and when table not exists the variable value titem_id return 0.

Instead in all conditions (the table exists or not) the value is always zero...

Help me to do it.

I don't need number of records but check if exist table in database.

If table exists the return of value titem_id it's 1 else it's 0.

CREATE DEFINER=`root`@`%` PROCEDURE `SP`(tmonth int(2), tddlarea CHAR(100), OUT titem_id INT(11))
BEGIN

DECLARE 2tmonth int(2);
DECLARE 2tddlarea char(100);
DECLARE 2tyear int(4);
DECLARE 2titem_id int(11);

SET 2tmonth = tmonth;
SET 2tddlarea = tddlarea;
SET 2tyear = YEAR(CURDATE());
SET 2titem_id = 0;

SET @t = CONCAT('SELECT EXISTS(SELECT * FROM INFORMATION_SCHEMA.tables AS titem_id
                 WHERE table_schema = ''db'' 
                 AND table_name = ''t_contents_', 2tddlarea, '_', 2tmonth, '_', 2tyear, ''');');

PREPARE stmt FROM @t;
EXECUTE stmt;
DEALLOCATE PREPARE `stmt`;

SELECT @t;

IF @t = 1 THEN

SET titem_id := 1;
                    
SET @s = -- EXECUTE SQL QUERY
                                    
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE `stmt`;

ELSE

SET titem_id := 0;  

END IF;

END

UPDATE

CALL sys.table_exists('db', CONCAT('t_contents_', 2tddlarea, '_', 2tmonth, '_', 2tyear, ''), @exists); 
SELECT @exists;                              

IF @exists > '' THEN
SET titem_id = 1;           
            
SET @s =  -- -- EXECUTE SQL QUERY

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

ELSE

SET titem_id = 0;   

END IF;
  • Does this answer your question? [My SQL Dynamic query execute and get ouput into a variable in stored procedure](https://stackoverflow.com/questions/5591338/my-sql-dynamic-query-execute-and-get-ouput-into-a-variable-in-stored-procedure) – P.Salmon Jan 14 '21 at 14:23
  • @P.Salmon What does the number of records have to do with checking the existence of a table, sorry but I don't understand – Iter Lsic Iealf Jan 14 '21 at 14:26
  • You have to select INTO a udv to get a value back from dynamic sql. – P.Salmon Jan 14 '21 at 14:37
  • @P.Salmon But in your link-suggestion does not indicated use select INTO syntax... sorry but I don't understand – Iter Lsic Iealf Jan 14 '21 at 14:40
  • Yes it does SET '@'query = 'SELECT column2, column3 INTO '@'c2, '@'c3 FROM table1 WHERE column1 = ?'; in the answer.. – P.Salmon Jan 14 '21 at 14:42
  • @P.Salmon I have error `1064 - 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 'SELECT EXISTS(SELECT * FROM INFORMATION_SCHEMA.tables WHERE ta' at line 1`. Pls see **UPDATE** in the question – Iter Lsic Iealf Jan 14 '21 at 15:02
  • it has to be into a udv ie a variable preceeded by @ – P.Salmon Jan 14 '21 at 15:08
  • @P.Salmon thanks I have the same error... Pls see **UPDATE** in the question – Iter Lsic Iealf Jan 14 '21 at 15:12
  • @P.Salmon Solved with https://dev.mysql.com/doc/refman/5.7/en/sys-table-exists.html Pls see **UPDATE** in the question – Iter Lsic Iealf Jan 14 '21 at 15:30

0 Answers0