I am trying to perform an INSERT INTO .. SELECT .. statement in a stored procedure in phpmyadmin. The statement works perfectly when I run it as SQL, but when I use a stored procedure, it fails without giving an error message.
stored procedure:
IN: v_activation_code varchar 15 utf-8
INSERT INTO tbl_users (username, firstname, name, email, password, status, studnr, address, city, dob, gender)
SELECT username, firstname, name, email, password, status, studnr, address, city, dob, gender
FROM tbl_users_non_activated
WHERE activation_code LIKE 'v_activation_code'
Thanks in advance!
Related
I want to do a stored procedure to add a new customer to a table named customers. I want to define the values but even if I choose to leave a field out that those field will be set to NULL and not to a blank string as it is now in the code. I have been thinking about how I can possibly write an if statement or a loop to check the input values.
I work in MySQL Workbench 8.0 if its to any help.
I did add a screenshot of the staff table under the code.
DROP PROCEDURE IF EXISTS add_customer;
DELIMITER //
CREATE PROCEDURE add_customer
(
in First_name TEXT,
in Last_name TEXT,
in Email VARCHAR(255),
in Adress TEXT,
in Postcode TEXT,
in Country TEXT,
in City TEXT,
in Phone TEXT,
-- in Points DECIMAL(9,2),
in Social_security_no TEXT,
in org_no TEXT,
in memberNO TEXT
)
BEGIN
INSERT INTO customers (id_customer, first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO)
VALUES (DEFAULT , first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO );
END //
DELIMITER ;
Screenshoot of the customers table
You can use nullif(varName,''). This will return null whn varName field is empty string. Use this on fields you want to:
INSERT INTO customers (id_customer, first_name, last_name, email, adress, postcode, country, city, phone, social_security_no, org_no, memberNO)
VALUES (DEFAULT , nullif(first_name,''), nullif(last_name,''), nullif(email,''), nullif(adress,''), nullif(postcode,''), nullif(country,''), nullif(city,''), nullif(phone,''), nullif(social_security_no,''), nullif(org_no,''), nullif(memberNO ,''));
I have created a table called Customers with a CustomerID, Last Name, First Name, Address, and City, but when I tried to use the INSERT INTO to add data and ran the SQL Statement, it gives me an error: "Syntax error in CREATE TABLE statement". Below is the SQL Statement I have so far:
CREATE TABLE Customers
(
CustomerID int,
LastName varchar(50),
FirstName varchar(50),
Address varchar(50),
City varchar(50)
);
INSERT INTO (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');
You are missing the table name from your INSERT statement:
INSERT INTO Customers (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');
As to why you got an error pointing to your CREATE TABLE statement (which looks correct), my guess is that Access tried to connect the botched INSERT statement to the create in an effort to parse everything correctly.
I have a feeling this is a simple error, but I do not know what I am doing wrong! I have one table in sqlite3 that has 33 fields, called "users" and want to import data for 7 of the fields from another table.
Here is what I am doing:
INSERT INTO users(id, username, password, firstName, lastName, email, membershipStart) SELECT(id, username, password, nicename, displayname, email, registered) FROM tempUSERS;
And then I get:
Error: near ",": syntax error
What is wrong with what I am doing??
-Raymosrunerx
You don't need the parens in the select:
INSERT INTO users(id, username, password, firstName, lastName, email, membershipStart)
SELECT id, username, password, nicename, displayname, email, registered
FROM tempUSERS;
When the SQL parser encounters parentheses, it is expecting a scalar expression or subquery. Your expression is clearly not a subquery, and commas are not appropriate in a scalar expression.
I'm copying a row in a table using this statement:
insert into Buyer (
version, creationDate, password, token, username, zip, city, lastname, firstname, preferredLanguage_id, title_id, contactEmail_id, active
) select
version, creationDate, password, token, "loadtest_1#example.com", zip, city, lastname, firstname, preferredLanguage_id, title_id, contactEmail_id, active
from Buyer where username="developer_de#example.com";
Only thing I change is the username/email. Now the number in the new username to be inserted, "loadtest_1#example.com", should increment every time. So the second should be loadtest_2..., loadtest_3 and so on. I don't really care at what number it starts as long as it's continuous, so taking the ID of the newly inserted row or the like would be totally okay.
Extra kudos for ideas on how to actually create a batch of these inserts so I don't have to run it X times.
You are selecting and inserting to same table and only change is username. what I see is you need a UPDATE statement rather like
update Buyer set username = 'loadtest_1#example.com'
where username="developer_de#example.com";
If it's test and you do really want to insert test data saying loadtest_1#example.com .. loadtest_100#example.com then you can use a while loop like
CREATE PROCEDURE usp_inserttestdata(total INT)
AS
BEGIN
DECLARE counter INT;
DECLARE uname varchar(20);
SET counter = 1;
label1: WHILE counter <= total DO
SET uname = concat('loadtest_', counter, '#example.com');
insert into Buyer (
version, creationDate, password, token, username, zip, city, lastname,
firstname, preferredLanguage_id, title_id, contactEmail_id, active)
select version, creationDate, password, token, uname, zip, city, lastname, firstname,
preferredLanguage_id, title_id, contactEmail_id, active
from Buyer where username="developer_de#example.com";
SET counter = counter + 1;
END WHILE label1;
END
Then call the procedure saying
CALL usp_inserttestdata 1000
You can use AFTER Trigger ON INSERT operation to achive your goal. In the body of this trigger update email and set corresponding value depends on auto increment id value.
I need some help with splitting an old customer database into customers and addresses
for example:
Lets call the old table TB_old
and the 2 new ones new_customer and new_address
TB_old have the following columns:
cust_id, firstname, lastname, address, city, postalcode, phone, email, password
new_customers got:
cust_id(new,A_I), firstname, lastname, phone, email, pass, address_id(link to new_address)
new_address got:
address_id(new), address, city, postalcode, cust_id (link to new_customers)
What I already have done:
INSERT INTO new_customers firstname, lastname, phone etc etc
SELECT TB_old.firstname TB_old.lastname etc etc from TB_old
Where I'm stuck at:
I'm stuck at inserting addresses to the new_address while updating it with the relative cust_id and updating the address_id inside new_customer with the relative address.
EDIT: extra image
to make it clear:
first thing: Addresses belong to a customer. With that in mind you can do it:
INSERT INTO new_customers
(cust_id, firstname, lastname, phone, email, password)
SELECT
(cust_id, firstname, lastname, phone, email, password)
FROM TB_old;
INSERT INTO new_address
(address_id, cust_id, address, city, postalcode)
SELECT
(null, cust_id, address, city, postalcode)
FROM TB_old;
;-)
you are need to link old key and new one. simpliest way: to add field oldKey to the first table, load data to it (storing old keys), and fill second table, using old keys for linking. after all - remove oldKey column.