Have a problem with my stored procedure (on MySQL). I need the data update process of the table if the record exists, else insert the record. I have:
DELIMITER //
CREATE PROCEDURE saveorUpdate(in product varchar(30), price int, stock int, active varchar(5))
BEGIN
DECLARE id int;
SELECT id_pro FROM products WHERE product=product into id;
IF(id_pro=id)THEN
UPDATE products SET product=product, price=price, stock=stock, active=active
WHERE id_pro=id;
ELSE
INSERT INTO products (product, price, stock, active) VALUES
(product, price, stock, active);
END IF;
END
Any ideas?
As your database is MySQL you could just use the INSERT INTO ... ON DUPLICATE KEY syntax and scrap the stored procedure stuff.
INSERT INTO products (
product, price, stock, active
) VALUES (
$product, $price, $stock, $active
) ON DUPLICATE KEY UPDATE
product=VALUES(product)
, price=VALUES(price)
, stock=VALUES(stock)
, active=VALUES(active)
Just an idea. Could be a cleaner and faster to write alternative.
Hope that helps
Here is the answer to the question - stored procedure with INSERT, UPDATE on DUPLICATE.
CREATE DEFINER=`root`#`localhost` PROCEDURE `manage-business`
(IN `bname` VARCHAR(200)) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
INSERT INTO tbl_company (co_name) VALUES (bname) ON DUPLICATE key UPDATE co_name=bname;
Related
I'm creating a stored procedure to insert data into three different tables.
Here is my code:
/*------------------------- Procedure for owner to submit his property -------------------------*/
DELIMITER //
CREATE PROCEDURE SubmitProperty (
IN input_property_owner_id INT,
IN input_property_type_id INT,
IN input_address VARCHAR(255),
IN input_zip_code VARCHAR(255),
IN input_area_m2 INT,
IN input_price_€ INT
)
BEGIN
INSERT INTO property (property_owner_id, property_type_id, address, zip_code, area_m2, price_€)
VALUES
(input_property_owner_id, input_property_type_id, input_address, input_zip_code, input_area_m2, input_price_€);
INSERT INTO survey (property_id, cas_eval_id, checksum_xxx)
VALUES
(property.id, 'CAS-XXX-YYYY', property.id % 1000);
INSERT INTO survey_question_answer (survey_id, question_id)
SELECT property_type_question.question_id
FROM property_type_question
WHERE property_type_question.property_type_id = input_property_type_id
VALUES
(survey.id, property_type_question.question_id);
END //
DELIMITER ;
The first 2 inserts work properly.
However, I get an error in the 3rd insert (around the area between the very last "FROM" and "VALUES").
Here is a picture of the error message:
Could you guys help me figure this out?
Thanks!
Is it because you're trying to insert into two columns, but are only selecting one?
INSERT INTO survey_question_answer (survey_id, question_id)
SELECT property_type_question.question_id
Is survey_id an identity column? If no, you need to bring in the survey_id from survey table and add that to your select clause:
INSERT INTO survey_question_answer (survey_id , question_id)
SELECT survey.id, property_type_question.question_id
FROM property_type_question
//Join here with your survey table
WHERE property_type_question.property_type_id = input_property_type_id;
I am running the following procedure in mysql:
create procedure addSavingAccount(id int(10), account_number varchar(10))
begin
insert into saving values(id,'savings', account_number, 0);
end //
However, when I try to call it, it gives me this error:
mysql> call addSavingAccount(103, 'B505');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
I checked on anything that could be linked to it, including triggers. But everything seems like it should be working. Here is my list of triggers:
create trigger balance_change_saving after update on saving for each row
begin
if old.balance != new.balance
then
insert into balance_update_history values(null, 'saving', new.account_number, old.balance, new.balance,
(SELECT NOW()), (select USER()));
end if;
end//
create trigger balance_insert_saving after insert on saving for each row
begin
insert into balance_update_history values(null, 'saving', new.account_number, 0, new.balance, (select now()),
(select user()));
end //
create trigger balance_delete_saving after delete on saving for each row
begin
insert into balance_update_history values(null, null, null, old.balance, null,
(SELECT NOW()), (select USER()));
end //
And here is where I define the table:
create table if not exists saving(account_number varchar(10) , customer_id int(10), balance decimal(8,2), primary key(account_number));
I'd just really like to to figure this out.
There are three columns based on your table create statement, not four. (what is the last 0 in that insert?)
Also, in the procedure, it appears that your insert values are out-of-order relative to the table creation order? So you can either rearrange the insert values to match the table, OR specify the columns with the insert.
I have two tables:table1 and rating
table1(id,name,category)
rating (cid,rating,total_rating,total_rates,photoID)
and now when i insert data into table1 i want to set all data in table rating at zero for that specific photoID from table1, but i dont know how..can someone help me?
You can use LAST_INSERT_ID() to retrieve the ID you just inserted. For example, assuming PhotoID is the relation between table1 and rating:
insert table1 (name,category) values ('waterfall 2', 'nature');
insert rating (rating,total_rating,total_rates,photoID) values
(0, 0, 0, last_insert_id());
I'd rather create a STORED PROCEDURE to make a single call from application. Assuming that you want to INSERT a record on table rating for every insert on table1 and that ID on table1 is set as AUTO_INCREMENT.
DELIMITER $$
CREATE PROCEDURE procedureName
(
IN _name VARCHAR(25),
IN _category VARCHAR(25)
)
BEGIN
INSERT INTO table1 (name, category)
VALUES (_name, _category);
SET #last_ID := LAST_INSERT_ID();
INSERT INTO rating (cid, rating, total_rating, total_rates, photoID)
VALUES (#last_ID, 0,0,0,0);
END $$
DELIMITER ;
and call the procedure,
CALL procedureName('nameHere','categoryHere')
You can use MySql triggers http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html:
CREATE TRIGGER ins_rating AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO rating (cid,rating,total_rating,total_rates,photoID)
VALUES( NEW.ID ,0,0,0,null)
END;
If you want to insert data into TABLE1 and delete it from TABLE2, you can write below listed query:
mysql_query("DELETE * FROM table2");
Mysql stored proc is
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `Check1`(
IN P_Name VARCHAR(50)
,OUT P_Id INT
)
BEGIN
INSERT INTO Company
(Name, Expiry, CreatedDate, Active)
VALUES
(P_Name, 90, NOW(), 1);
SET P_Id = ##identity;
END
Company TABLE has
CompanyID
Name
CreateDate
Active
as various columns
I need use VB .Net to call this procedure, insert value into the database and get the ID (primary key - company ID).
Whenever I insert a row, I need to retrieve the ID. Right now, I am able only to insert the row, but don't know how to call a SP with both input and output parameters from VB.
Check out the Direction member of OleDbParameter.
Is there a way to actually get the column name that was updated in order to use it in a trigger?
Basically I'm trying to have an audit trail whenever a user inserts or updates a table (in this case it has to do with a Contact table)
CREATE TRIGGER `after_update_contact`
AFTER UPDATE ON `contact` FOR EACH ROW
BEGIN
INSERT INTO user_audit (id_user, even_date, table_name, record_id, field_name, old_value, new_value)
VALUES (NEW.updatedby, NEW.lastUpdate, 'contact', NEW.id_contact, [...])
END
How can I get the name of the column that's been updated and from that get the OLD and NEW values of that column. If multiple columns have been updated in a row or even multiple rows would it be possible to have an audit for each update?
Just use OLD.colname <> NEW.colname for each column to check and find those that are different. Triggers are a bit limited in their use in MySQL, too bad.
Try this code...
create table sales (
orderno INT,
sale INT,
empsalary int,
ts TIMESTAMP
);
create table history (
updated varchar(20),
oldvalue INT,
newvalue INT
);
INSERT INTO sales
(orderno, sale, empsalary)
VALUES
(1,700,7000),
(2,800,8000),
(3,900,9000);
DROP TRIGGER test.instrigger;
DELIMITER ///
CREATE TRIGGER test.instrigger
AFTER UPDATE ON sales
FOR EACH ROW
BEGIN
IF NEW.sale <> OLD.sale THEN
INSERT INTO history
(updated, oldvalue, newvalue)
VALUES
('sale', OLD.sale,NEW.sale);
END IF;
IF NEW.empsalary <> OLD.empsalary THEN
INSERT INTO history
(updated, oldvalue, newvalue)
VALUES
('empsalary', OLD.empsalary,NEW.empsalary);
END IF;
END;
///
DELIMITER ;