Here is my procedure code. I am trying to update Username by putting old username in where clause.but it doesn't work.
DELIMITER $$
DROP PROCEDURE IF EXISTS `databasename`.`UpdateUsername` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `UpdateUsername`
(IN uname VARCHAR(30),tid VARCHAR(100),username VARCHAR(30) )
BEGIN
UPDATE table_name SET Username=username WHERE Username=uname;
END $$
DELIMITER ;
please help me to FIX this problem.
Try to remove 'username' from line number '6' and use some other parameter name. It might be conflicting with your table Username field.
For example: UPDATE table_name SET Username=OTHER_PARAMETER_NAME WHERE Username=uname;
If tid is the table name, shouldn't you use tid instead of table_name in the update query?
Hi i have faced same issue give an alise to table name and try running update query
try this query
UPDATE table_name tn SET tn.Username=username WHERE tn.Username=uname;
the query i have used is :
update file_structure fs set fs.active_status = 'N' where fs.fileid = temp_fileid and fs.appid = temp_appid;
Related
Please support, I had a stored procedure create in sql server database using a Merge statement inside. I would like to use the same stored procedure in a Mysql database. Unfortunatly it seem the Merge funtion not work in MySql. Anybody can help me to do that ? Below my stored procedure
ALTER PROCEDURE [dbo].[ValiderFacturePharmacie]
#numdossierhospi VARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
MERGE INTO pharmacie P
USING (SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = #numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
WHEN MATCHED THEN
UPDATE SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END
Thanks you for your answer ! I solved the issue by using this statement below
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ValiderFacturePharmacie`(IN `numdossierhospi` VARCHAR(30))
NO SQL
UPDATE pharmacies
SET totalpharma = (select SUM(totalprod) from pharmacies where numdosshp = numdossierhospi),
etat = 'FACTURER'
WHERE numdosshp = numdossierhospi$$
DELIMITER ;
It is alomst the same
DELIMITER //
CREATE PROCEDURE ValiderFacturePharmacie (
_numdossierhospi VARCHAR(50))
BEGIN
UPDATE pharmacie P INNER JOIN
(SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = _numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END //
DELIMITER ;
I have stored procedure as below:
DROP PROCEDURE IF EXISTS TEST;
CREATE PROCEDURE TEST()
BEGIN
IF (SELECT count(*) FROM (SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'User') A) > 0 THEN
ALTER TABLE User DROP FOREIGN KEY FK_forkey
END IF;
END
SELECT ....
I have the syntax error at END IF. I have been trying many ways to fix this but no success so far. If I add Delimiter // after the DROP PROCEDURE and after the END, I get error at the end Delimiter (delimiter is not valid input at this position). What did I miss/How did I do it wrong? Thank you in advance for your response.
UPDATED
DROP PROCEDURE IF EXISTS TEST;
DELIMITER //
CREATE PROCEDURE TEST()
BEGIN
IF EXISTS(SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'User')
THEN
ALTER TABLE User DROP FOREIGN KEY FK_AccountID;
END IF;
END //
DELIMITER;
The updated above is my new change and it reports error at the end Delimiter: Delimiter is not valid input at this position
This answer is from #Solarflare, since he/she didn't post the answer, I'll do it instead so I could mark this question as answered and it may come in handy for others newbies that might run into the same problem as me.
You have to leave a space between delimiter and ;
I am facing trouble with comparing a value in table 1 against a string
Here is what ive tried..
DELIMITER $$
DROP TRIGGER IF EXISTS comment_trigger$$
CREATE TRIGGER comment_trigger AFTER INSERT ON post_comments
FOR EACH ROW
DECLARE
#var_post_id varchar(20);
BEGIN
IF (NEW.post_id LIKE 'rmdid%') THEN
SET #var_post_id=SELECT post_id FROM recommendations WHERE recommendation_id=NEW.post_id;
IF(#var_post_id LIKE 'recid%') THEN
UPDATE cooking_category SET last_activity=NEW.comment_time WHERE post_id=NEW.post_id;
END IF;
END IF;
END$$
DELIMITER ;
Can someone help me with this please.
You can assign the variable directly in the select statement:
SELECT #var_post_id = post_id
FROM recommendations
WHERE recommendation_id=NEW.post_id;
This is preferred, but your syntax would also work if you put parentheses around the select statement:
SET #var_post_id=(SELECT post_id FROM recommendations WHERE recommendation_id=NEW.post_id);
I'm coming from a MS SQL Server background.
Working on a new project using MySQL with NaviCat 8 Admin tools.
Ok, here's the question.
Normally when working in MS land if I want to update some data I use a stored procedure to do this:
Drop Procedure spNew
Create Procedure spNew (#P_Param)
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
I am trying to do this same logic from within NaviCat.
I defined the Parameter, (IN '#P_Param' int)
In the Definition i placed:
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
END;
When I try and save the stored procedure, i'm getting this error:
"1064 - You have an error in your SQL syntax, blah, blah, blah"
Can anyone at least point me in the right direction?
Thanks.
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
Note that MySQL syntax and overall ideology are very different from those of SQL Server.
You may also need to set delimiter:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
$$
DELIMITER ;
BTW, I'm assuming you don't actually call your table "Table", since it's a reserved word.
If you do, you need to enclose it into backticks like this:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE `Table`
SET `Field` = 'some value'
WHERE `ID` = P_Param;
END;
$$
DELIMITER ;
Parameters to MySQL stored procedures aren't prefixed with # or quoted in either the declaration or when used. Local variables are prefixed with #, however.
Try:
DROP PROCEDURE IF EXISTS spNew;
CREATE PROCEDURE spNew(IN P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param
END;
I must be overlooking something simple. I'm setting a variable from a query result in a MySQL stored procedure like this:
SELECT #myName := username FROM User WHERE ID=1;
So, #myName is storing the username of userid 1, which is 'Paul'. Great.
But later in the stored procedure I run an update on this record:
UPDATE User SET username = 'Fred' WHERE ID=1;
Now, for some reason, #myName = 'Fred' when it should still equal 'Paul', right? It appears MySQL is just creating a pointer to the record rather than storing a static value in the #myName variable.
So in short, I want to be able to store a value in a variable from a query result and assure the value of my variable doesn't change, even when the data in the table it was set from changes.
What am I missing? Thanks in advance!
That's very surprising, I agree. I'm not sure how to explain it, but for what it's worth, try this instead:
SELECT username INTO myName FROM User WHERE ID=1;
See http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html
update: I'm trying to reproduce this problem, but I can't. I'm using MySQL 5.0.51 on Mac OS X.
drop table if exists user;
create table user (
id serial primary key,
username varchar(10)
);
insert into user (username) values ('Paul');
drop procedure if exists doit;
delimiter !!
create procedure doit()
begin
declare name varchar(10);
select #name:=username from user where id=1;
select #name; -- shows 'Paul' as expected
update user set username = 'Fred' where id=1;
select #name; -- still shows 'Paul'
end!!
delimiter ;
call doit();
Can you try the code above in your test database and let us know if it exhibits the problem you describe?
Variable #name still has the value Paul.
If you want to update your variable you should assign the new value :
drop procedure if exists doit;
delimiter !!
create procedure doit()
begin
declare name varchar(10);
select #name:=username from user where id=1;
select #name; -- shows 'Paul' as expected
update user set username = 'Fred' where id=1;
select #name:=username from user where id=1; -- this will update the #name value
select #name; -- shows 'Fred'
end!!
delimiter ;
call doit();
drop table if exists user;
create table user (
id serial primary key,
username varchar(10)
);
insert into user (username) values ('Paul');
drop procedure if exists doit;
delimiter !!
create procedure doit()
begin
declare name varchar(10);
select #name:=username from user where id=1;
select #name; -- shows 'Paul' as expected
set #name = '' /*Initialize #name with '' and then get the result as you expected*/
update user set username = 'Fred' where id=1;
select #name; -- now you will get exepected results
end!!
delimiter ;
call doit();
Regards,
Jitendra Pasi.(Ugam)