I have a table with names (names). I have another that holds ids (user).
I have a conditional construct - a case-when statement that is supposed to insert an id into the user table if a name in the names table matches a certain condition.
I have used like % to match string patterns:
delimiter //
create procedure name_matching (in names.name varchar, out id int)
begin
case
when names.name like 's%_%a' then
insert into user (id) values ('1');
else
insert into user (id) values ('2');
end case
end//
This outputs error 1064 on mysql terminal.
Is there a way to do this differently?
There are a couple of small problems with your procedure. You need a semicolon after the end case, you need to specify a field size for the varchar in the input parameter list, etc. The following works for me on MySQL 5.1 ("works" meaning: does not produce error when creating procedure):
delimiter $$
create procedure name_matching (in name varchar(500))
begin
case
when name like 's%_%a' then
insert into user (id) values (1);
else
insert into user (id) values (2);
end case;
end $$
Related
DROP PROCEDURE IF EXISTS kund2orderNew;
DELIMITER ;;
CREATE PROCEDURE kund2orderNew(kundId2 INT)
BEGIN
IF kundId2 <> (SELECT kundId FROM kund2order) THEN
INSERT INTO kundOrder VALUES ();
INSERT INTO kund2order VALUES (kundId2, (SELECT id FROM kundOrder));
END IF;
END
;;
DELIMITER ;
Alright am I doing something wrong here? What im trying to do is to check if kundId is in the kund2order, if its not then what I want to do is create a new row in the kundOrder table that just uses the default values and then take the recently created id from that row in the kundOrder and put it inside the new row in kund2order (together with kundId).
For some reason it just gives me (node:18328) UnhandledPromiseRejectionWarning: Error: ER_BAD_NULL_ERROR: Column 'kundId' cannot be null
I am a bit confused as to what the problem is, both tables are empty after I have called this procedure. Is the problem my if statement or is it something else?
That's not the correct way to check if an ID is already in the table. When you use a SELECT query as an expression, it has to return just one row. You can use:
IF NOT EXISTS (SELECT * FROM kund2Order WHERE kundId = kundId2) THEN
And if you want to insert the auto-increment of the row that was just inserted into kundOrder, you should use LAST_INSERT_ID():
INSERT INTO kund2order VALUES (kundId2, LAST_INSERT_ID());
Using a single MySQL procedure i need to write queries to get data from a table (where the results can be of a list also) after getting the results,need to insert those selected rows into another table of the same database..I'm finding dificulty in getting the result of the select query and fetching values to insert into another table...
Iam able to do the above one if it returns only one row but in my case it can return any number of rows...
DELIMITER $$
USE `scmn_nov21`$$
DROP PROCEDURE IF EXISTS `procedure1`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `procedure1`(
IN Param1 VARCHAR(255),
OUT Param2 VARCHAR(255),
OUT Param3 VARCHAR(255)
)
BEGIN
DECLARE myvar TEXT;
SET myvar = (SELECT column1 FROM table1 WHERE column1 =2);
INSERT INTO table1 (column1,column2,column3)
VALUES (myvar,'Malaysia','Asia');
COMMIT;
END$$
DELIMITER ;
I believe that you can do a Create as select: http://dev.mysql.com/doc/refman/5.7/en/create-table-select.html
Returning malassia and asia as fixed values for your query...
I have two tables. The first is my person table which has id, name, creation_date as values, I have a old_person table (id, name, modified_date) which I want to populate the value of person before it actually changes. How would I go about that? I have tried triggers but failed.
I tried as follows
create trigger Person_Trigger Update on person
before update
as
insert into old_person(id, name, modified)
select id, new.name, getdate()
from person
It's giving me syntax errors...Not many Trigger references out there either, a little push would be greatly appreciated!
Have a look at the following example
SQL Fiddle DEMO
IO hade a bit of trouble myself, but from How to Create Triggers in MySQL there was a line that made me think
The first MySQL command we’ll issue is a little unusual:
DELIMITER $$
Our trigger body requires a number of SQL commands separated by a
semi-colon (;). To create the full trigger code we must change
delimiter to something else — such as $$.
Finally, we set the delimiter back to a semi-colon:
DELIMITER ;
So in the SQL Fiddle I changed the query terminator to GO and that seemed top work.
CREATE TABLE person
(
id INT,
name varchar(20)
)
GO
CREATE TABLE old_person
(
id INT,
name varchar(20),
modified DATETIME
)
GO
CREATE TRIGGER Person_Trigger before update on person
FOR EACH ROW BEGIN
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, NOW());
END
GO
INSERT INTO person VALUES (1,'TADA')
GO
UPDATE person SET name = 'FOO'
GO
Try this:
DELIMITER \\
CREATE TRIGGER `Person_Trigger`
BEFORE UPDATE ON `Person`
FOR EACH ROW
BEGIN
DECLARE date_modified datetime;
SET date_modified = NOW();
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, date_modified);
END\\
This syntax works for me on my own projects. You may also need to declare delimiters before you begin the trigger. Also if you want to use the NEW keyword it should be AFTER update. Switch to the OLD keyword if you are going to keep using BEFORE update on your trigger.
I'm trying to create a trigger, however I keep getting back a syntax error.
Here's the statement:
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END
DELIMITER ;
I have a registration form on my site. When a person registers it populates the Candidates table with their profile information.
In the Candidates table, there are various fields, two of them being 'email' and 'UserID'.
UserID is also the PK in 'useradmin', so I'm linking the two up.
So when a user registers, I need to insert a record into 'useradmin' with the email address that's just been used to register, and then update the 'Candidates' table, with UserID that's just been created in 'useradmin'.
I hope this makes sense?
NB. I am changing the delimiter before running the statement.
Besides properly using DELIMITER when creating a trigger you have at least two fundamental issues with your current code:
In MySQL you can't use issue a DML statement (in your case UPDATE) against a table (candidates) on which you defined a trigger (also candidates). Your only option is to use BEFORE trigger and set a value of userid column of a row being inserted to a proper value.
You can't arbitrarily reference a column (useradmin.userid) of a table out of the context like you did in your UPDATE. You didn't joined useradmin table or used it in a subquery.
That being said and assuming that userid in useradmin table is an auto_increment column your trigger might look like this
DELIMITER $$
CREATE TRIGGER after_candidate_insert
BEFORE INSERT ON candidates
FOR EACH ROW
BEGIN
INSERT INTO useradmin (`username`, `talent`) VALUES (NEW.email, 1);
SET NEW.userid = LAST_INSERT_ID();
END$$
DELIMITER ;
Here is SQLFiddle demo
You should use semicolon after end your insert query.
You can use INSERT ... ON DUPLICATE KEY UPDATE syntax for your purpose
try out this...
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END $$
DELIMITER ;
I am learning about sql databases. I am using mysql.
I have designed the tables, and queries. I am now working on the code to put stuff into the database.
I can not work out how to ensure that a record is unique when I have a text field. I tried to mark the part of the record that was not the pk (primary key) as unique, but when it is text it complains that it is not fixed length. I then played with the idea of conditionals in a stored procedure, but could not get it to work.
DELIMITER $$
DROP PROCEDURE IF EXISTS `experiment1`.`add_zzzz`$$
CREATE PROCEDURE `experiment1`.`add_zzzz` (IN v INT, IN n TEXT)
BEGIN
IF EXISTS (
SELECT value, name
FROM zzzz
WHERE value=v AND name=n
)
THEN
ELSE
INSERT INTO zzzz(value,name)
VALUES v,n;
END IF;
END$$
DELIMITER ;
So anyone know what I am doing wrong?
VARCHAR is not fixed and you can use unique index with it
more on http://www.w3schools.com/sql/sql_unique.asp
you can also use INSERT ignore
http://dev.mysql.com/doc/refman/5.5/en/insert.html