Fairly new to databases and am using mysql 8.0. I am trying to change my delimiter for a stored procedure but it keeps telling me that create is expected. Not sure why. Any suggestions would be greatly appreciated.
This is my code for the procedure.
DELIMITER //
CREATE PROCEDURE CustomerInfo
(
CustomerFirstName CHAR (20),
CustomerLastName CHAR (20),
CustomerAddress CHAR (100),
CustomerState CHAR (20),
CustomerZip INT(5),
CustomerPhoneNumber CHAR (15),
CustomerEmailAddress CHAR (50)
)
BEGIN
INSERT INTO Customers
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
INSERT INTO Invoice
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
END //
DELIMITER ;
Related
I am currently learning MySQL and trying to find out how to code dynamically for input values.
DELIMITER //
CREATE PROCEDURE add_user(
IN username VARCHAR (16),
IN pass VARCHAR (255),
IN email VARCHAR(255),
IN fname VARCHAR (32),
IN lname VARCHAR (32),
IN age INT )
BEGIN
INSERT INTO users (username, pass, email, fname, lname, age)
VALUES (#username, #pass, #email, #fname, #lname, #age);
INSERT INTO usershobbies (u_id, h_id)
VALUES (LAST_INSERT_ID(), '1');
END; //
DELIMITER ;
When I call the procedure and insert values, I get an error message:
call add_user('bugsbunny', 'Carrot1000', 'bugs#bunny.com', 'Bugs','Bunny', '80');
#1054 - Unknown column 'age' in 'field list'
I appreciate any feedback. Thank you!
This error is telling you that there is no column age in table users. You would need to check the definition of your table (which you did not provide in your question).
Also, when using in/out parameters in MySQL, you should not prefix them with #. I like to give them a string prefix (like p_) do disambiguate from other litteral strings such as column names :
CREATE PROCEDURE add_user(
IN p_username VARCHAR (16),
IN p_pass VARCHAR (255),
IN p_email VARCHAR(255),
IN p_fname VARCHAR (32),
IN p_lname VARCHAR (32),
IN p_age INT
)
BEGIN
INSERT INTO users (username, pass, email, fname, lname)
VALUES (p_username, p_pass, p_email, p_fname, p_lname);
INSERT INTO usershobbies (u_id, h_id) VALUES (LAST_INSERT_ID(), '1');
END;
CREATE PROCEDURE novi_korisnik (korisnik_id INT, new_ime varchar(45), new_prezime varchar(60), new_datum_rodj DATE, new_drzava_rodj varchar(45), new_email varchar(45), new_korisnicko_ime varchar(45), new_biografija TEXT)
BEGIN
insert into korisnik ( ime, prezime, datum_rodj, drzava_rodj, email, korisnicko_ime, biografija) values ( new_ime, new_prezime, new_datum_rodj, new_drzava_rodj, new_email, new_korisnicko_ime, new_biografija);
END
Please help me with these procedure
Result is INCORRECT NUMBER OF ARGUMENTS
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 */
I have a stored procedure as
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out user_status varchar (30),
out reg_date DATETIME
)
BEGIN
select fname=users_table.first_name,
lname=users_table.last_name,
user_status=users_table.user_status,
reg_date=users_table.reg_date
FROM users_table
WHERE users_table.userid=userid;
END$$
I am using it like
SET #fname=#lname=#user_status=#reg_date=NULL;
CALL GetInfo(
'XYZ',
#fname,
#lname,
#user_status,
#reg_date
);
SELECT #fname,#lname,#user_status,#reg_date;
But I am getting only NULL values. I think that problem is with variable assignment using Select statement.
I know that in MSSQLSERVER, we can assign variable in SELECT statement as I have but I am not sure about MySQL.
You must change the SQL in you procedure:
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out p_user_status varchar (30),
out p_reg_date DATETIME
)
BEGIN
select users_table.first_name,
users_table.last_name,
users_table.user_status,
users_table.reg_date
into fname,lname,p_user_status,p_reg_date
FROM users_table
WHERE users_table.userid=userid
limit 1;
END$$
Variable names must be different than fields
And will work
I am getting the following error at command line when creating this new sproc and I can't seem to figure out what's causing it. All column names have been verified in the table along with their data types to be correct.
Error:
ERROR 1064 (42000): 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 'CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN Me' at line 2
here is the script:
DELIMITER //
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass
CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN MerchantStoreName VARCHAR (200),
IN RewardCardType VARCHAR (20),
IN RewardCardId BIGINT (20),
IN RewardCardTitle VARCHAR (50),
IN RewardCardDescription VARCHAR (100),
IN MerchantCharity VARCHAR (200),
IN MerchantDonation DOUBLE,
IN RewardCardExpDate DATE,
IN RewardCardreateDate DATE,
IN RewardCardStatus INT (1),
IN RewardCardPassHeader VARCHAR (50)
);
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50);
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20);
DECLARE RCpassbarcode_encoding VARCHAR (200);
DECLARE RCpassback_value1 TEXT;
DECLARE RewardCardTitle TEXT;
SET RCpasssecondary_value1 = 'expires';
SET RCpassbarcode_type = 'PKBarcodeFormatQR';
SET RCpassbarcode_encoding = 'iso-8859-1';
SET RCpassback_value1 = 'RCpassback_value1';
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `mcp_passes_merchants` (
passtype,
`status`,
merchantid,
rewardid,
merchantname,
meta_description,
passtypeidentifier,
formatversion,
teamidentifier,
serialnumber,
backgroundcolor,
visual_1xicon,
visual_2xicon,
visual_logo,
visual_text,
meta_expdate,
primary_label,
primary_value,
secondary_label1,
secondary_value1,
aux_label1,
aux_value1,
barcode_value,
barcode_text,
barcode_type,
barcode_encoding,
datecreated,
nonprofit,
donation,
back_label1,
back_value1);
VALUES (
RewardCardType,
RewardCardStatus,
MerchantNo,
RewardCardId,
MerchantStoreName,
RewardCardTitle,
RCpasstypeidenitifier,
RCpassformatversion,
RCpassteamidentifier,
RCpassserialnumber,
RCpassbackgroundcolor,
RCpassvisual_1xicon,
RCpassvisual_2xicon,
RCpassvisual_logo,
MerchantStoreName,
RewardCardExpDate,
RewardCardPassHeader,
RewardCardTitle,
RCpasssecondary_label1,
RCpasssecondary_value1,
RCpassaux_label1,
RewardCardId,
RewardCardId,
RewardCardId,
RCpassbarcode_type,
RCpassbarcode_encoding,
RewardCardCreateDate,
MerchantCharity,
MerchantDonation,
RewardCardDescription,
RCpassback_value1);
END//
DELIMITER ;
You need to end every statement with a delimiter. In your case it is //. Add this after the drop statement
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass //
here -------------------------^
here is how I fixed it. It turns out I had extra ';' after CREATE PROCEDURE statement.
DELIMITER $$
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass $$
CREATE PROCEDURE sproc_CreateFullRewardCardPass (IN MerchantNo INT (11),IN MerchantStoreName VARCHAR (200), IN RewardCardId BIGINT (20), IN RewardCardTitle VARCHAR (50), IN RewardCardDescription VARCHAR (100), IN MerchantCharity VARCHAR (200), IN MerchantDonation DOUBLE, IN RewardCardExpDate DATE, IN RewardCardreateDate DATE, IN RewardCardStatus INT (1),IN RewardCardPassHeader VARCHAR (50) )
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50) default 'expires' ;
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20) default 'PKBarcodeFormatQR' ;
DECLARE RCpassbarcode_encoding VARCHAR (200) default 'iso-8859-1' ;
DECLARE RCpassback_value1 TEXT default 'RCpassback_value1' ;
DECLARE RewardCardType VARCHAR (20) default 'full' ;
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `tablename` (passtype, `status`, merchantid, rewardid, merchantname, meta_description, passtypeidentifier, formatversion,
teamidentifier, serialnumber, backgroundcolor, visual_1xicon, visual_2xicon, visual_logo, visual_text, meta_expdate, primary_label, primary_value,
secondary_label1, secondary_value1, aux_label1, aux_value1, barcode_value, barcode_text, barcode_type, barcode_encoding, datecreated, nonprofit,
donation, back_label1, back_value1) VALUES (RewardCardType, RewardCardStatus, MerchantNo, RewardCardId, MerchantStoreName, RewardCardTitle, RCpasstypeidenitifier, RCpassformatversion, RCpassteamidentifier, RCpassserialnumber, RCpassbackgroundcolor, RCpassvisual_1xicon, RCpassvisual_2xicon, RCpassvisual_logo, MerchantStoreName, RewardCardExpDate, RewardCardPassHeader, RewardCardTitle,
RCpasssecondary_label1, RCpasssecondary_value1, RCpassaux_label1, RewardCardId, RewardCardId, RewardCardId, RCpassbarcode_type,
RCpassbarcode_encoding, RewardCardCreateDate, MerchantCharity, MerchantDonation, RewardCardDescription, RCpassback_value1);
END $$
DELIMITER ;;