Is there any way to know if one stored procedure is called from another stored procedure in particular?
CREATE PROCEDURE `sp_1`(
IN invar_one VARCHAR(32),
OUT outvar_one VARCHAR(32)
)
BEGIN
/*I want some condition like this:*/
IF (/*Is called from sp_2 */) THEN
SET outvar_one = "OK";
ELSE
SET outvar_one = "NOT OK";
END IF;
END;
CREATE PROCEDURE `sp_2`(
IN invar_two VARCHAR(32),
OUT outvar_two VARCHAR(32)
)
BEGIN
CALL sp_1(invar_two,#outvar_two);
END;
A table called INFORMATION_SCHEMA.ROUTINES has the content of the procedure, You can do a like clause to check if a given Stored procedure is referred there.
SELECT COUNT(1)
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%sp_1%'
AND ROUTINE_TYPE='PROCEDURE'
AND ROUTINE_NAME = 'sp_2';
Details here - routines table
However, this would give a count even if procedure sp_1 is commented. So have a better like condition.
This is one of the way, there may be better ways.
Related
I'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END
You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END
I am following sql in 10 minutes to learn "stored procedure"
#+BEGIN_SRC sql :engine mysql :dbuser org :database grocer
CREATE PROCEDURE MailingListCount (
ListCount OUT INTEGER )
IS
v_rows INTEGER;
BEGIN
SELECT COUNT(*) INTO v_rows
FROM Customers
WHERE NOT cust_email IS NULL;
ListCount := v_rows;
END;
#+END_SRC
#+RESULTS:
| |
it report error:
ERROR 1064 (42000) at line 1: 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 'OUT INTEGER )
IS
v_rows INTEGER' at line 2
Could you please provide any hints?
Couple of fixes:
The OUT comes before the parameter name
Remove the unnecessary IS
Declare the variables inside the BEGIN END block
Use SET when you assign variables
So:
CREATE PROCEDURE MailingListCount(OUT ListCount INTEGER )
BEGIN
declare v_rows INTEGER;
SELECT COUNT(*) INTO v_rows
FROM Customers
WHERE NOT cust_email IS NULL;
SET ListCount := v_rows;
END;
Usually it's easier to handle the procedure output from result set rather than OUT variables. The OUT variables are useful primarily on calls between procedures.
So if you plan to call the routine from application, use:
CREATE PROCEDURE MailingListCount()
BEGIN
SELECT COUNT(*) as 'Count'
FROM Customers
WHERE NOT cust_email IS NULL;
END;
First thing is the position of the OUT keyword. It should be before the Parameter name.
Then second one no need to create the variable v_rows to store the output and then finally assigning it back to the OUT parameter listCount.
If you want to check condition like email should not null then you should do something like WHERE cust_email IS NOT NULL instead of WHERE NOT cust_email IS NULL
Please refer below code for the reference :
DELIMITER $$
CREATE PROCEDURE `MailingListCount` (OUT listCount INTEGER)
BEGIN
SELECT COUNT(*) INTO listCount
FROM Customers
WHERE cust_email IS NOT NULL;
END$$
DELIMITER ;
You can use the different delimiter for the MySql stored procedures and functions.
MySql use ; as default delimiter so delimiters other than the default ; are typically used when defining functions, stored procedures, and triggers wherein you must define multiple statements. You define a different delimiter like $$ which is used to define the end of the entire procedure, but inside it, individual statements are each terminated by ;. That way, when the code is run in the mysql client, the client can tell where the entire procedure ends and execute it as a unit rather than executing the individual statements inside.
You can refer the https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html to learn MySQL Stored procedure.
I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//
I've written a stored procedure. It's working fine except taking the table name as input parameter.
Let see my proc in MySQL:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS test_proc$$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SELECT COUNT(*) FROM newsInfoTable WHERE newsServiceName=serviceName;
END$$
DELIMITER ;
Stored procedure calling parameters:
USE db_test;
CALL test_proc('abc','tbl_test_news');
Here the service name parameter is working fine. But if I include the newsInfoTable variable as table input parameter then a error shows.
Table 'db_test.newsinfotable' doesn't exist
Why does this happen only for table parameter? How can I retrieve from this error or
How I pass a table name into a stored procedure as a parameter?
An SP cannot be optimized with a dynamic table name, so many DBs, MySQL included, don't allow table names to be specified dynamically.
One way around this is to use Dynamic SQL.
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql = CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=?;');
PREPARE s1 from #sql;
SET #paramA = serviceName;
EXECUTE s1 USING #paramA;
END$$
You can use EXECUTE IMMEDIATE for a "less is more" solution (for me, less code = good)
CREATE PROCEDURE test_proc(IN serviceName VARCHAR(10), IN newsInfoTable VARCHAR(100))
BEGIN
EXECUTE IMMEDIATE CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=''', serviceName, '''');
END
that part of a query cannot be dynamic.
you may consider implementing as a string that is executed dynamically at runtime
Although may not be what you want, alternatively, can consider to use conditionally if and prepare the statement.
DELIMITER $$
CREATE PROCEDURE select_count(IN table_name VARCHAR(20))
BEGIN
IF table_name = 'xxx' THEN
SELECT * FROM xxx;
ELSEIF table_name = 'yyy' THEN
...
ENDIF
END$$
I am trying to make a stored procedure using mySQL. This procedure will validate a username and a password. I'm currently running mySQL 5.0.32 so it should be possible to create procedures.
Heres the code I've used. All I get is an SQL syntax error.
GO
CREATE PROCEDURE checkUser
(IN #brugernavn varchar(64)),IN #password varchar(64))
BEGIN
SELECT COUNT(*) FROM bruger WHERE bruger.brugernavn=#brugernavn AND bruger.pass=#Password;
END;
Thank you in advance
I figured it out now. Here's the correct answer
CREATE PROCEDURE checkUser
(
brugernavn1 varchar(64),
password varchar(64)
)
BEGIN
SELECT COUNT(*) FROM bruger
WHERE bruger.brugernavn=brugernavn1
AND bruger.pass=password;
END;
# points to a global var in mysql. The above syntax is correct.
(IN #brugernavn varchar(64)**)**,IN #password varchar(64))
The problem is the )
Its very easy to create procedure in Mysql. Here, in my example I am going to create a procedure which is responsible to fetch all data from student table according to supplied name.
DELIMITER //
CREATE PROCEDURE getStudentInfo(IN s_name VARCHAR(64))
BEGIN
SELECT * FROM student_database.student s where s.sname = s_name;
END//
DELIMITER;
In the above example ,database and table names are student_database and student respectively.
Note: Instead of s_name, you can also pass #s_name as global variable.
How to call procedure?
Well! its very easy, simply you can call procedure by hitting this command
$mysql> CAll getStudentInfo('pass_required_name');