#1064 - You have an error in your SQL syntax - mysql

I already have a table employee with columns name(String),id(int),age(int).
I can't figure out where the syntax is wrong?
CREATE PROCEDURE recins (
name1 IN employee.name%type ,
id1 IN employee.id%type ,
age1 IN employee.age%type
) AS
BEGIN
INSERT INTO employee VALUES(name1,id1,age1);
END;

create table employee2
(
name varchar(100) not null,
id int not null,
age int not null
);
DELIMITER $$
CREATE PROCEDURE recins (
IN name1 varchar(100),
IN id1 int,
IN age1 int
)
BEGIN
INSERT INTO employee2 (name,id,age) VALUES(name1,id1,age1);
END $$
DELIMITER ;
-- test:
call recins('a',1,2);
delimiter is a special wrapper for stored procs, events, functions. Delimiter ; at the end of the stored proc sets it back to the normal/default delimiter of a semi-colon.
The above was tested.

Related

Mysql procedure, If statement

How do i make an if statment that ROLLBACK: if the studentbrukernavn (students username) already exist, and if the klassekode (classcode) does not exist
DELIMITER $$
DROP PROCEDURE IF EXISTS OPPRETT_STUDENT$$
CREATE PROCEDURE OPPRETT_STUDENT
(
IN bilde_bildenr INT,
IN bilde_filnavn VARCHAR(30),
IN bilde_beskrivelse VARCHAR(30),
IN student_brukernavn VARCHAR(30),
IN student_fornavn VARCHAR(30),
IN student_etternavn VARCHAR(30),
IN student_klassekode VARCHAR(30)
)
BEGIN
START TRANSACTION;
INSERT INTO bilde
VALUES (bilde_bildenr,
CONCAT('bilder/',student_brukernavn,'.jpg'),
CONCAT('bilde av ',student_fornavn,' ',student_etternavn)
);
INSERT INTO student VALUES (student_brukernavn, student_fornavn, student_etternavn, student_klassekode, bilde_bildenr);
COMMIT;
END$$
DELIMITER ;
IF NOT EXISTS(SELECT query) THEN
ELSE
END IF;
Hope this helps

Oracle function for auto increment using in procedure

I have table name render and i create a procedure for it.
My table is
CREATE TABLE ROOM(
ROOM_ID NUMBER(10) NOT NULL,
ROOM_NO NUMBER(10) NOT NULL,
ROOM_DETAILS VARCHAR2(100) NOT NULL,
PRICE NUMBER(30) NOT NULL,
ROOM_PACKAGE VARCHAR2(100) NOT NULL
);
And procedure is
CREATE OR REPLACE PROCEDURE P_ROOM(
p_room_id IN ROOM.ROOM_ID%TYPE,
p_room_no IN ROOM.ROOM_NO%TYPE,
p_room_details IN ROOM.ROOM_DETAILS%TYPE,
p_price IN ROOM.PRICE%TYPE,
p_room_package IN ROOM.ROOM_PACKAGE%TYPE)
IS
BEGIN
INSERT INTO ROOM("ROOM_ID","ROOM_NO","ROOM_DETAILS","PRICE","ROOM_PACKAGE")
VALUES(p_room_id,p_room_no,p_room_details,p_price,p_room_package);
COMMIT;
END;
/
But i want to make a function which will call in procedure and will use to increment ROOM_ID. How i create a function for doing this?
I am trying to create a function but really i can`t
CREATE OR REPLACE FUNCTION AUTO_GEN
ID IN ROOM.ROOM_ID%TYPE
RETURN NUMBER
BEGIN
F_ID NUMBER(5);
F_ID=SELECT MAX(ROOM_ID) FROM ROOM;
IF(ROOM_ID NULL) THEN
ID=0;
ELSE
ID=ROOM_ID+1;
END IF;
RETURN ID;
END;
/

mysql Insert Statements in Stored Procedure not Inserting

I created a stored procedure which should perform the following:
Insert into Agencies Table, creating an id in the PK index row
Save the id in a variable
Insert the id and other data into Users table
The syntax below does not trigger any errors:
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END
However, the Stored Proc doesn't insert my parameters into the tables when executed. So, after searching I try to create the SP like this (including $$ after "END" and resetting the delimiter to a semi-colon ):
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END $$
DELIMITER ;
MySql creates the Stored Proc but gives me the following error:
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 'DELIMITER' at line 1
When I execute the created Stored Proc it still doesn't insert the data in my parameters.
Any help would be appreciated.
Some problems in your stored procedure:
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 (eg.: aName != #aName).
Avoid naming parameters or variables as columns of your tables.
DELIMITER $$
CREATE PROCEDURE `insertNewAgencyAndAdmin` (
/*
IN `aName` varchar (100),
IN `numTrav` int,
IN `polType` int,
IN `uEmail` varchar(255),
IN `uFName` varchar(40),
IN `uLName` varchar(40),
IN `uTitle` varchar(100)
*/
IN `_aName` varchar (100),
IN `_numTrav` int,
IN `_polType` int,
IN `_uEmail` varchar(255),
IN `_uFName` varchar(40),
IN `_uLName` varchar(40),
IN `_uTitle` varchar(100)
)
BEGIN
/*
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
#`aName`,
#`numTrav`,
#`polType`
);
*/
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
`_aName`,
`_numTrav`,
`_polType`
);
/*
SET #`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
#`agencyID`,
#`uEmail`,
#`uFName`,
#`uLName`,
#`uTitle`
);
*/
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
LAST_INSERT_ID(),
`_uEmail`,
`_uFName`,
`_uLName`,
`_uTitle`);
END$$
DELIMITER ;
SELECT * FROM discounts $$
delimiter $$;
CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)
BEGIN
insert into discounts values (id,title,amount);
END; $$
call insertData(3,"sadasd",56);
/*DROP PROCEDURE insertData */

Create function in MySQL

I'm just needing a little jump start, I'm a little confused how to do this. What I need to do is pull a 'customer_id' from the table and see how many items the customer has rented. If there are no 'customer_id' then it'll return 0. I'm just no quite grasping how to do this so any help is appreciated.
Create Table:
CREATE TABLE Customer
(name VARCHAR(30) NOT NULL,
address VARCHAR(70),
phone CHAR(10) NOT NULL,
customer_id INT(10) PRIMARY KEY NOT NULL);
Create Function: Have this partially started, but unsure if I'm doing it correctly.
DELIMITER $$
CREATE FUNCTION Num_Of_Rented(IN customer_id INT(10))
RETURNS INT(10)
BEGIN
DECLARE num INT(10);
SELECT IFNULL()
FROM
WHERE
RETURN num;
END $$
DELIMITER;
Inside your function, you need to select your value into your variable and then return your variable:
DECLARE num INT(10);
SELECT COUNT(field) INTO num
FROM table
WHERE condition;
RETURN num;
In your case:
DELIMITER $$
CREATE FUNCTION Num_Of_Rented(IN custId INT(10))
RETURNS INT(10)
BEGIN
DECLARE num INT(10);
SELECT COUNT(*) INTO num
FROM Customer C
WHERE C.customer_id = custId ;
RETURN num;
END $$
DELIMITER;

how to write procedure to insert data in to the table in phpmyadmin?

I have created table as below:
student:
+----+------+-----------+--------+
|uid | name | user_name | branch |
+----+------+-----------+--------+
| | | | |
+----+------+-----------+--------+
I want to insert data in to the table using procedure.
the procedure which i wrote is:
create procedure add(in_name varchar(50),in_user_name
varchar(50),in_branch varchar(50))
begin
insert into student
(name,user_name,branch) values (in_name ,in_user_name,in_branch);
end;
Try this-
CREATE PROCEDURE simpleproc (IN name varchar(50),IN user_name varchar(50),IN branch varchar(50))
BEGIN
insert into student (name,user_name,branch) values (name ,user_name,branch);
END
# Switch delimiter to //, so phpMyAdmin will not execute it line by line.
DELIMITER //
CREATE PROCEDURE usp_rateChapter12
(IN numRating_Chapter INT(11) UNSIGNED,
IN txtRating_Chapter VARCHAR(250),
IN chapterName VARCHAR(250),
IN addedBy VARCHAR(250)
)
BEGIN
DECLARE numRating_Chapter INT;
DECLARE txtRating_Chapter VARCHAR(250);
DECLARE chapterName1 VARCHAR(250);
DECLARE addedBy1 VARCHAR(250);
DECLARE chapterId INT;
DECLARE studentId INT;
SET chapterName1 = chapterName;
SET addedBy1 = addedBy;
SET chapterId = (SELECT chapterId
FROM chapters
WHERE chaptername = chapterName1);
SET studentId = (SELECT Id
FROM students
WHERE email = addedBy1);
SELECT chapterId;
SELECT studentId;
INSERT INTO ratechapter (rateBy, rateText, rateLevel, chapterRated)
VALUES (studentId, txtRating_Chapter, numRating_Chapter,chapterId);
END //
//DELIMITER;
This method work for me:
DELIMITER $$
DROP PROCEDURE IF EXISTS db.test $$
CREATE PROCEDURE db.test(IN id INT(12),IN NAME VARCHAR(255))
BEGIN
INSERT INTO USER VALUES(id,NAME);
END$$
DELIMITER ;