Creating MySQL Stored Procedure - mysql

I am using Aqua Data Studio 20.6 to create a MySQL stored procedure. The following is my procedure, I have named it sp_GetObjIDByProType:
( IN prono char(15), IN imgtype char(8), OUT objid bigint )
BEGIN
SELECT ObjectID INTO objid FROM images WHERE ProNumber = prono AND DocType = imgtype LIMIT 20;
END
When I try to create the stored procedure, I get this error message:
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 'IN imgtype char(8) )
( OUT objid bigint )
BEGIN
SELECT ObjectID INTO objid F' at line 3
However, it appears as if creating the task was actually successfully
(sp_GetObjIDByProType appears under 'Procedures'). I can view and alter this created stored procedure in another window (resulting stored procedure in 'Alter' window). Does this mean that creation of the stored procedure was actually successful, or is there something wrong with what I did?

Looks like your procedure was created. However, the LIMIT 20 in the query does not make sense. You are returning the selected ObjectID in the OUT parameter which can hold only one value. Yet you limit the query to 20 rows.
If you want to return max 20 rows, return the rows as a result set (leave out the out parameter and the INTO objid), or limit the rows to just one row.

Related

Create a stored procedure that deletes a row when entering ID for that row

I have a problem with a stored procedure, both in syntax and logic. I got the error below when trying it on sqlfiddle.com.
Also I'm not sure if the logic is right. Is there anyway I can check by entering the ID and the query will return a table which row that contains the ID has been deleted?
create a procedure named delete_id which deletes the row that goes by the ID
CREATE PROCEDURE DELETE_ID (ID_INPUT IN INT)
AS
BEGIN
DELETE FROM TABLE ALBUM WHERE ID=ID_INPUT;
END;
Expected result: Enter an Id and SQL will delete the row which contains the ID
Actual Result:
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 'TABLE ALBUM WHERE ID=ID_INPUT' at line 3
You need to add "DELIMITER //" at the start on the code and "//" at the end. So your code should look like
DELIMITER //
CREATE PROCEDURE `DELETE_ID`(IN `ID_INPUT` INT)
BEGIN
DELETE FROM `ALBUM` WHERE `ID` = ID_INPUT;
END //

select from temporary table after call in mysql procedure

I am trying to make a reusable mysql procedure that takes data from join tables and store that in a temporary table. I will then create different procedures to select/calculate data from the result.
Here is my SQL code so far:
DELIMITER //
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;//
DELIMITER ;
The getModulesInCourse procedure creates a temporary table called modules
After getModulesInCourse is called in the procedure getModulesForCourseYear, I would then like to filter this result. This is where it fails.
I guess this is happening because mysql does not know what table modules is as it does not currently exist.
How would I be able to select from a temporary table in this procedure?
I am doing it in PHPMyAdmin which gives this error:
SQL query:
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;
MySQL said:
#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 'SELECT * FROM modules WHERE year = yr' at line 3
NOTE: I am doing the procedure this way as I want to select the same initial data in multiple procedures, then I will filter it down. This way, it prevents duplicate code and if I need to change it later on, I can do so once.

LIMIT not working with user defined parameters in Stored Procedure in MySQL 8.0

When I try to fetch user details from user details table using a Stored Procedure, the LIMIT statement not working with startIndex and endIndex parameters. The below code produces MySQL error 2013, ie Lost connection to MySQL server during query.
This will only happen in MySQL version 8.0, and properly working in below versions like 5.7 etc.. And also work when I remove the LIMIT condition from the procedure.
After that, I tried with PREPARE STMT this query produces an error because of multiple CONCAT using.
CREATE PROCEDURE return_something(IN markFilter VARCHAR(100), IN nameFilter VARCHAR(100), IN startIndex INT, IN endIndex INT)
BEGIN
CREATE TEMPORARY TABLE temp_user_details
SELECT id, name, address FROM user_details
WHERE (CASE WHEN markFilter!='' THEN FIND_IN_SET(mark,markFilter) ELSE mark IS NOT NULL END)
AND (CASE WHEN nameFilter !='' THEN name LIKE CONCAT('%',nameFilter,'%') ELSE id IS NOT NULL END)
LIMIT startIndex, endIndex;
SELECT id, name FROM temp_user_details;
DROP TEMPORARY TABLE return_something;
END
call return_something('','', 0, 100);
Most likely cause for this is a timeout. The LIMIT-clause might make the query slower and therefore the connection times out.
You did not state where you call the procedure from. If you call from command line, you can try to adjust the connect_timeout-variable.

Getting an error while trying to execute MYSQL stored procedure in phpmyadmin

I have created the following table:
CREATE TABLE Toy
(Toy_ID INT NOT NULL AUTO_INCREMENT,
Toy_Name VARCHAR(30) UNIQUE NOT NULL,
Toy_Price NUMERIC NOT NULL,
PRIMARY KEY (Toy_ID)
)
and then I inserted the values in toy table:
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Naruto',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Goku',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Luffy',25.00);
and then I typed the following stored procedure in SQL window in phpmyadmin:
CREATE PROCEDURE searchtoy (IN toy_no INT)
BEGIN
SELECT * FROM Toy
WHERE Toy_ID = toy_no;
END;
The stored procedure has been created successfully.
Then I tried to execute the stored procedure in the SQL window and I also have added // in the Delimiter text box:
CALL searchtoy(1);
But I am getting the following error:
Error
Static analysis:
1 errors were found during analysis.
Unexpected token. (near ";" at position 17)
SQL query:
CALL searchtoy(1);
MySQL said: Documentation
#1305 - PROCEDURE demo.searchtoy does not exist
Despite the stored procedure being created successfully, it is still showing that the stored procedure does not exist.
Where did I go wrong ?
It would be really helpful if the solution code is provided.
it is looking for searchtoy in demo schema. check the schema in which you have created your function

SQL Server 2008 create procedure and create view

I was trying to create a view and a procedure. However none of them can be done.
I have tried creating the procedure like this:
create procedure name ( #time )
as
begin
select tag_ID from Location where tag_ID=#time;
end
create procedure name
as
select tag_ID from Location where tag_ID=#time;
end
Both result in the following error
Major Error 0x80040E14, Minor Error 25501
create procedure name
as
select tag_ID from Location where tag_ID=#time
There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = procedure ]
For creating view
create view Time as select time from Location;
The error I received is:
Major Error 0x80040E14, Minor Error 25501
create view Time as select time from Location
There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = view ]
It seems to be some problem in using the CREATE, however i cannot figure it out.
I have tried most of the syntax but it seems not working, most of them pop out the same error.
** I am using SQL compact edition.
You're missing the type from the procedure parameter. Try something like this:
CREATE procedure name ( #time varchar(100))
AS BEGIN
select tag_ID from Location where tag_ID=#time;
END
Error Because You are missing datatype for procedure's parameter.
Note : Please recheck which datatype you are using I'm using datetime here.
CREATE PROCEDURE Name (#time DATETIME)
AS
BEGIN
SELECT tag_id
FROM location
WHERE tag_id = #time;
END
Are you using Sql Server Database Engine or SQL CE? I think You are using SQL CE or Limited version in which View and Procedures are not allowed;
Ref
http://social.msdn.microsoft.com/Forums/uk/sqlce/thread/f6ba9114-a962-41c2-b142-448c0f427cce