I want to get null count of dyanmic column name - mysql

CREATE PROCEDURE ts(IN col1 varchar(100),IN val int, OUT res int)
BEGIN
SET #s=CONCAT("SELECT ISNULL(NULLIF(",col1,",'')) INTO #res FROM demo WHERE d=",val);
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SELECT #res INTO res;
END
i get following error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4
i want to used this stored procedure in PHP??? please help!!

ISNULL is not SQL92, try:
sum(case when field is null then 1 else 0 end)
for better portability

Related

UPDATE Stored Procedure in Mysql

I have written following Mysql stored procedure with 2 input parameters (ids and tags)
SET #sql = Concat('UPDATE tbl_Members SET TagId=CONCAT(TagId,''',tags,''') WHERE MemberID IN (',ids,')');PREPARE stmt FROM #sql;EXECUTE Stmt;
When I try to execute that it throws the following error
The following query has failed: "CREATE DEFINER=`0zrt`#`localhost` PROCEDURE `update_member_tag`(IN `ids` VARCHAR(255), IN `tags` VARCHAR(255)) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER SET #sql = CONCAT('UPDATE tbl_Members SET TagId=CONCAT(TagId,''',tags,''') WHERE MemberID IN (',ids,')'); PREPARE stmt FROM #sql; EXECUTE stmt;"
MySQL said: #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 'PREPARE stmt FROM #sql; EXECUTE stmt' at line 2

allocated prepared query error syntax error

I try to execute a variable query within a custom mysql function, here's the script :
DELIMITER $
CREATE FUNCTION is_present(in_id BIGINT, in_table_name VARCHAR(255)) RETURNS BIT
BEGIN
DECLARE stm VARCHAR(255);
DECLARE result BIT DEFAULT 0;
SET stm := CONCAT('SELECT IF(COUNT(*), 1, 0) INTO result FROM', in_table_name, 'WHERE id=? LIMIT 1');
PREPARE query FROM stm;
EXECUTE query USING in_id;
DEALLOCATE PREPARE query;
RETURN result;
END $
DELIMITER ;
Mysql warns me about the syntax :
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 'stm; EXECUTE query USING in_id; DEALLOCATE PREPARE query;
RETURN result; ' at line 9
I think you are missing a semicolon before var name:
PREPARE query FROM :stm;

dynamic sql in mysql stored proceure

I am trying to write a generic sql procedure that can execute any string as a sql statement.
Here is the procedure definition.
DELIMITER //
DROP PROCEDURE IF EXISTS execute_dynamic_sql;
CREATE PROCEDURE execute_dynamic_sql (IN sql_query longtext)
BEGIN
SELECT sql_query;
PREPARE stmt FROM #sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
I am calling the above function like this
mysql> call execute_dynamic_sql('show tables');
-> //
+-------------+
| sql_query |
+-------------+
| show tables |
+-------------+
1 row in set (0.00 sec)
ERROR 1064 (42000): 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 'NULL' at line 1
mysql> #
Can somebody tell me why is the error coming ?
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables.
In your example #sql_query is NULL and sql_query is assigned to SHOW TABLES.
Try:
DELIMITER//
CREATE PROCEDURE `execute_dynamic_sql` (IN `sql_query` LONGTEXT)
BEGIN
SET #`sql_query` := `sql_query`;
PREPARE `stmt` FROM #`sql_query`;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER;
SQL Fiddle demo

MySQL Stored Procedure with Prepared Statement does not work

I have the following stored procedure:
CREATE PROCEDURE getLastValueAutomaticShelter(IN fieldName varchar(30), position_number INT)
BEGIN
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE',fieldName,'IS NOT NULL AND P_id=?');
PREPARE stmt FROM #query;
SET #position_number=position_number;
EXECUTE stmt USING #position_number;
DEALLOCATE PREPARE stmt;
END
Then I am running it:
mysql> call getLastValueAutomaticShelter('current_level', 500)//
And getting the following error:
ERROR 1064 (42000): 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 'NOT N
ULL AND P_id=?' at line 1
Any ideas? Thanks.
You need to add some spaces in there:
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE ',fieldName,' IS NOT NULL AND P_id=?');
/* right ^ here....and ^ here*/
Otherwise your final query might look like this:
SELECT * FROM automatic_changes WHEREcolumnameIS NOT NULL AND P_id='whatever';
You get the idea :)

Error on executing proc in mysql insert statement

I am trying to execute this proc below and I get an error. please can someone bail me out
----
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_class_code`(p_newS varchar(45), p_designation varchar(45))
BEGIN
SET #table_name = p_designation;
SET #new_supply = p_newS;
SET #sql_text = concat('insert into simsed_',#table_name,' (class) values ',#new_supply) ;
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
----
This is the error i get.
call insert_class_code('supply 3', 'supplier)
Error Code: 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 'supply 3'
Your below line have error:
SET #sql_text = concat('insert into simsed_',#table_name,' (class) values ',#new_supply) ;
Change it with the following code:
SET #sql_text = concat('insert into simsed_',#table_name,' (class) values ("',#new_supply,'")') ;