Hi I'm building a vinyl record store database and I want to update the stock quantity after making a sale.
These are the codes for my order table:
CREATE TABLE ORDER_INFO (
ORDER_ID INT NOT NULL,
CUSTOMER_ID INT NOT NULL,
RECORD_ID1 VARCHAR (50) NOT NULL,
RECORD1_QUANT INT NOT NULL,
RECORD_ID2 VARCHAR (50) NULL,
RECORD2_QUANT INT NULL,
RECORD_ID3 VARCHAR (50) NULL,
RECORD3_QUANT INT NULL,
RECORD_ID4 VARCHAR (50) NULL,
RECORD4_QUANT INT NULL,
RECORD_ID5 VARCHAR (50) NULL,
RECORD5_QUANT INT NULL,
CONSTRAINT PK_ORDER_INFO PRIMARY KEY (ORDER_ID));
These are the codes for my stock table:
CREATE TABLE STOCK_INFO (
RECORD_ID VARCHAR (50) NOT NULL,
RECORD_TITLE VARCHAR (1000) NOT NULL,
RECORD_ARTIST VARCHAR (1000) NOT NULL,
RECORD_LABEL VARCHAR (1000) NOT NULL,
RELEASE_YEAR YEAR NULL,
GENRE1 VARCHAR (50) NOT NULL,
GENRE2 VARCHAR (50) NULL,
MINTORNOT VARCHAR (3) NOT NULL,
PRICE DECIMAL (10,2) NOT NULL,
STOCK_QUANTITY INT NOT NULL,
CONSTRAINT PK_STOCK_INFO PRIMARY KEY (RECORD_ID));
And this is the trigger I created:
DELIMITER //
CREATE TRIGGER Update_Stockquantity
AFTER INSERT ON ORDER_INFO
FOR EACH ROW
BEGIN
UPDATE STOCK_INFO
SET STOCK_QUANTITY = STOCK_QUANTITY - RECORD1_QUANT WHERE RECORD_ID = RECORD_ID1;
UPDATE STOCK_INFO
SET STOCK_QUANTITY = STOCK_QUANTITY - RECORD2_QUANT WHERE RECORD_ID = RECORD_ID2;
UPDATE STOCK_INFO
SET STOCK_QUANTITY = STOCK_QUANTITY - RECORD3_QUANT WHERE RECORD_ID = RECORD_ID3;
UPDATE STOCK_INFO
SET STOCK_QUANTITY = STOCK_QUANTITY - RECORD4_QUANT WHERE RECORD_ID = RECORD_ID4;
UPDATE STOCK_INFO
SET STOCK_QUANTITY = STOCK_QUANTITY - RECORD5_QUANT WHERE RECORD_ID = RECORD_ID5;
END //
DELIMITER ;
The trigger has been created, but when I'm trying to insert a new sale into the ORDER_INFO table, MySQL gives me the "Unkown Column 'RECORD_ID1' in 'Where' clause". I am really confused, can anyone see where the problem is and how to correct it? Thank you!
I didn't really "try" anything--I mean I tried to think of it as a procedure or a function but after reading a bunch of material I think it makes sense to make it as a trigger instead of a procedure? But all the examples I read are usually simple
DELIMITER //
CREATE TRIGGER Update
AFTER INSERT ON ORDER
FOR EACH ROW
BEGIN
UPDATE STOCK
SET STOCK_QUANTITY = STOCK_QUANTITY - ORDER_QUANTITY WHERE STOCK_ID = GOODS_ID;
END //
DELIMITER ;
Does it have anything to do with the way I design my order table? Should I change it and how can I change it?
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.
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 ;;
I have a stored procedure, that checks if there is a user with the same e-mail address as the input, if not, then registeres one.
Here is the table:
CREATE TABLE IF NOT EXISTS `overkill`.`accounts` (
`accountID` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(64) NOT NULL ,
`firstName` VARCHAR(32) NOT NULL ,
`lastName` VARCHAR(32) NOT NULL ,
`passSaltedSHA` BINARY(20) NOT NULL ,
`salt` BINARY(20) NOT NULL ,
`gender` ENUM('m','f') NOT NULL ,
`birthDate` DATE NOT NULL ,
`regTime` TIMESTAMP NOT NULL ,
PRIMARY KEY (`accountID`) )
ENGINE = InnoDB;
Here is the stored procedure:
DELIMITER $$
CREATE PROCEDURE `overkill`.`registerUser` (
IN emailIN VARCHAR(64),
IN passwordIN VARCHAR(16),
IN firstNameIN VARCHAR(32),
IN lastNameIn VARCHAR(32),
IN birthIN DATE,
IN genderIN ENUM('f','m'))
BEGIN
DECLARE existingMailAccLOG INT DEFAULT NULL;
DECLARE saltLOC CHAR(40);
DECLARE regSuccessLOC BOOLEAN DEFAULT FALSE;
SELECT COUNT(*) INTO existingMailAccLOG FROM `overkill`.`accounts` WHERE `accounts`.`email` = emailIN;
IF existingMailAccLOG = 0 THEN
SET saltLOC = SHA1(rand());
SET regSuccessLOC = TRUE;
INSERT INTO `overkill`.`accounts` (`email`, `firstName`, `lastName`, `passSaltedSHA`, `salt`, `gender`, `birthDate`) VALUES(emailIN, firstNameIN, lastNameIn, UNHEX(SHA1(CONCAT(passwordIN, saltLOC))), UNHEX(saltLOC), genderIN, birthIN);
END IF;
SELECT regSuccessLOC AS `registered`, saltLOC AS `salt`;
END
If I call:
CALL registerUser("abc#def.com", "pass", "firstn", "lastn", "2012-01-01", "f");
It inserts a line into the accounts table, but forgets to return the proper values that I set inside the IF
SET saltLOC = SHA1(rand());
SET regSuccessLOC = TRUE;
How is it even possible? Why are theese lines skipped and INSERT still gets executed, without mistake?
Try to add "#" in front of your variable names after DECLARE keyword. It can cause some confusion, as it is described here: MySQL: #variable vs. variable. Whats the difference? (Part2) and here: MySQL: #variable vs. variable. Whats the difference?