I've seen a lot of similar problems but none of them solved my problem. This is the code i used to create the stored procedure.
DELIMITER //
CREATE PROCEDURE sp_regUser(
IN user VARCHAR(100),
IN name VARCHAR(50),
IN surname VARCHAR(50),
IN email VARCHAR(250),
IN password VARCHAR(250),
IN telephone VARCHAR(15),
IN street VARCHAR(120),
IN postalcode VARCHAR(6),
IN id INT,
IN birthdate DATE,
IN message VARCHAR(320),
IN photo LONGBLOB,
IN photoName VARCHAR(50),
IN role VARCHAR(10)
)
BEGIN
INSERT INTO klientet(Perdoruesi,K_Emri,K_Mbiemri,K_Email,Fjalekalimi,K_T_Kontaktues,K_Rruga,K_KodiPostar,ID_Qyteti,K_Datelindja,Mesazh_shtese,Foto,Foto_Emri,Roli)
VALUES(user, name, surname, email, password, telephone, street, postalcode, id, birthdate, message, photo, photoName, role);
END//
DELIMITER ;
And this is the way i call it.
CALL sp_regUser('arlind','Arlind','Hajdari','arlind.hajdari#smth.com','perdorimi','0037745231807','Muhaxhiret 13','60000',8,'2016-01-01','miredita',LOAD_FILE('E:/Koala.jpg'),'image.jpg','user')
Doing this way, i get an error that says: Error Code: 1054. Unknown column 'K_Emri' in 'field list'. The columns specified in the insert syntax are correctly corresponding to ones in database, i have no idea whats the problem. Thanks in advance.
The problem was with a trigger that had to be triggered after the insertion to the klientet. Thank you for answers.
Related
DELIMITER &&
DROP PROCEDURE IF EXISTS add_row_in_emp_table;
CREATE PROCEDURE add_row_in_emp_table
(
empcode VARCHAR(15),
empname VARCHAR(15),
deptcode VARCHAR(15),
birthdate DATE,
sex CHAR(1),
desigcode varchar(15),
supcode varchar(15),
gradecode varchar(15),
gradelevel varchar(15),
basicpay varchar(15)
)
BEGIN
INSERT INTO emp
(empcode)
values(
empcode,empname,deptcode,birthdate,sex,desigcode,
supcode,gradecode,gradelevel,basicpay
);
END &&
DELIMITER ;
it is showing commands out of sync. can you please explain what is wrong?
i also tried using session variables that also is not working
Use columns name if few columns value are inserted. But if all columns value are inserted then columns name using are not mandatory but may be a chance for data type mismatch. Also passing session value with parameter or handle with variable inside procedure.
CREATE PROCEDURE add_row_in_emp_table (IN ecode varchar(15),IN ename varchar(15))
BEGIN
insert into emp (empcode, empname) values (ecode,ename);
END
N.B.: use columns name for inserting data or selecting data for better readability
Please check this url https://dbfiddle.uk/ODxp-NFH
I am trying to write a stored procedure in mysql using Heidisql. I keep getting the error. Below is the screen shot:
below is the script of the stored procedure.
CREATE PROCEDURE index_insertPersonadata
(
IN p_firstName VARCHAR(50),
IN p_middleName VARCHAR(50),
IN p_lastName VARCHAR(50),
IN p_address VARCHAR(200),
IN p_City VARCHAR(50),
IN p_State VARCHAR(50),
IN p_ZIP VARCHAR(50)
)
BEGIN
INSERT INTO personalrecord(
FirstName,
MiddleName,
LastName,
Address,
City,
state,
ZIP
)
VALUES
(
p_firstName,
p_middleName,
p_lastName,
p_address,
p_City,
p_State,
p_ZIP
);
END;
any help will be highly appreciated.
As far as your question is concerned, you are just missing a delimiter command:
DELIMITER //
CREATE PROCEDURE index_insertPersonadata (
IN p_firstName VARCHAR(50),
IN p_middleName VARCHAR(50),
IN p_lastName VARCHAR(50),
IN p_address VARCHAR(200),
IN p_City VARCHAR(50),
IN p_State VARCHAR(50),
IN p_ZIP VARCHAR(50)
)
BEGIN
INSERT INTO personalrecord(FirstName, MiddleName, LastName, Address, City, state, ZIP)
VALUES(p_firstName, p_middleName, p_lastName, p_address, p_City, p_State, p_ZIP);
END;
//
DELIMITER ;
Why you need this is explained in the documentation:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command.
I am trying to create some stored procedures in my database for my assignment and I can't work out why they don't work correctly as they run, they just don't have the desired effect.
The first is attempting to add members to my table. It runs fine but nothing is added. It just add's 0's for everything.
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddMember`(IN `iFirstname` VARCHAR(15) CHARSET utf8, IN `iLastname` VARCHAR(15) CHARSET utf8, IN `iCPR` INT(10) ZEROFILL, IN `iPhone` INT(8) ZEROFILL, IN `iAddress` VARCHAR(50) CHARSET utf8, IN `iPostcode` INT(4) UNSIGNED, IN `iDateJoined` DATE, IN `iNewsletter` BOOLEAN)
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO members (FirstName,
LastName,
CPR,
PhoneNumber,
Address,
Postcode,
DateJoined,
Newsletter)
VALUES (iFirstname,
iLastname,
iCPR,
iPhone,
iAddress,
iPostcode,
iDateJoined,
iNewsletter)
The second is deleting a member. If I run the DELETE FROM WHERE line by it's self it works fine but when its put into the stored procedure it doesn't work.
CREATE DEFINER=`root`#`localhost` PROCEDURE `DeleteMember`(IN mID INT)
BEGIN
DELETE FROM members WHERE MemberID = mID;
END
I am a bit confused as to what is wrong with them as I am new to this and finding specific answers is difficult especially as most examples are even more complex.
Hello Create an id Field in database table with auto incremented values then you will be able to store values via Stored Procedure returned above
I am trying to create a database in SQL Server but it keeps throwing errors. Could you look at the script and see if I am messing it up somehow?
drop database if exists cptc;
create database cptc;
use cptc;
drop table if exists staff;
create table staff (
staffID int not null auto_increment,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID),
FULLTEXT (fname,lname)
)ENGINE = MYISAM;
INSERT INTO staff VALUES ('','michael','herrera','doctor','2539703420','B14','122');
INSERT INTO staff VALUES ('','holly','herrera','teacher','2534667896','B35','116');
INSERT INTO staff VALUES ('','jesse','kirsch','professor','2534567890','B12','112');
INSERT INTO staff VALUES ('','mark','wahlberg','professor','5552345678','B01','112');
INSERT INTO staff VALUES ('','philip','spears','technician','2065672345','B12','123');
INSERT INTO staff VALUES ('','andrew','jackson','teacher','2061234567','B32','101');
INSERT INTO staff VALUES ('','annie','smith','mechanic','2533345609','B23','102');
INSERT INTO staff VALUES ('','alfred','hills','teacher','2535821513','B14','103');
INSERT INTO staff VALUES ('','bobby','jones','nurse','5559876056','B10','104');
INSERT INTO staff VALUES ('','tiffany','jones','janitor','2539981265','B02','108');
UPDATE: I think I was having Errors because it is MySQL and NOT SQL. Thanks everyone For Your (Really Quick) Answers!
This part is MYSQL not SQL Server ENGINE = MYISAM;
here is what the table would look like in SQL Server (sans the fulltext index, you first need to enable fulltext search in order to create a full text index))
create table staff (
staffID int not null identity,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID)
)
The drop and create database will look like this
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases
WHERE name = N'cptc')
DROP DATABASE cptc
GO
CREATE DATABASE [cptc]
The drop table will look like this
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'staff') AND type in (N'U'))
DROP TABLE staff
GO
Finally, you need to ommit the column for the identity while inserting, example
INSERT INTO staff VALUES ('michael','herrera','doctor','2539703420','B14','122');
If you don't ommit the column, you will get the following error
Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'staff' can only be specified when a column list is used and IDENTITY_INSERT is ON.
auto_increment should be identity(1,1)
ENGINE = MYISAM; doesnt exist what do you expect with it?
FULLTEXT (fname, lname) is not correct. What do you want, implement full text serach?
The question was not clear. You were creating a table not database.
It would be helpful if you provide the actual error detail.
BTW, you shouldn't put any value for the autoincrement column which will be assigned by server automatically. I guess this was causing the issue.
while creating trigger in mysql i m getting error 1046. my query is:
CREATE TABLE test.Employee(
id int,
first_name VARCHAR(30),
last_name VARCHAR(15),
start_date DATE,
end_date DATE,
city VARCHAR(10),
description VARCHAR(15)
);
CREATE TABLE test.Employee_log(
id int,
first_name varchar(50),
last_name varchar(50),
start_date date,
end_date date,
city varchar(50),
description varchar(50),
Lasinserted Time
);
when i am executing below lines it is giving error:
Error Code : 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 'END$$
delimiter' at line 1
(16 ms taken)
delimiter $$
CREATE TRIGGER test.Employee_Trigger
AFTER insert ON test.employee
FOR EACH ROW
BEGIN
insert into test.employee_log values(new.id,new.first_name,
new.last_name,new.start_date,new.end_date,
new.city,new.description,curtime());
END$$
delimiter ;
if some one could please help it would be greately appreciated.
Thanks
Yugal
trigger are not routine nor stored procedure,
so there is not need for BEGIN...END, plus the table name is case sensitive
CREATE TRIGGER Employee_Trigger
AFTER insert ON Employee
FOR EACH ROW
INSERT INTO test.Employee_log values(new.id,new.first_name,
new.last_name,new.start_date,new.end_date,
new.city,new.description,curtime());