I'm trying to create a table inside a mysql stored procedure, but whenever I execute this procedure, I do not see the resulting table in my database. Can anyone tell me what's going on? The create table query works whenever I enter it in the cmd outside of a stored procedure.
delimiter //
drop procedure if exists gm //
create procedure gm()
begin
create table errorMessages (
error_id int not null auto_increment,
message varchar(200) not null default '',
primary key(error_id)
);
end //
delimiter ;
Are you executing the stored procedure or just creating it?
Try
call gm();
It gives any error?
Related
I have below basic table:
create table hobbies (
id int primary key auto_increment,
name varchar(32) unique);
And I am trying to create a stored procedure to insert a new row in this table, like below:
delimiter //
create procedure add_hobby(hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end
delimiter //
call add_hobby('randomHobby');
When I call the procedure like above I am getting message "An unexpected error occured". I am running the queries on db-fiddle.com, MySQL ver 8.0. Could anyone offer some guidance if I've done something wrong or missed something? I can mention that procedure with select operation and no parameters works. Many thanks in advance.
You need to set your delimiter to begin with and then reset it at the end
for your parameters you need to set weather they are IN or out then the name and the type
delimiter //
create procedure add_hobby(IN hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end//
delimiter ;
My tables are
create table employee(
id int(10) auto_increment primary key,
name varchar(100),
addressId int(10)
);
go
create table address(
id varchar(10) auto_increment primary key,
name varchar(100)
);
Here is my procedure
create procedure insert_employee(IN emp_name varchar(100),IN emp_address varchar(100))
begin
DECLARE #addressId varchar(10);
SELECT #addressId:=id from address where name LIKE '%'+emp_address+'%';
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF
INSERT INTO employee values(emp_name,#addressId);
END
I don't understand what is the problem. If i write this type of if condition in ms sql server there is not error. every time execute the procedure ti say error in end if. I have search in google but there is not idea about this. there is a problem in declare variable. If i copy form mysql documentation that also not work. why is that?
please help me
1. What is the proper way to declare variable under mysql stored procedure,
2. how to write if condition in mysql stored procedure.
thank you
Lots of differences between mysql and mssql. Declared variables should not include '#', all statements must be terminated, + is an arithmetic operator, if you procedure has multiple statements you must set delimiters before and after.
Further reading
How to declare a variable in MySQL?
https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
From MySQL reference Manual
An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF;
While I created loads of procs in SQL server, I want to start using MariaDB and so tried creating the simple proc below in MySQL Workbench.
I keep on getting an error stating that there is a missing SELECT on the opening '(' after the table name:
DELIMITER $$
drop procedure if exists usp_AddSentEmail$$
CREATE PROCEDURE usp_AddSentEmail (in pSender varchar(36)
,in pTo varchar(1000)
,in pSubject varchar(100)
,in pBody varchar(10000)
,in pRecordDT datetime)
BEGIN
INSERT INTO Emails('To','Subject','Body','Sender','RecordDT','Sent','SentDT')
VALUES (pTo,pSubject,pBody,pSender,pRecordDT,1,pRecordDT);
END$$
DELIMITER ;
Maybe I am trying the wrong google search but that all comes up is delimiter errors.
remove the quotes from the column names in your insert query:
INSERT INTO Emails(To,Subject,Body,Sender,RecordDT,Sent,SentDT)
VALUES (pTo,pSubject,pBody,pSender,pRecordDT,1,pRecordDT);
I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end
The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table
I' experiencing problems while trying to run a batch file within Mysql shell.
I wrote a stored procedure :
DELIMITER $$
DROP PROCEDURE IF EXISTS creaFornitori_prova$$
CREATE PROCEDURE creaFornitori_prova ()
BEGIN
create table Fornitori_prova (id_For mediumint(5) NOT NULL auto_increment,
nome_For varchar(50) NOT NULL,
ind_For varchar(50) NOT NULL,
PRIMARY KEY (id_For)
) ENGINE=InnoDB;
END$$
DELIMITER ;
I called the file : st_F.sql
Whenever I try to execute the file through the command : source st_F.sql;
I get this error:
ERROR:
Failed to open file 'st_F.sql', error: 2
I get the same error when I use \. st_Fornitori.sql;
Thanks Mauro
source DRIVE:ABSOLUTE_PATH_TO/st_F.sql;