简体   繁体   中英

Inserting a row in SQL and setting up variables in a stored procedure?

I was wondering if anyone knew or if it is possible to set (id, FirstName, LastName) inside the procedure below so I could insert new customers by typing...

CALL ('1','John','Smith');
CALL ('2', 'Jane','Smith'); and so on.

At the moment my code looks like this. I am using phpMyAdmin

CREATE PROCEDURE spUpdateCustomers(id INT(10),
FirstName VARCHAR(30), 
LastName VARCHAR(30))
BEGIN   
INSERT INTO customers (CustomerID, CustomerFirstName, CustomerLastName) VALUES (id , FirstName, LastName);
END

Then I call it using.

SET @id='1'; 
SET @FirstName='John'; 
SET @LastName='Smith'; 
CALL spUpdateCustomer(@id, @firstName, @lastName);

I am aiming to avoid setting each variable before every call. Any help would be appreciated!

You do not need to define variables for parameter values:

CALL spUpdateCustomer(1, 'John', 'Smith');

Note: The first parameter of your procedure is defined as an integer. MySQL implicitly casts '1' to 1 (string to integer). Nevertheless, if you are executing the procedure on the database server, it would be a good practice to use strictly integer values for integer parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM