0

I have stored procedure in my database and i need to look up a table and cross reference an id, then using the returned row i need to insert information into another table, but i cant seem to use the infomation from the lookup into the insert. This is what i have..

BEGIN
#Routine body goes here...
SET @UID = uid;
SET @UIDTOFB = uid_to;

SET @SQLTEST = CONCAT('SELECT users.user_auto_id FROM users WHERE users.user_fb_uid=     @UIDTOFB LIMIT 1');
PREPARE sqlcmd from @SQLTEST;
EXECUTE sqlcmd;

INSERT INTO challenges(challenge_from_uid, challenge_to_uid, challenge_dateadded) VALUES(@UID, @SQLTEST.users.user_auto_id, now());

SET @LASTID = LAST_INSERT_ID();
SELECT @LASTID as id;

END

any help would be much appreciated!

1 Answer 1

1

This won't insert the value of @UIDTOFB since you missed some '. It takes this whole statement as one string and therefore the statement fails.

SET @SQLTEST = CONCAT('SELECT users.user_auto_id FROM users WHERE users.user_fb_uid=     @UIDTOFB LIMIT 1');
PREPARE sqlcmd from @SQLTEST;
EXECUTE sqlcmd;

Anyway I'd recommend you use parameters like this:

PREPARE sqlcmd from 'SELECT users.user_auto_id FROM users WHERE users.user_fb_uid= ? LIMIT 1';
EXECUTE sqlcmd USING @UIDTOFB;

You can read more about it here in the manual.

UPDATE: Now I get, what you want to do. Do it simply like this:

SELECT @anyVariable:=users.user_auto_id FROM users WHERE users.user_fb_uid= @UIDTOFB LIMIT 1;
INSERT INTO challenges(challenge_from_uid, challenge_to_uid, challenge_dateadded) VALUES(@UID, @anyVariable, now());
Sign up to request clarification or add additional context in comments.

2 Comments

How would i then use users.user_auto_id that is returned in the same procedure?
Thanks tombom! much appreciated, saved me hours of head aches ;) haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.