I want to insert into one table selecting values from another table and some with paramters.
CREATE PROCEDURE `insertuser`(
IN `param1` VARCHAR(50)
)
BEGIN
insert into users(firstname,lastname,email)
select name,nickname,#param1 from members
where id=3;
END
I fill param1 but the query insert null to email
what I need to do?
# symbol is used to define Session variables (accessible to the Session everywhere). You don't need to use # here, as you are using the input parameter value from the Stored procedure.
Try the following instead:
DELIMITER $$
CREATE PROCEDURE `insertuser`(IN `param1` VARCHAR(50))
BEGIN
INSERT INTO users(firstname,lastname,email)
SELECT name,nickname,param1 -- remove # from #param1 here
FROM members
WHERE id=3;
END$$
DELIMITER ;
Related
I'm trying to make a stored procedure that conditionally if the document number exists looks for an invoice and updates its balance to a new value that is a subtraction of the current balance and an input value, but for some reason when the procedure is executed it doesn't the update is running.
DELIMITER $$
CREATE PROCEDURE insertar_egreso(IN no_registro VARCHAR(255), IN id_proveedor INT, IN descripcion VARCHAR(255), IN no_doc VARCHAR(255),IN total DECIMAL ,IN no_cuenta VARCHAR(100),IN aprobado VARCHAR(255),IN subclase_gasto VARCHAR(255) )
BEGIN
IF no_doc!='' AND no_doc IS NOT NULL THEN
UPDATE ct_facturas SET saldo_fact =((SELECT saldo_fact FROM ct_facturas WHERE no_registro = no_doc)-total) WHERE no_registro = no_doc;
END IF;
SET total = ABS(total);
INSERT INTO ct_regtransac(no_registro,no_cuenta,vr_transaccion,tipo_mov,saldoreg) VALUES (no_registro,no_cuenta,total,1,total*-1);
INSERT INTO ct_egresos(tipo_reg,no_registro,id_proveedor,descripcion,no_doc,total,no_cuenta,aprobado, tipo_movimiento_cta, subclase_gasto, estado) VALUES ('EGRESO',no_registro,id_proveedor,descripcion,no_doc,total,no_cuenta,aprobado,'Salida',subclase_gasto,NULL);
END$$
DELIMITER ;
the queries inside if are not executing but i executed that update into phpMyAdmin with the same parameters and did work ,could it be that the if is not correctly written or the update sentence is confusing parameters?
I m passing parameters in stored procedures but unable to fetch records when filtering with inputs.But when I store that Input into variables and pass it to fetch record I get the records.why?
Here is stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = ip_Id;
END$$
DELIMITER;
I tried to take input in variable and pass it to where clause ti fetch records .
Here is the method that I have tried. And it works why?
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
Select ip_Id into #ip_Id;
SELECT * FROM table_namel IAS WHERE Id = #ip_Id;
END$$
DELIMITER;
We didn't know the structure of table_namel, but it seems the table have a field with same name as input parameter.
To prevent that use the parameters with some prefix/surfix:
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN p_ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = p_ip_Id;
END$$
DELIMITER;
My question is basically this one but for MySQL instead of SQL Server.
I have a stored procedure which returns a result set.
CREATE STORED PROCEDURE CreateFoo (input_id INT)
DECLARE result_id INT;
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SELECT LAST_INSERT_ID() INTO result_id;
INSERT INTO OtherTable (x, y, blah) VALUES (input_id, result_id, blah);
SELECT result_id;
END
I have a trigger on another table
CREATE TRIGGER MyTrigger AFTER INSERT ON Bar
FOR EACH ROW
BEGIN
CALL CreateFoo (NEW.a);
CALL CreateFoo (NEW.b);
END
But now when I insert into Bar I get
ERROR 1415 (0A000) at line 5365: Not allowed to return a result set from a trigger
I understand why this is happening, now I need to quietly ignore the resultsets from CreateFoo.
In SQL Server the answer is to create a temporary table and INSERT INTO Temp CALL CreateFoo but apparently you can't do this in MySql.
Is there a way to drop the resultset from the trigger in MySql?
The only way I can think of is using OUT parameters (and this is actually the "clean" way of returning a result from a procedure, when it's just a single row).
Rewrite your procedure like this:
CREATE STORED PROCEDURE CreateFoo (IN input_id INT, OUT result INT)
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SET result = LAST_INSERT_ID();
END
or
CREATE STORED PROCEDURE CreateFoo (IN input_id INT, OUT result INT)
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SELECT LAST_INSERT_ID() INTO result;
END
You'd execute the procedure like this then:
CALL CreateFoo(5, #my_result);
When you need the last insert id, you can use it with
SELECT #my_result;
in the same session.
In your trigger of course you simply ignore the variable.
CREATE TRIGGER MyTrigger AFTER INSERT ON Bar
FOR EACH ROW
BEGIN
CALL CreateFoo (NEW.a, #variable_you_donT_care_about);
CALL CreateFoo (NEW.b, #variable_you_donT_care_about);
END
Read more about IN, OUT and INOUT parameters here.
I am trying to insert a value in case not inserted else trying to update some of its field.There is only one variable used.
Value is not inserting although ion calling the store procedure it shows one row inserted.
Kindly help me , trying SP first time.
This is mine stored procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertLocation`(in IpAddress varchar(45))
BEGIN
if (SELECT count(*) as count
FROM mbuzzz.location_byhits
where IpAddress = IpAddress
having count>0)
then
UPDATE location_byhits SET Hits=Hits+1 where IpAddress=IpAddress;
else
insert into location_byhits(IpAddress,Date_current)
values (IpAddress,CURTIME());
End if ;
end
Rename your input parameter to make it clear to the DB engine when you mean the parameter and when the column name.
where IpAddress = IpAddress
is always true since the engine thinks you compare a column to itself.
Try
delimiter |
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertLocation`(in IpAddrParam varchar(45))
BEGIN
if ((SELECT count(*) FROM mbuzzz.location_byhits where IpAddress = IpAddrParam)>0)
then
UPDATE location_byhits SET Hits=Hits+1 where IpAddress=IpAddrParam;
else
insert into location_byhits(IpAddress,Date_current)
values (IpAddrParam, CURTIME());
End if ;
end
|
delimiter ;
I trying execute an trigger on mysql database.
The command executes successfully, but the trigger not working.
DELIMITER #
CREATE TRIGGER generate_coupon AFTER INSERT ON order
FOR EACH ROW
BEGIN
DECLARE userid, couponvalue INT;
DECLARE couponcode VARCHAR;
SELECT idUser INTO userid FROM indication WHERE email = NEW.email;
SET couponvalue = 20;
SET couponcode = 'abc123';
INSERT INTO coupon(idUser,idOrder,couponvalue,couponcode) values(userid, NEW.id, couponvalue, couponcode);
END#
DELIMITER ;
I suspect your problem arises from the collisions between your variables couponvalue and couponcode with the same-named columns in your coupon table. As documented under Local Variable Scope and Resolution:
A local variable should not have the same name as a table column.
You could simplify your trigger to the following and, in so doing, avoid this problem entirely:
CREATE TRIGGER generate_coupon AFTER INSERT ON order FOR EACH ROW
INSERT INTO coupon
(idUser, idOrder, couponvalue, couponcode)
SELECT idUser, NEW.id, 20, 'abc123'
FROM indication
WHERE email = NEW.email
;