mysql if without select - mysql

I'm using mysql and I want to use the if statement in a way you are doing in a stored procedure. Something like this:
delimiter $$
if #myVariable is null then
drop temporary table tmp_buffer;
select 'cannot proceed without variable #myVariable';
else
update my_table as t
set t.name = #myVariable;
end;
end$$
delimiter ;
When I execute this code it does nothing.
I tried to google this but I only find select if(....) explanations what doesn’t fit to my requirements.
I know that it is possible to create a stored procedure and put the code there and then call the procedure, but I'm looking for a way to do it without stored procedures.
Is this possible? If yes, what is wrong in my code?
Thanks for reading this
Felix

but I'm looking for a way to do it without stored procedures. Is this
possible?
NO; as already commented above, you cann't use if .. else construct block like the way you are intend to use in a normal SQL query. You will have to wrap it inside a procedural block which could be a stored procedure or a function.

Related

Call stored procedure in select statement in MySQL

I tried to write a recursive function, which is not possible. (I'm trying to check if a given row has a given ancestor somewhere in its chain). So, i wrote this check as a procedure instead. But I can't call a procedure within a select statement.
What's the solution for such a case?
i found a solution.
it's possible to use a function which internally calls a procedure

Can create procedure without using delimiter in MySQL?

I have a question about creating stored procedure in MySql without using delimiter
I search a lot in web but i dont find anything usefull for finding my answer.
so can we ever dont use delimiter??
if yes, how?
I am so happy if anyone can help me here with this question
thank you
If the procedure is just a single statement, you don't need to change the delimiter.
CREATE PROCEDURE myProc(param INT)
SELECT col1, col2 FROM someTable WHERE col3 = param;
You only have to change the delimiter if the procedure consists of multiple statements, since ; is the statement terminator in the procedure. If you don't change the delimiter, it will be treated as the terminator of the CREATE PROCEDURE statement itself.

Create a stored procedure only if the procedure does not exist in mysql

In SQL Server I am able to achieve this using dynamic sql string, but now I need to do the same thing for mysql but am getting nowhere, is there any way to achive this
IF NOT EXISTS (SELECT 1 FROM mysql.proc p WHERE NAME = 'stored_proc_name')
BEGIN
DELIMITER $$
CREATE PROCEDURE justATest()
BEGIN
-- some SP logic here
END$$
END
I am storing the whole sql as a string inside a database column and execute the statement using a prepared statement Execute inside another stored procedure.
IF NOT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name') THEN
....
taken from
Older Post
Control statements like if then else are only allowed inside Stored Procedures in MySQL (unfortunately). There are usually ways around this, but it depends why you are conditionally creating the sproc.
E.g. If you're trying to avoid errors when running build scripts because sprocs already exist then you can use a conditional drop statement prior to your create like this:
DROP PROCEDURE IF EXISTS justATest;
CREATE PROCEDURE justATest()
BEGIN
-- enter code here
END;
This will ensure the any changed code gets run (rather than skipped).

Call a stored procedure from within a view

I have a procedure that creates a table, is it possible to have a view (or similar) that can call the procedure then select from the table?
I've tried this:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `new_routine`(p1 INT) RETURNS int(1)
BEGIN
CALL rMergeDateFields();
RETURN 1;
END
CREATE VIEW `db`.`vIntervals` AS
SELECT new_routine(0) AS col1;
SELECT * FROM MergedData;
but I get this error
Error 1422: Explicit or implicit commit is not allowed in stored function or trigger.
Any ideas?
No, you cannot. Views are typically read-only operations; and that behavior cannot be guaranteed if stored-procedures are invoked.
Related question:
How to call Stored Procedure in a View?
Is it possible to call stored procedure in view?
Here is a canonical resource:
http://dev.mysql.com/doc/refman/5.1/en/view-updatability.html
Some views are updatable. That is, you can use them in statements such as UPDATE, DELETE, or INSERT to update the contents of the underlying table. For a view to be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table. There are also certain other constructs that make a view nonupdatable.
As invoking the stored procedure cannot assure 1:1 relationships with view rows, the update is not permitted.
You can't do this from a view, but a stored procedure itself can return a result set.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `merge_and_select` ()
BEGIN
CALL rMergeDateFields();
SELECT * FROM MergeData;
END $$
If you CALL merge_and_select() the rMergeDateFields procedure will be run and then you will get the contents of the MergeData table returned to you, in one step... which sounds like what you're wanting.
This is, however, a really bad implementation, because there's no control for concurrent calls to merge_and_select(), so all kinds of things could go wrong if it's run more than once at the same time.
However, depending on what you really need rMergeDateFields() to do, it's possible that you could rewrite rMergeDateFields() to actually perform whatever work you need done and return it directly to the client without using the MergeData table using an unbounded SELECT, as shown above.
Anything you SELECT in a stored procedure without using INTO a variable is returned to the client as a response from the CALL.

Mysql Stored Procedures Dynamic Queries

I've had quite a few problems and know that I can get some good answers here!
Ok kinda 2 part question.
Part 1 I'm doing some really big updating of data, kind rejiging the tables mostly.
so the question is should I be using a mysql stored procedure or mysql/php like normal.
I'm currently on the stored producure frame of mind.
Reasons are
a) Quicker
b) No timeouts.
If anyone has any other opinions let me know.
P.S we are talking about a big heap of data. LIKE over 1.5 million rows
2nd part.
In stored procedures how do I make a query that will only return one row just give me that row. Also the query is a little dynamic so like
SET tag_query = concat('SELECT tag_id FROM tags WHERE tag = "',split_string_temp,'"');
Any clues?
I can't seem to find anything just easy about this language!
Thanks in advance for your help.
Richard
Your question is a little vague, so I'll just respond to the one piece of code you included.
If you want to get a tag_id from a tag name, I would recommend a stored function instead of a stored procedure.
Something like this:
DELIMITER $$
DROP FUNCTION IF EXISTS GET_TAG_ID $$
CREATE FUNCTION GET_TAG_ID(P_TAG_NAME varchar(255)) RETURNS int
BEGIN
DECLARE v_return_val INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_return_val = -1;
IF (P_TAG_NAME IS NULL)
THEN
RETURN NULL;
END IF;
select tag_id
into v_return_val
from TAGS
where tag = P_TAG_NAME;
RETURN v_return_val;
END $$
DELIMITER ;
To update data once (not as a regular task) I would prefer using a gui admin like phpmyadmin or sqlyog issuing SQL commands directly (with a good backup of course!) as you can see the results quickly and don't need to worry with other things than your main task.