Mysql Procedure INSERT (Already I looked at other answers) - mysql

in mysql I have two tables: produto and dim_produto.
CREATE TABLE `produto` (
`IdProduto` int(11) NOT NULL AUTO_INCREMENT,
`Ativo` char(1) DEFAULT 'S',
`CodPro` int(4) NOT NULL,
`CodCat` int(4) NOT NULL,
`DesPro` varchar(45) NOT NULL,
`CodMar` int(3) NOT NULL,
`CodSab` int(3) NOT NULL,
`QtdCaixa` int(2) NOT NULL,
`Transmitido` char(1) DEFAULT 'N',
PRIMARY KEY (`IdProduto`,`CodPro`),
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
--
CREATE TABLE `dim_produto` (
`CodPro` int(4) NOT NULL,
`Ativo` char(1) DEFAULT NULL,
`CodCat` int(4) DEFAULT NULL,
`Despro` varchar(45) DEFAULT NULL,
`IdInc` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`CodPro`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I'm trying to make a procedure that makes a select product in the table and enter the dim_produto table.
DELIMITER |
CREATE PROCEDURE dimProduto (codpro int (4), ativo char (1), codcat int (4), despro varchar (45))
BEGIN
insert into dim_produto (codpro, ativo, codcat, despro) (select codpro, ativo, codcat, despro from produto where transmitido = 'N');
END |
If I do this insert the way it is in the procedure, it works perfectly.
How do you call the procedure when I insert it in the select table dim_produto?
And how can I call it?
Grateful.

Related

Why don't I have all permissions when I am logged on as root?

This is a GCP MySQL Cloud instance, MySQL 2nd Gen 5.7. Google installed and configured it.
I am logged in as root:
When I run a script to create schemas, tables, and views, I get this error:
Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation
Here are the permissions assigned to root:
Here is the SQL that fails, in the form of a stored procedure:
CREATE DEFINER=`root`#`%` PROCEDURE `CreateFullCoverageTestCase`()
BEGIN
-- Create all the elements of the Full Coverage Test Case
-- *********************
-- Schema Store
-- *********************
Begin
DROP Schema if exists `store`;
END;
BEGIN
CREATE SCHEMA `store` ;
CREATE TABLE `store`.`city` (
`CityID` int(11) NOT NULL AUTO_INCREMENT,
`City` varchar(45) NOT NULL,
PRIMARY KEY (`CityID`),
UNIQUE KEY `City_UNIQUE` (`City`));
INSERT INTO store.city (City) VALUES('Cincinnati');
INSERT INTO store.city (City) VALUES('Columbus');
INSERT INTO store.city (City) VALUES('Kokomo');
INSERT INTO store.city (City) VALUES('Hillsdale');
INSERT INTO store.city (City) VALUES('Lexington');
INSERT INTO store.city (City) VALUES('Oxford');
CREATE TABLE `store`.`state` (
`StateID` int(11) NOT NULL AUTO_INCREMENT,
`State` varchar(45) NOT NULL,
`StateAbbreviation` varchar(2) NOT NULL,
PRIMARY KEY (`StateID`),
UNIQUE KEY `StateAbbreviation_UNIQUE` (`StateAbbreviation`),
UNIQUE KEY `State_UNIQUE` (`State`),
UNIQUE KEY `StateID_UNIQUE` (`StateID`));
INSERT INTO store.state (State, StateAbbreviation) VALUES('Indiana', 'IN');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Kentucky', 'KY');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Michigan', 'MI');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Ohio', 'OH');
CREATE TABLE `store`.`store` (
`StoreID` int(11) NOT NULL AUTO_INCREMENT,
`StoreNumber` varchar(10) NOT NULL,
`AddressLine1` varchar(45) NOT NULL,
`AddressLine2` varchar(45) DEFAULT NULL,
`CityID` int(11) NOT NULL,
`StateID` int(11) NOT NULL,
`ZipCode` varchar(10) NOT NULL,
PRIMARY KEY (`StoreID`),
UNIQUE KEY `StoreNumber_UNIQUE` (`StoreNumber`));
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000001', '111 Main', 1, 4, '45255-1321');
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000002', '817 Nordyke', 1, 4, '45103-0000');
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000003', '123 Green', 6, 4, '45056-0000');
End;
-- *********************
-- Schema HR (Human Resources)
-- *********************
BEGIN
DROP Schema if exists `hr`;
END;
BEGIN
CREATE SCHEMA `hr` ;
CREATE TABLE `hr`.`employee` (
`EmployeeID` INT NOT NULL AUTO_INCREMENT,
`LastName` VARCHAR(45) NOT NULL,
`FirstName` VARCHAR(45) NOT NULL,
`EmployeeNumber` VARCHAR(10) NOT NULL,
`AddressLine1` VARCHAR(45) NOT NULL,
`AddressLine2` VARCHAR(45) NULL,
`City` VARCHAR(45) NOT NULL,
`State` VARCHAR(2) NOT NULL,
`ZipCode` VARCHAR(10) NOT NULL,
PRIMARY KEY (`EmployeeID`),
UNIQUE INDEX `EmployeeNumber_UNIQUE` (`EmployeeNumber` ASC));
INSERT INTO hr.employee (lastName, firstName, EmployeeNumber, AddressLine1, City, State, ZipCode) VALUES('Cheaney', 'Calbert', 'E000000001', '17th Street', 'Bloomington', 'IN', '47405');
INSERT INTO hr.employee (lastName, firstName, EmployeeNumber, AddressLine1, City, State, ZipCode) VALUES('Reynolds', 'Chris', 'E000000002', 'Fee Street', 'Bloomington', 'IN', '47405');
END;
-- *********************
-- Products for sale
-- *********************
BEGIN
DROP Schema if exists `product`;
END;
BEGIN
CREATE SCHEMA `product` ;
CREATE TABLE `product`.`manufacturer` (
`ManufacturerID` int(11) NOT NULL AUTO_INCREMENT,
`Manufacturer` varchar(45) NOT NULL,
`Comment` varchar(1000) DEFAULT NULL,
`AddressLine01` varchar(45) DEFAULT NULL,
`AddressLine02` varchar(45) DEFAULT NULL,
`City` varchar(45) DEFAULT NULL,
`State` varchar(45) DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
`Phone` varchar(13) DEFAULT NULL,
`Manufacturercol` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ManufacturerID`),
UNIQUE KEY `Manufacturer_UNIQUE` (`Manufacturer`));
INSERT INTO product.manufacturer (Manufacturer) VALUES('Smuckers');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Dannon');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Kellogs');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Pepsi');
CREATE TABLE `product`.`unit` (
`UnitID` INT NOT NULL AUTO_INCREMENT,
`Unit` VARCHAR(45) NOT NULL,
PRIMARY KEY (`UnitID`),
UNIQUE INDEX `Unit_UNIQUE` (`Unit` ASC));
INSERT INTO product.unit(Unit) VALUES('Box');
INSERT INTO product.unit(Unit) VALUES('Bag');
INSERT INTO product.unit(Unit) VALUES('Carton');
INSERT INTO product.unit(Unit) VALUES('Jar');
INSERT INTO product.unit(Unit) VALUES('Tube');
INSERT INTO product.unit(Unit) VALUES('Case');
INSERT INTO product.unit(Unit) VALUES('Cup');
CREATE TABLE `product`.`product` (
`ProductID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`Description` varchar(45) NOT NULL,
`ManufacturerID` int(11) NOT NULL,
`UnitCost` decimal(10,3) DEFAULT '0.000',
`UnitPrice` decimal(10,3) DEFAULT '0.000',
`UnitID` int(11) DEFAULT NULL,
PRIMARY KEY (`ProductID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`),
UNIQUE KEY `Description_UNIQUE` (`Description`),
KEY `ManufacturerID_idx` (`ManufacturerID`),
KEY `UnitID_idx` (`UnitID`),
CONSTRAINT `ManufacturerID` FOREIGN KEY (`ManufacturerID`) REFERENCES `manufacturer` (`ManufacturerID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `UnitID` FOREIGN KEY (`UnitID`) REFERENCES `unit` (`UnitID`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000001', 'Blueberry Jam', 1, .50, 1.00, 4);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000002', 'Coffee Yogurt', 2, 1.00, 2.00, 7);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000003', '12 Individual Boxes Corn Flakes', 3, 1.00, 5.00, 6);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000004', 'Box Corn Flakes', 3, 1.00, 5.00, 1);
END;
-- *********************
-- Loyalty (Customers)
-- *********************
BEGIN
DROP schema if exists `loyalty`;
END;
BEGIN
CREATE SCHEMA `loyalty`;
CREATE TABLE `loyalty`.`loyalty` (
`LoyaltyID` INT NOT NULL AUTO_INCREMENT,
`LoyaltyNumber` VARCHAR(10) NOT NULL,
`LastName` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NULL,
`AddressLine1` VARCHAR(45) NULL,
`AddressLine2` VARCHAR(45) NULL,
`City` VARCHAR(45) NULL,
`State` VARCHAR(2) NULL,
`ZipCode` INT NULL,
PRIMARY KEY (`LoyaltyID`),
UNIQUE INDEX `LoyaltyNumber_UNIQUE` (`LoyaltyNumber` ASC));
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber, LastName) VALUES(1, 'L000000001', 'Smith');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber) VALUES(2, 'L000000002');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber) VALUES(3, 'L000000003');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber, LastName, FirstName) VALUES(4, 'L000000004', 'Knight', 'Robert');
END;
-- *********************
-- Reconciled Schema
-- *********************
BEGIN
DROP Schema if exists `reconciled`;
END;
BEGIN
CREATE SCHEMA `reconciled` ;
CREATE TABLE `reconciled`.`sale` (
`saleID` int(11) NOT NULL AUTO_INCREMENT,
`EmployeeFirstName` varchar(45) DEFAULT NULL,
`EmployeeLastName` varchar(45) DEFAULT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Unit` varchar(45) DEFAULT NULL,
`SKU` varchar(45) DEFAULT NULL,
`Qty` int(11) DEFAULT NULL,
`UnitCost` decimal(10,3) DEFAULT NULL,
`UnitPrice` decimal(10,3) DEFAULT NULL,
`Manufacturer` varchar(45) DEFAULT NULL,
`EmployeeNumber` varchar(45) DEFAULT NULL COMMENT 'The employee who handled the transaction',
`LoyaltyNumber` varchar(45) DEFAULT NULL,
`StoreNumber` varchar(45) DEFAULT NULL,
`DateOfTransaction` date DEFAULT NULL,
`TimeOfTransaction` time DEFAULT NULL,
`DateOfTransactionString` varchar(45) DEFAULT NULL,
`TimeOfTransactionString` varchar(45) DEFAULT NULL,
`WeekdayOfTransaction` int(11) DEFAULT NULL,
`MonthOfTransaction` int(11) DEFAULT NULL,
`YearOfTransaction` int(11) DEFAULT NULL,
`WeekdayNameOfTransaction` varchar(45) DEFAULT NULL,
`MonthNameOfTransaction` varchar(45) DEFAULT NULL,
`TotalPrice` decimal(10,3) DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
PRIMARY KEY (`saleID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
CREATE TABLE `reconciled`.`weather` (
`WeatherID` int(11) NOT NULL AUTO_INCREMENT,
`DateStamp` date DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
`Temperature` int(11) DEFAULT NULL,
`MonthName` varchar(45) DEFAULT NULL,
`YearNumber` int(11) DEFAULT NULL,
`DayName` varchar(45) DEFAULT NULL,
`MonthNumber` int(11) DEFAULT NULL,
`DayNumber` int(11) DEFAULT NULL,
PRIMARY KEY (`WeatherID`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
END;
-- *********************
-- Aggregated schema for the data warehouse
-- *********************
BEGIN
DROP Schema if exists dw;
CREATE SCHEMA `dw` ;
CREATE TABLE `dw`.`salesqtybyproductandstore` (
`SalesByProductAndStoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Monday` int(11) DEFAULT '0',
`Tuesday` int(11) DEFAULT '0',
`Wednesday` int(11) DEFAULT '0',
`Thursday` int(11) DEFAULT '0',
`Friday` int(11) DEFAULT '0',
`Saturday` int(11) DEFAULT '0',
`Sunday` int(11) DEFAULT '0',
PRIMARY KEY (`SalesByProductAndStoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
);
END;
BEGIN
CREATE TABLE `dw`.`monthlysalesqtybyproductandstore` (
`monthlysalesqtybyproductandstoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`January` int(11) DEFAULT '0',
`February` int(11) DEFAULT '0',
`March` int(11) DEFAULT '0',
`April` int(11) DEFAULT '0',
`May` int(11) DEFAULT '0',
`June` int(11) DEFAULT '0',
`July` int(11) DEFAULT '0',
`August` int(11) DEFAULT '0',
`September` int(11) DEFAULT '0',
`October` int(11) DEFAULT '0',
`November` int(11) DEFAULT '0',
`December` int(11) DEFAULT '0',
PRIMARY KEY (`monthlysalesqtybyproductandstoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `dw`.`weeklysalesqtybyproductandstore` (
`WeeklysalesqtybyproductandstoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Monday` int(11) DEFAULT '0',
`Tuesday` int(11) DEFAULT '0',
`Wednesday` int(11) DEFAULT '0',
`Thursday` int(11) DEFAULT '0',
`Friday` int(11) DEFAULT '0',
`Saturday` int(11) DEFAULT '0',
`Sunday` int(11) DEFAULT '0',
PRIMARY KEY (`WeeklysalesqtybyproductandstoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `dw`.`storesalesbytemperature` (
`StoreSalesByTemperatureID` INT NOT NULL AUTO_INCREMENT,
`StoreNumber` VARCHAR(45) NULL,
`Under0` DECIMAL(10,2) NULL,
`0To20` DECIMAL(10,2) NULL,
`21To30` DECIMAL(10,2) NULL,
`31To40` DECIMAL(10,2) NULL,
`41To50` DECIMAL(10,2) NULL,
`51To60` DECIMAL(10,2) NULL,
`61To70` DECIMAL(10,2) NULL,
`71To80` DECIMAL(10,2) NULL,
`81To90` DECIMAL(10,2) NULL,
`Over90` DECIMAL(10,2) NULL,
`StoreSalesByTemperaturecol` VARCHAR(45) NULL,
PRIMARY KEY (`StoreSalesByTemperatureID`),
INDEX `Unique` (`StoreNumber` ASC));
END;
BEGIN
DROP Schema if exists icecream;
CREATE SCHEMA `icecream`;
CREATE TABLE `icecream`.`flavor` (
`FlavorID` INT NOT NULL AUTO_INCREMENT,
`Flavor` VARCHAR(45) NULL,
PRIMARY KEY (`FlavorID`),
UNIQUE INDEX `Flavor_UNIQUE` (`Flavor` ASC));
CREATE TABLE `icecream`.`container` (
`ContainerID` INT NOT NULL AUTO_INCREMENT,
`Container` VARCHAR(45) NULL,
PRIMARY KEY (`ContainerID`),
UNIQUE INDEX `Container_UNIQUE` (`Container` ASC));
CREATE TABLE `icecream`.`icecream` (
`IceCreamID` int(11) NOT NULL AUTO_INCREMENT,
`FlavorID` int(11) DEFAULT NULL,
`ContainerID` int(11) DEFAULT NULL,
`SKU` varchar(45) DEFAULT NULL,
PRIMARY KEY (`IceCreamID`),
UNIQUE KEY `Unique` (`ContainerID`,`FlavorID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`),
CONSTRAINT `Container` FOREIGN KEY (`ContainerID`) REFERENCES `icecream`.`container` (`ContainerID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `Flavor` FOREIGN KEY (`FlavorID`) REFERENCES `icecream`.`flavor` (`FlavorID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO icecream.flavor (flavor) VALUES('Chocolate');
INSERT INTO icecream.flavor (flavor) VALUES('Vanilla');
INSERT INTO icecream.flavor (flavor) VALUES('Strawberry');
INSERT INTO icecream.flavor (flavor) VALUES('Raspberry Chocolate Chip');
INSERT INTO icecream.flavor (flavor) VALUES('Blueberry');
INSERT INTO icecream.container (Container) VALUES('cup');
INSERT INTO icecream.container (Container) VALUES('pint');
INSERT INTO icecream.container (Container) VALUES('quart');
INSERT INTO icecream.container (Container) VALUES('half gallon');
INSERT INTO icecream.container (Container) VALUES('gallon');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(1, 2, 'I000000001');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(2, 2, 'I000000002');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(3, 2, 'I000000003');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(1, 3, 'I000000004');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(2, 3, 'I000000005');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(3, 3, 'I000000006');
INSERT INTO icecream.icecream (flavorID, containerID) VALUES(4, 1);
END;
-- *********************
-- Schema Sales
-- *********************
BEGIN
DROP Schema if exists `weather`;
END;
BEGIN
CREATE SCHEMA `weather` ;
CREATE TABLE `weather`.`weather` (
`weatherText` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO weather.weather (weatherText) VALUES('2018/01/01 00:00:00.0000,45255,10');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/02 00:00:00.0000,45255,12');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/03 00:00:00.0000,45255,14');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/04 00:00:00.0000,45255,16');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/01 00:00:00.0000,45103,15');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/02 00:00:00.0000,45103,17');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/03 00:00:00.0000,45103,19');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/04 00:00:00.0000,45103,21');
END;
-- *********************
-- Schema Sales
-- *********************
BEGIN
DROP Schema if exists `sales`;
END;
BEGIN
CREATE SCHEMA `sales` ;
CREATE TABLE `sales`.`transaction` (
`TransactionID` int(11) NOT NULL AUTO_INCREMENT,
`DateTimeOfTransaction` datetime NOT NULL,
`LoyaltyNumber` varchar(10) DEFAULT NULL,
`StoreNumber` varchar(10) NOT NULL,
`EmployeeNumber` varchar(10) NOT NULL,
PRIMARY KEY (`TransactionID`),
UNIQUE KEY `NaturalKey` (`DateTimeOfTransaction`,`LoyaltyNumber`,`StoreNumber`,`EmployeeNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
END;
BEGIN
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(1, '2018-01-01 15:00:00', 'L000000001', 'S000000001', 'E000000001');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(2, '2018-02-02 15:00:00', 'L000000002', 'S000000002', 'E000000002');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(3, '2018-02-07 16:00:00', 'L000000002', 'S000000002', 'E000000002');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(4, '2018-02-09 16:16:00', 'L000000002', 'S000000002', 'E000000002');
END;
BEGIN
-- YYYY-MM-DD HH:MM:SS
CREATE TABLE `sales`.`transactiondetail` (
`TransactionDetailID` int(11) NOT NULL AUTO_INCREMENT,
`TransactionID` int(11) NOT NULL,
`SKU` varchar(45) NOT NULL,
`Qty` int(11) NOT NULL,
`TotalPrice` decimal(10,3) NOT NULL,
`Comment` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`TransactionDetailID`),
UNIQUE KEY `NaturalKey` (`TransactionID`,`SKU`),
KEY `TransactionID_idx` (`TransactionID`),
CONSTRAINT `TransactionID` FOREIGN KEY (`TransactionID`) REFERENCES `sales`.`transaction` (`TransactionID`) ON DELETE NO ACTION ON UPDATE NO ACTION);
END;
BEGIN
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(1, 'P000000001', 10, 100);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(1, 'P000000002', 5, 25);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(2, 'P000000001', 1, 100);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(2, 'P000000002', 2, 25);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(3, 'P000000002', 40, 40);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(4, 'P000000002', 44, 44);
END;
BEGIN
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`localhost`
SQL SECURITY DEFINER
VIEW `reconciled`.`mapping` AS
SELECT
`hr`.`employee`.`FirstName` AS `EmployeeFirstName`,
`hr`.`employee`.`LastName` AS `EmployeeLastName`,
`product`.`product`.`Description` AS `ProductDescription`,
`product`.`unit`.`Unit` AS `Unit`,
`product`.`product`.`SKU` AS `SKU`,
`sales`.`transactiondetail`.`Qty` AS `Qty`,
`product`.`product`.`UnitCost` AS `UnitCost`,
`product`.`product`.`UnitPrice` AS `UnitPrice`,
`product`.`manufacturer`.`Manufacturer` AS `Manufacturer`,
`sales`.`transaction`.`EmployeeNumber` AS `EmployeeNumber`,
`sales`.`transaction`.`LoyaltyNumber` AS `LoyaltyNumber`,
`sales`.`transaction`.`StoreNumber` AS `StoreNumber`,
`store`.`store`.`zipCode` as `ZipCode`,
CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS DATE) AS `DateOfTransaction`,
CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS TIME) AS `TimeOfTransaction`,
CAST(CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS DATE)
AS CHAR CHARSET UTF8) AS `DateOfTransactionString`,
CAST(CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS TIME)
AS CHAR CHARSET UTF8) AS `TimeOfTransactionString`,
WEEKDAY(`sales`.`transaction`.`DateTimeOfTransaction`) AS `WeekdayOfTransaction`,
MONTH(`sales`.`transaction`.`DateTimeOfTransaction`) AS `MonthOfTransaction`,
YEAR(`sales`.`transaction`.`DateTimeOfTransaction`) AS `YearOfTransaction`,
DAYNAME(`sales`.`transaction`.`DateTimeOfTransaction`) AS `WeekdayNameOfTransaction`,
MONTHNAME(`sales`.`transaction`.`DateTimeOfTransaction`) AS `MonthNameOfTransaction`,
`sales`.`transactiondetail`.`TotalPrice` AS `TotalPrice`
FROM
((((((`sales`.`transaction`
JOIN `sales`.`transactiondetail` ON ((`sales`.`transaction`.`TransactionID` = `sales`.`transactiondetail`.`TransactionID`)))
JOIN `hr`.`employee` ON ((`hr`.`employee`.`EmployeeNumber` = `sales`.`transaction`.`EmployeeNumber`)))
JOIN `product`.`product` ON ((`product`.`product`.`SKU` = `sales`.`transactiondetail`.`SKU`)))
JOIN `product`.`unit` ON ((`product`.`product`.`UnitID` = `product`.`unit`.`UnitID`)))
JOIN `store`.`store` ON ((`store`.`store`.`StoreNumber` = `sales`.`transaction`.`StoreNumber`)))
JOIN `product`.`manufacturer` ON ((`product`.`product`.`ManufacturerID` = `product`.`manufacturer`.`ManufacturerID`))) ;
CREATE TABLE `reconciled`.`icecream` (
`icecreamid` int(11) NOT NULL AUTO_INCREMENT,
`flavor` varchar(45) DEFAULT NULL,
`container` varchar(45) DEFAULT NULL,
`sku` varchar(45) DEFAULT NULL,
PRIMARY KEY (`icecreamid`),
UNIQUE KEY `Unique` (`flavor`,`container`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;
BEGIN
CREATE VIEW `reconciled`.`totalSalesByStoreAndTemperature` AS
SELECT StoreNumber, temperature, SUM(TotalPrice) as `SumOfTotalPrice`
FROM reconciled.sale inner join reconciled.weather on Substring(reconciled.sale.zipCode,1,5) = substring(reconciled.weather.zipCode,1,5)
Group By StoreNumber, temperature
ORDER BY StoreNumber, Temperature;
END;
END
Just want to add the root cause of this issue.
Cloud SQL currently is limited to grant SUPER and FILE privileges according to GCP documentation, so they can't be assigned to any user including root.
As per MySQL 5.7 documentation, the DEFINER attribute requires the user to have the SUPER privilege.
The situation above was the cause of your error and it worked when you took out the DEFINER statement.
In summary any other action that requires SUPER o FILE privileges most probably will fail with the error "Access denied; you need (at least one of) the SUPER privilege(s) for this operation".
I took out the DEFINER clause in the creation of the view reconciled.mapping.
That eliminated the error when I executed the SP.
It wasn't the DEFINER clause of the SP creation script, it was the DEFINER clause in the SP itself.
with my experience in AWS RDS, you don't get SUPER privileges as those are used by the provisioner engine. I am pretty sure that is how it is done in GCP. It could be that something in query make MySQL think it needs SUPER privileges. Posting your query which is giving the error will be helpful.
-------Update after OP posted the query----------
this is the problem. DEFINER. try CREATE PROCEDURE CreateFullCoverageTestCase()

MYSQL Trigger Copy Entire Row while insert,update

I have two Table namely admin_user,admin_user_bak
the structure of the table admin_user is
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and structure of table admin_user_bak have all the fields of admin_user and additionally a field bak_user_id . its auto increment id..
CREATE TABLE IF NOT EXISTS `admin_user_bak` (
`bak_user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`bak_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
here my trigger is
CREATE TRIGGER ins_admin_user BEFORE UPDATE ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( NEW.user_id, NEW.user_name, NEW.emailid, new.password, NEW.roll, NEW.created, NEW.modified, NEW.last_login, NEW.status);
END
my purpose is i want to back up all events. insert update delete of a particular record. not all record. i write for insertion. its not working
any idea.. thanks
i want a insertion trigger but you had written update on in your trigger so it's not work.
You use INSERT ON
CREATE TRIGGER ins_admin_user BEFORE INSERT ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( old.user_id, old.user_name, old.emailid, old.password, old.roll, old.created, old.modified, old.last_login, old.status);
END

Error executing stored procedure fails

I created a stored procedure in mysql database, which fires multiple insert statemets.
As shown below:
DELIMITER //
DROP PROCEDURE IF EXISTS insert_user_group_info
//
CREATE PROCEDURE insert_group_user_info(groupname varchar(50), groupdesc varchar(100),
createddate varchar(50), createdby varchar(100))
BEGIN
DECLARE RETURN_VAL INT UNSIGNED DEFAULT 0;
DECLARE NEWGROUPID INT UNSIGNED DEFAULT 0;
START TRANSACTION;
Insert into group_tbl (`groupname`,
`groupdesc`,
`groupusers`,
`createdon`,
`createdby`,
`groupstatus`)
values
(groupname,
groupdesc,
'1',
createddate,
createdby,
'1');
SET NEWGROUPID = LAST_INSERT_ID();
INSERT INTO useringroup_tbl
( groupid,
username,
regdate,
joindate,
userstatus,
roleid)
VALUES
( NEWGROUPID,
createdby,
createddate,
createddate,
'1',
'1');
INSERT INTO userinrole_tbl
( username,
groupid,
roleid)
VALUES
(createdby,
NEWGROUPID,
'1');
SET RETURN_VAL = LAST_INSERT_ID();
SELECT RETURN_VAL;
COMMIT;
END//
DELIMITER ;
Schema for tables are as follows
Table 1 group_tbl
CREATE TABLE `group_tbl`
(
`groupid` int(11) NOT NULL auto_increment,
`groupname` varchar(50) NOT NULL,
`groupdesc` varchar(100) NOT NULL,
`groupusers` int(11) NOT NULL,
`createdon` datetime NOT NULL,
`createdby` varchar(50) NOT NULL,
`groupstatus` tinyint(4) NOT NULL,
PRIMARY KEY (`groupid`)
);
Table 2 useringroup_tbl
CREATE TABLE IF NOT EXISTS `useringroup_tbl`
(
`groupid` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`regdate` datetime NOT NULL,
`joindate` datetime NOT NULL default '0000-00-00 00:00:00',
`userstatus` tinyint(4) NOT NULL,
`closingdate` datetime NOT NULL default '0000-00-00 00:00:00',
`roleid` int(11) NOT NULL default '0',
PRIMARY KEY (`groupid`,`username`)
);
Table 3 userinrole_tbl
CREATE TABLE IF NOT EXISTS `userinrole_tbl`
(
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`groupid` int(11) NOT NULL,
`roleid` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
While executing this procedure by this statement
call insert_group_user_info('test name','test description','2015-05-10 12:10:12','XYZuser')
I get an error
1062 - Duplicate entry '1-XYZuser' for key 1
and each time it the number appended to the username gets incremented
like this
1062 - Duplicate entry '2-XYZuser' for key 1
So please if anybody can give me direction what I am doing wrong,
Thanks in advance.
According to the error you have pasted,
1062 - Duplicate entry '1-XYZuser' - PRIMARY KEY (groupid,username) - It doesn't matter if you truncate the tables or not. You might be inserting the same data again.
You might be using last_insert_id() incorrectly.
Why is there no auto_increment in useringroup_tbl?

#1415 - Not allowed to return a result set from a trigger

DELIMITER $$
CREATE TRIGGER `msg_after_insert`
AFTER INSERT ON `meter_reading`
FOR EACH ROW
BEGIN
select meter_reading.*,a.amount as Light_Reading_Amt,b.amount as Power_Reading_Amt from meter_reading inner join master_unit a on a.min_unit<=msg1 and msg1<=a.max_unit and doc>=a.date_of_enter_value_fm inner join master_unit b on b.min_unit<=msg2 and msg2<=b.max_unit and doc<=b.date_of_enter_value_to;
INSERT INTO `smsd`.`outbox` (DestinationNumber,TextDecoded) VALUES (NEW.mobno, CONCAT_WS(NEW.msg1,NEW.Light_Reading_Amt,' ',NEW.msg2,NEW.Light_Reading_Amt));
END $$
DELIMITER ;
My databases are
1. mr_sms (tables are : 1.meter_reading 2.master_unit)
--meter_reading table--
CREATE TABLE IF NOT EXISTS `meter_reading` (
`serno` int(50) NOT NULL AUTO_INCREMENT,
`cno` varchar(50) DEFAULT NULL,
`accn_no` varchar(20) DEFAULT NULL,
`loc` varchar(20) DEFAULT NULL,
`type_of_accn` varchar(50) NOT NULL,
`rank` varchar(25) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`mobno` varchar(15) DEFAULT NULL,
`msg1` int(100) DEFAULT NULL,
`msg2` int(250) NOT NULL,
`status` varchar(30) NOT NULL,
`doc` date NOT NULL,
`remarks` varchar(100) DEFAULT NULL,
PRIMARY KEY (`serno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
--master_unit table--
CREATE TABLE IF NOT EXISTS `master_unit` (
`u_id` int(11) NOT NULL AUTO_INCREMENT,
`min_unit` int(150) NOT NULL,
`max_unit` int(200) NOT NULL,
`rate` varchar(20) NOT NULL,
`amount` varchar(20) NOT NULL,
`duty` varchar(20) NOT NULL,
`dutyamount` varchar(20) NOT NULL,
`date_of_enter_value_fm` date NOT NULL,
`date_of_enter_value_to` date NOT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
2. smsd (table is : outbox)
--
-- Table structure for table `outbox`
--
CREATE TABLE IF NOT EXISTS `outbox` (
`UpdatedInDB` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`InsertIntoDB` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendingDateTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendBefore` time NOT NULL DEFAULT '23:59:59',
`SendAfter` time NOT NULL DEFAULT '00:00:00',
`Text` text,
`DestinationNumber` varchar(20) NOT NULL DEFAULT '',
`Coding` enum('Default_No_Compression','Unicode_No_Compression','8bit','Default_Compression','Unicode_Compression') NOT NULL DEFAULT 'Default_No_Compression',
`UDH` text,
`Class` int(11) DEFAULT '-1',
`TextDecoded` text NOT NULL,
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`MultiPart` enum('false','true') DEFAULT 'false',
`RelativeValidity` int(11) DEFAULT '-1',
`SenderID` varchar(255) DEFAULT NULL,
`SendingTimeOut` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`DeliveryReport` enum('default','yes','no') DEFAULT 'default',
`CreatorID` text NOT NULL,
PRIMARY KEY (`ID`),
KEY `outbox_date` (`SendingDateTime`,`SendingTimeOut`),
KEY `outbox_sender` (`SenderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `outbox`
--

mysql TRIGGER update field after update

i have 2 table today_plan and kiln_master.
after pattern and itemno inserted to today_plan then need to select matching yeild from kiln_master table and update this value to yeild field in today_plan.
i have created a trigger
CREATE TRIGGER update_yeild AFTER INSERT ON today_plan
FOR EACH ROW UPDATE today_plan
SET yeild= (SELECT kiln_master.yeild from kiln_master,today_plan WHERE today_plan.itemno = kiln_master.item AND today_plan.pattern = kiln_master.pattern ) WHERE itemno=new.itemno AND pattern=new.pattern
what part is wrong with my code
today_plan
DROP TABLE IF EXISTS decsys.today_plan;
CREATE TABLE `today_plan` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`belt` varchar(25) NOT NULL,
`distant` varchar(25) NOT NULL,
`pjtno` varchar(15) DEFAULT NULL,
`pattern` varchar(25) NOT NULL,
`itemno` varchar(25) NOT NULL,
`pro_qty` varchar(25) NOT NULL,
`act_qty` varchar(25) NOT NULL,
`yeild` varchar(25) NOT NULL,
`remark` varchar(100) NOT NULL,
`shipment` varchar(15) NOT NULL,
`temp` varchar(15) NOT NULL,
`fire_date` date NOT NULL,
`kiln` varchar(10) DEFAULT NULL,
`kiln_plan` varchar(15) NOT NULL,
`kiln_act` varchar(15) NOT NULL,
`ins_date` date NOT NULL,
`ins_act` varchar(15) NOT NULL,
`plandate` date NOT NULL,
`ship_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;
kiln_master
DROP TABLE IF EXISTS decsys.kiln_master;
CREATE TABLE `kiln_master` (
`kid` int(7) NOT NULL,
`pattern` varchar(30) NOT NULL,
`item` varchar(30) NOT NULL,
`yeild` double NOT NULL,
`temp` int(6) NOT NULL,
`kiln` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
error
#1442 - Can't update table 'today_plan' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
thank you very much
You're trying to update the row after it has been inserted, isn't it better to do before so that it gets inserted with the correct values? That'd be something like;
CREATE TRIGGER update_yeild BEFORE INSERT ON today_plan
FOR EACH ROW
SET NEW.yeild = COALESCE((SELECT kiln_master.yeild
FROM kiln_master
WHERE NEW.itemno = kiln_master.item
AND NEW.pattern = kiln_master.pattern
LIMIT 1), 0);