MySQL SP: ERROR 1241 (21000) - mysql

Hi im trying to insert using this sp with insert statment of:
call insertuser (1, '077788899965', 'Digest 1.0', ':=', 'asjdfhiuoadshgiadufg');
SP CODE:
DELIMITER$$
CREATE PROCEDURE insertuser (IN IdParam INT, IN UserNameParam varchar(64), IN AttributeParam varchar(64), IN OpParam char(2), IN ValueParam varchar(253))
BEGIN
// Delete user if they already exist
DELETE FROM radcheck
WHERE username = UserNameParam;
// Insert
INSERT INTO radcheck (id, username, atrribute, op, value)
SELECT (IdParam, UserNameParam, AttributeParam, OpParam, ValueParam);
END$$
But I am getting error of:
ERROR 1241 (21000): Operand should contain 1 column(s)
Any idea how to resolve this?

DELIMITER $$
CREATE PROCEDURE insertuser (IN IdParam INT, IN UserNameParam varchar(64),
IN AttributeParam varchar(64), IN OpParam char(2),
IN ValueParam varchar(253))
BEGIN
DELETE FROM radcheck
WHERE username = UserNameParam;
INSERT INTO radcheck (id, username, atrribute, op, `value`)
SELECT IdParam, UserNameParam, AttributeParam, OpParam, ValueParam;
END
$$
There were 2 problems:
missing space after DELIMITER
// is not a comment start in MySQL. Use /* */
If you use a SQL IDE like MySQL Workbench such errors will be highlighted and are easier to fix.

Related

What's the problem with my stored procedure?

I have to test some features with store procedures in mysql, so i trying to create one in MySQL:
CREATE OR REPLACE PROCEDURE insert_test(IN in_id Varchar(20), IN in_name Varchar(20))
BEGIN
insert into test(id, name) values(in_id, in_name);
END
but I have an error:
Error executing INSERT statement. Unknown column 'in_id' in 'field list' - Connection: MySQLConnection: 16ms
What's wrong with that? If i change in_d and in_name variables to static values like 'test1', 'test2' it works ok.
upd:
I tried with delimeters - no effect:
DELIMITER //
CREATE OR REPLACE PROCEDURE insert_test(IN in_id Varchar(10), IN in_name
Varchar(30))
BEGIN
insert into test(id, name) values(in_id, in_name);
END //
DELIMITER ;
Ok, i have created my table and store procedure via DB Browser in Intellij Idea, m.b. therw was a problem. I just recreated table and procedure via mysql console.

Insert current datetime into mysql table resulting an error

I am creating a procedure called AccLikesVid inserting into a table named a_likes_v (table infomation):
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), type BOOL)
BEGIN
IF (type = 1) THEN
INSERT INTO a_likes_v VALUES (username, vidid, NOW(), 1);
ENDIF;
END$$
But when I execute the above code, MySQL Workbench generates an error: Error Code 1193. Unknown system variable 'now'
Could you tell me what's wrong with my code?
You should use:
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), v_type BOOL)
BEGIN
IF (v_type = 1) THEN -- TYPE is keyword, avoid such identifiers
INSERT INTO a_likes_v(account_name, video_id, dtime, liked)
VALUES (username, vidid, NOW(), 1); -- avoid blind insert
END IF; -- END IF not ENDIF
END$$
DBFiddle Demo

Stored Procedure - Error #1064

Getting error during Creating Stored Procedure for Callable Statement:
I know some thing very simple going wrong, but i'm just unable to figure out!
My QUERY:
USE demo;
1. CREATE PROCEDURE
2. INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
3. BEGIN
4. INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
5. END;
/
SQL query:
CREATE PROCEDURE
INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
BEGIN
INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
Appreciate your help!
Thank for your time!
When you write a stored procedure in MySQL, you should use the DELIMITER statement. In addition, you should name your columns so they do not conflict with column names. And, when using INSERT always list the columns name. So:
DELIMITER $$
CREATE PROCEDURE INSERT_emp_data (
IN IN_ID INT,
IN IN_NAME VARCHAR(2),
IN IN_AGE INT,
IN IN_IMAGE BLOB
)
BEGIN
INSERT INTO emp_data(id, name, age, image)
VALUES(IN_ID, IN_NAME, IN_AGE, IN_IMAGE);
END;
$$
DELIMITER ;

Nested Stored Procedure in MYSQL has error

So I have this stored procedure, that I have tried to write a few different way, all to no avail.
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
((CALL GetUserId(USER_EMAIL)), NAME, GENDER);
END
I am getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CALL GetUserId(USER_EMAIL)), NAME, GENDER)' at line 6
I have tried tinkering with for a while.
GetUserId works. I tried to store the result in a temporary variable and then insert it but that did not work.
Not to be shameless but If you can determine a solution, a solution where the CALL GetUserId is stored in a variable would be best.
You can't use it like this. Rewrite your GetUserId procedure with an OUT parameter.
Something like this:
DELIMITER $$
CREATE PROCEDURE GetUserId(IN p_email varchar(20), OUT p_id int)
BEGIN
SELECT id INTO p_id FROM users where email = p_email;
/*or whatever your procedure does*/
END$$
DELIMITER ;
Then your procedure CreateHero would look like this:
DELIMITER $$
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
DECLARE v_id int;
CALL GetUserId(USER_EMAIL, v_id);
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
(v_id, NAME, GENDER);
END$$
DELIMITER ;
Old thread but could help some one looking for later...
Base on fancyPants answer but more beautiful like this:
DELIMITER $$
CREATE FUNCTION GetUserId(IN p_email varchar(20)) RETURNS int
BEGIN
RETURN(SELECT id FROM users where email = p_email);
/*or whatever your function does*/
END$$
DELIMITER ;
And then:
DELIMITER $$
CREATE PROCEDURE `CreateHero` (IN USER_EMAIL VARCHAR(40),
IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES (GetUserId(USER_EMAIL), NAME, GENDER);
END$$
DELIMITER ;

SQL STORED PROCEDURE SAVING INTO VARIABLE

Can anybody spot the problem with this stored procedure? mysql is reporting the following error:
1048 Column 'categoryID' cannot be null
DELIMITER $$
CREATE PROCEDURE catalogue_assign_product_to_subcategory(
IN inProductId INT,
IN insubCategoryId INT)
BEGIN
DECLARE catID INT;
SELECT subcategoryParent FROM tblSubcategory
WHERE subcategoryID = insubCategoryId INTO catID;
INSERT INTO tblProdCat (productID, categoryID)
VALUES (inProductId, 'catID');
END
'catID' should be without the singlequotes in the INSERT statement.
I guess the field is defined as INT, you now try to insert a STRING
INSERT INTO tblProdCat (productID, categoryID)
VALUES (inProductId, catID);