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.
Related
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;
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 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.
someone can help me with procedure?
i get error:
Where is problem?
If you write a procedure with more than one statement in the body, you need to use BEGIN...END.
CREATE PROCEDURE addItemToRepository(IN name VARCHAR(50), IN rfid VARCHAR(20),
IN type VARCHAR(20), IN manufacturer INT, IN model VARCHAR(30), IN toRent TINYINT)
NOT DETERMINISTIC MODIFIES SQL DATA SECURITY DEFINER
BEGIN
SET #typeid = (SELECT id FROM dictionary WHERE value = type LIMIT 1);
INSERT INTO depository (name, rfidtag, type, manufacturer, model, torent)
VALUES (name, rfid, #typeid, manufacturer, model, torent);
END
Note I changed select(#typeid) in your insert statement. It's unnecessary to use select, and you can't do it without putting it inside a subquery in parentheses anyway.
I have some more comments:
Make sure your IN parameter names are distinct from your column names, or else you might create ambiguous SQL statements, that is MySQL doesn't know if you mean manufacturer the in parameter or manufacturer the column in the depository table. It's not an error, but it might insert a NULL because it's using manufacturer from the non-existing row. So I suggest the habit of naming in parameters with a prefix like "p" indicating parameter.
Declare a local variable instead of using session variables. MySQL treats them differently.
Consider using the alternative INSERT...SET syntax.
Here's my suggestion:
CREATE PROCEDURE addItemToRepository(IN pName VARCHAR(50), IN pRfid VARCHAR(20),
IN pType VARCHAR(20), IN pManufacturer INT, IN pModel VARCHAR(30), IN pToRent TINYINT)
NOT DETERMINISTIC MODIFIES SQL DATA SECURITY DEFINER
BEGIN
DECLARE vTypeid INT;
SELECT id INTO vTypeid FROM dictionary WHERE value = pType LIMIT 1;
INSERT INTO depository
SET name = pName,
rfidtag = pRfid,
type = vTypeid,
manufacturer = pManufacturer,
model = pModel,
toRent = pToRent;
END
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;