mysql DECLARE WHILE outside stored procedure how? - mysql

I fairly new to mysql but have MS SQL experience.
Is it possible to declare variables and use while statement
outside stored procedure?
I only found examples where guys doing like this
1. procedure created
2. execute proc
3. drop proc
Suggest me the right way

No, you cannot do it. You can use these statements only inside BEGIN...END clause.
So, it is possible in stored procedures/functions, triggers and events.
More information here - MySQL Compound-Statement Syntax.

Related

How to Declare table to variable in MARIADB/MYSQL for function/procedure?

I don't know how to operate MySQL DECLARE THIS TABLE?
MS_SQL CAN
DECLARE #TEMP TABLE
(variable INT)
But MySQL can't do this! How can i do it?
Syntax error !!
MySQL does not have the feature of declaring a variable for a table. Variables can only be scalars.
If you need a feature that Microsoft SQL Server supports, then you should use Microsoft SQL Server.
Also when you use DECLARE in MySQL, you can't use the # sigil on variables. Local variables in stored routines don't have that sigil in MySQL. This is another difference from Microsoft SQL Server.
If you need to make a table or database name come from an argument or variable, you will need to
Construct the query, probably using CONCAT.
PREPARE the query.
EXECUTE the query.
DEALLOCATE PREPARE.

mysql stored function calling dynamic stored procedure

I have a stored procedure that concatenates sql to make insert/update etc.
The stored procedure works.
However, I dont like the way its called (its OT but Im using node.js and calling a stored procedure is quite hard to accomplish and there doesnt seem to be much support/documentation for this).
So I decide to create a "simple" mysql stored function that will basically call the stored procedure (the function is called with a few IN parameters, the procedure (from the function) is called with the same IN parameters PLUS the OUT parameter ).
However, i get an error:
Dynamic SQL is not allowed in stored function or trigger
yes, I have read about functions and dynamic sql isnt allowed. BUT: Im doing dynamic sql from the procedure, not from the function. But still mysql doesnt like it. Is there a solution to this? I dont think something is wrong with my code...
CREATE FUNCTION my_func(a varchar(50), b varchar(50), c varchar(50), d integer, e integer)
RETURNS INT(10)
DETERMINISTIC
BEGIN
set #theid = 0;
call my_proc(a, b, c, d, e, #theid);
RETURN (#theid);
Q: But still MySQL doesnt like it. Is there a solution to this?
No.
The "Dynamic SQL is not allowed in a function" restriction also extends to any stored procedures called by the function.
There is no solution. There is no workaround.
This simply can't be done: you can't use Dynamic SQL in the context of a function or trigger.
I can't confirm this right now in the code, but I would assume that there are some data structures associated with prepared queries that can exist only one at a time in a given MySQL thread. Since a stored function or trigger is invoked from a query (which may be dynamic SQL itself), the function or trigger can't initiate a new dynamic SQL query.
So it doesn't matter that the dynamic SQL is in a procedure. You can't make a stored function that prepares and executes dynamic SQL, whether directory or indirectly.
You might be trying to find a workaround for a solved problem. See Is there a driver for mysql on nodejs that supports stored procedures?

if no data has changed, the behavior stored procedure won't execute

I have a .dbml with one table and that table have associated behavior for update/delete/insert which are stored procedures.
If I query a row and I'm not modifying it or I set the same value that was there and I call datacontext.save(), it won't execute the stored procedure.
Is there a way to know that it won't be executed or force it?
You can force the stored procedure to execute by tricking linq2sql to think that a column is always 'dirty'. The answer to this question may help:
Can you convince a DataContext to treat a column as always dirty?

Stored procedure gone missing in SQL Server 2008

I created a stored procedure which ran successfully but I can't seem to find the procedure in the usual place within the database and I can't execute the procedure in Excel but I can drop the procedure.
I am confused.
I have written many stored procedures and never had this problem. Thanks in advance.
Sounds like something I've done before: inadvertently added the stored proc to the master database rather than the database I thought I was working in.
Check the master database under System Databases.
To prevent this in the future, you may want to consider adding a use dbname statement in the script.

how do I create a stored procedure from within a stored procedure

I would like to manage the deploy of stored procedures by first inserting them into a table, then, when it's time to deploy.... use a stored procedure to create all of the stored procedures. (Execute seems to be limited to CRUD)
This is not possible with MySQL. In stored procedures and functions you have even more restrictions in language use then in prepared statements.
The list of acceptable commands for prepared statements (see manual) does not include create procedure.
I'm not sure if this is what you looking for, but its has a good example and a reference at the bottom.
http://forums.mysql.com/read.php?98,219523,219787#msg-219787
Matthew H.