after searching for a while I could not find a way to add rows in bulk by passing in argument for example a list of values. I know that it is possible to create a for loop, but it would insert each value separately n times, which I assume is bad performancewise. Here is a sample of what I have
CREATE PROCEDURE \`spLogAdd\`(
IN \`userId\` VARCHAR(255),
IN \`isActive\` BIT,
IN \`valueDatetime\` DATETIME,
IN \`valueId\` INT
)
BEGIN
INSERT INTO log_table (user_id, is_active, value_datetime, valueId)
VALUES (\`userId\`, \`isActive\`, \`valueDatetime\`, \`valueId\`);
END;
What I would like to have is a list of userIds('1,2,3,4,5,6') to insert multiple rows with 1 then 2 and so on at once. Other arguments do not need to change. If there is any neat way, please help me to do that.
CREATE PROCEDURE \`spLogBulkAdd\`(
IN \`userId\` VARCHAR(255),
IN \`isActive\` BIT,
IN \`valueDatetime\` DATETIME,
IN \`valueId\` INT
)
BEGIN
INSERT INTO log_table (user_id, is_active, value_datetime, valueId)
VALUES (\`userId\`, \`isActive\`, \`valueDatetime\`, \`valueId\`);
END;
Related
I cannot figure out the proper syntax in this stored procedure. I declared the Course_id variable. One of the input values to the stored procedure is named csi. I want to use value of csi to lookup a value in another table. I am trying to retrieve the course_id from a table named course_schedule. If you look at the first SELECT statement in this procedure you'll see what I'm trying to do. I have tried different ways of syntax in the statement, but I can't get it to work.
Can someone give me clue where I am going wrong?
Thank you,
Mike Reed
CREATE DEFINER=`root`#`%` PROCEDURE `ClassRegistration`(IN csi numeric, IN regid numeric, IN area_id numeric, IN comm_id numeric, IN feeamount numeric, IN status CHAR(50), IN addby CHAR(50), IN addip CHAR(50) )
BEGIN
DECLARE course_regid INTEGER DEFAULT 0;
DECLARE course_id INTEGER DEFAULT 0;
SELECT course_id INTO #course_id FROM course_schedule WHERE course_sked_id = csi;
INSERT INTO course_registered (course_sked_id, regid, area_id, comm_id, status, feeamount, added, addby, addip, modified, modby, modip) values (csi, regid, area_id, comm_id, status, feeamount, now(), addby, addip, now(), addby, addip );
SELECT LAST_INSERT_ID() INTO course_regid;
SELECT course_id;
INSERT INTO student_eval (regid, course_sked_id) values (regid, csi);
INSERT INTO instructor_eval (regid, course_sked_id) values (regid, csi);
END
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 want to create a procedure that creates a course for a user this takes one parameter that is userid and the other value will be selected from tbl_chapter.there are 27 chapters that are going to be selected and 27 inserts will be executed .insert is going to be like INSERT INTO tbl_user_chapter(user_id,chapter_id) VALUEs (9 , 1),(9,2),(9,3),....
I want something like this:
CREATE PROCEDURE createCourse (IN userid_param int)
BEGIN
INSERT INTO tbl_user_chapter(tbl_user_chapter.user_id,tbl_user_chapter.chapter_id) VALUE(userid_param , SELECT id FROM tbl_chapter)
END
SELECT id FROM tbl_chapter will be multiple rows.
I know this is wrong and I need help.
if there is a better way to do this please let me know.
If the select does not return one row, then don't use the VALUES( ) syntax. Use INSERT ... SELECT syntax:
CREATE PROCEDURE createCourse (IN userid int)
BEGIN
INSERT INTO tbl_user_chapter(user_id,chapter_id)
SELECT userid, id FROM tbl_chapter;
END
Make sure userid does NOT conflict with a column of the same name in your tbl_chapter table. If a column exists with that name, you should change the IN parameter of the stored procedure.
Im trying to generate a bunch of discounts codes. These have foreign key contraints.
At the moment I have
INSERT INTO code (title, code, desc) VALUES ('code1','XPISY9','test code');
INSERT INTO code_details (code_id, used, attempts) VALUES (
SELECT code_id from code where code = 'XPISY9',0,0);
The code_id in code_details is a foreign key for code_id in the code table.
What would be the best way to create a loop where I can generate a set of these values (around 100). I would need the code to be a not repeating random value.
Any help would be appreciated.
THanks
Once you have your 100 or so records in the code table, you can add the code details in one statement:
INSERT INTO code_details (code_id, used, attempts)
SELECT code, 0, 0
FROM code;
Generating the code records in the first place is another matter and is probably best done by using some other tool to generate 100 insert statements in a text file which you can then execute.
I've seen that done with every scripting language possible - chose your favourite. I've even seen Excel used with a column for the id and string formula to generate the insert statements.
Thanks for the help guys. I decided to put together a procedure for this and it worked great:
DELIMITER $$
CREATE PROCEDURE vouchergen(IN length INT(10) ,IN duration VARCHAR(20),IN sponsor VARCHAR(20),IN amount INT(10))
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i< amount) DO
SET #vcode= CONCAT(BINARY brand , UPPER(SUBSTRING(MD5(RAND()) FROM 1 FOR 6)));
INSERT INTO code (title, code, desc) VALUES (CONCAT(brand,i),#vcode,CONCAT(length,' ',duration));
INSERT INTO code_details (code_id, used, attempts) VALUES (
SELECT code_id from code where code = #vcode,0,0);
SET i=i+1;
END WHILE;
END$$;
I can then call this to gen however many i want:
CALL vouchergen(1,'week',APPL,400);
CALL vouchergen(1,'month',APPL,100);
CALL vouchergen(1,'day',APPL,200);
Im trying to make an stored Procedure that makes an insert into tables.
The insert effects two tables and there is an FK between them.
CREATE PROCEDURE `db`.`add_user_hr` (
IN in_Pass VARCHAR(45),
IN in_Value VARCHAR(45),
IN in_HR_ID INT,
)
BEGIN
INSERT INTO HR
(
Value
)
VALUES
(
in_Value
);
INSERT INTO User
(
Password,
HR_idHR
)
VALUES
(
in_Pass,
(Get the ID from HR)
);
END
How do I make sure that the correct id gets into User. And
if there is a error in the insert into HR, who do I make
sure it dosent tyr to make an insert into User? Ive
looked into "commit" but don't know how it's used.
Or is there a better way to do this?
In this example I have table UserType. Is this needed to
to find out witch type of user it is later on? And
how do I user this in the insert, select procedure in the
future?
Thx for all the help!
If PK (primary key) has AUTO_INCREMENT options than you can do something like this:
declare value_id_v INT;
insert into hr(value) values(value_in);
set value_id_v = last_insert_id();
Now you can use value_id_v to do inserts. Here is the link to read about this function LAST_INSERT_ID