Oracle function for auto increment using in procedure - function

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;
/

Related

Trigger doesn't get created, despite no errors being detected

Table:
create table produto
(
referencia varchar(3) primary key,
descricao varchar(50) unique,
estoque int not null default 0,
preco_normal decimal(10,2),
preco_desconto decimal(10,2)
);
Trigger (I tried creating it without a delimiter to no avail as well):
delimiter //
create trigger desconto before insert
on produto
for each row
begin
if new.estoque < 5 then
set new.preco_desconto = new.preco_normal * 0.90;
else
set new.preco_desconto = new.preco_normal;
end if;
end//
delimiter ;
The trigger result:
But when I use drop trigger if exists desconto;
The output says Trigger does not exist
Any ideas on how to solve this?

Error Code: 1054. Unknown column in where clause

I have the following table..
create table Reservation(
Reservation_number int not null auto_increment,
Flight_id int not null,
Number_of_passengers int,
Contact_Passport_number int,
primary key(Reservation_number),
foreign key(Flight_id) references Flight(Flight_id),
foreign key(Contact_Passport_number) references Contact(Contact_Passport_number)
);
.. the following stored procedure
delimiter //
create procedure addContact(
in reservation_nr int,
in passport_number int,
in email varchar(30),
in phone bigint)
begin
declare PassNumCheck int;
declare ResNumCheck int;
select Passengers.Passport_number into PassNumCheck
from Passengers
where Passengers.Passport_number = passport_number;
select Reservation_number into ResNumCheck
from Reservation
where Reservation_number = reservation_nr;
if (ResNumCheck is null) then
select "The given reservation number does not exist" as message;
elseif (PassNumCheck is null) then
select "The person is not a passenger of the reservation" as message;
else
insert into Contact(Contact_Passport_number,Phone_number,E_mail)
values (passport_number,phone,email);
update Reservation
set Contact_Passport_number = passport_number
where Reservation_number = reservation_nr;
end if;
end;
//
delimiter ;
..and the following procedure call
CALL addContact(#a,00000001,"frodo#magic.mail",080667989);
I am getting the following error " Error Code: 1054. Unknown column 'Reservation_number' in 'where clause' " when i try to execute the procedure call.
Update..
I also have the following trigger and table
drop trigger if exists ticketnumber;
delimiter //
create trigger ticketnumber
after update on Reservation
for each row
begin
declare ticket_number bigint;
set ticket_number=rand();
update Ticket set Ticket_number=ticket_number
where Reservation.Reservation_number=NEW.Reservation_number;
end;
//
delimiter ;
create table Ticket(
Booking_id int not null,
Passport_number int not null,
Ticket_number int,
primary key(Booking_id,Passport_number),
foreign key(Booking_id) references Booking(Booking_id),
foreign key(Passport_number) references Passengers(Passport_number)
);

How to get a row by ID in a MySQL Stored Procedure?

I have a problem with a Stored Procedure in MySQL. I want to get a single row by passing the ID parameter, however, it seems the SP ignores the WHERE filter and displays all the columns of the table.
What I find strange is that if I pass a specific value for id in a query outside the stored procedure, for example 1003, returns the expected result.
Sorry if my mistake is silly, I'm newbie.
The table structure is something like this:
CREATE TABLE Paciente
(
ID INT AUTO_INCREMENT NOT NULL,
DNI CHAR(8) UNIQUE NULL,
Nombre NVARCHAR(70) NOT NULL,
Apellido_Paterno NVARCHAR(30) NOT NULL,
Apellido_Materno NVARCHAR(30) NOT NULL,
Edad TINYINT NOT NULL,
Sexo CHAR(1) NOT NULL,
Calle NVARCHAR(50) NULL,
Numero_Domicilio SMALLINT(4) NULL,
Telefono NVARCHAR(8) NULL,
Movil NVARCHAR(10) NULL,
Estado_Civil NVARCHAR(20) NULL,
Ocupacion NVARCHAR(30) NULL,
Fecha_Registro DATETIME DEFAULT CURRENT_TIMESTAMP,
Estado BOOLEAN NULL DEFAULT TRUE,
PRIMARY KEY(ID)
)ENGINE = InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1000;
Stored Procedure:
DELIMITER $$
CREATE PROCEDURE `buscarPaciente`(IN id INT)
BEGIN
SELECT * FROM Paciente WHERE ID = id LIMIT 1;
END
$$
DELIMITER ;
The behaviour is caused by naming the input parameter same as the column name, therefore mysql cannot distinguish between the column and the parameter within the where clause. Rename the input parameter to let's say param_ID and it will return the record with the requested ID value.
DELIMITER $$
CREATE PROCEDURE `buscarPaciente`(IN param_ID INT)
BEGIN
SELECT * FROM Paciente WHERE ID = param_ID LIMIT 1;
END
$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE `Student`(IN param_ID INT)
BEGIN
SELECT * FROM Student WHERE **Student.ID = param_ID** LIMIT 1;
END
$$
DELIMITER ;

Create Table and Insert using stored Procedure

I'am trying to create a procedure checking table and insert
but it show me some error which i'am not sure how to fix it
this is the error code
Explicit or implicit commit is not allowed in stored function or
trigger
DELIMITER ;;
CREATE FUNCTION `getLabel`(paradocid INT, paradoctype char(10),paradoclineid INT,paraqty INT,paracreated date,paracreatedby INT) RETURNS int(100)
BEGIN
DECLARE transtotal int;
DECLARE i int DEFAULT 0;
DECLARE total int;
CREATE TABLE IF NOT EXISTS `sim_lable`(
`label_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`doctype` varchar(10) NOT NULL,
`docid` int NOT NULL,
`doclineid` int NOT NULL,
`created` date NOT NULL,
`createdby` int NOT NULL
) ENGINE='InnoDB' COLLATE 'utf8_unicode_ci';
DELETE FROM sim_lable where
(TIME_TO_SEC(TIMEDIFF(paracreatedby,createdby))/60) >30;
SELECT #total=coalesce(count(*),0) as total2 from sim_label where
doctype=paradoctype and paradocid=docid;
IF total = 0 THEN
WHILE i < paraqty DO
INSERT INTO dbo.Students
(
`doctype` ,
`docid`,
`doclineid` ,
`created` ,
`createdby`
)
VALUES
(
paradoctype,
paradocid,
paradoclineid,
paracreated,
paracreatedby
) ;
END WHILE;
END IF;
END
As the error message says, explicit or implicit commit is not allowed in stored function or trigger. CREATE TABLE statement causes an implicit commit, therefore it is not allowed in a function.
Your code does not seem to return any value (there is no return statement in the function's body) and what you are doing there should be done in a stored procedure, rather than in a function.

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;