I am new to PL SQL and still learning, but I need to solve a problem and I don't know enough about PL SQL to solve my problem quickly.
I'm referencing two tables: users and attributes. I have a procedure that takes 3 arguments: attrib_id, uid, attrib_value.
I first queried the attributes table with the attrib_id to return an attribute name and assign it to a variable. My code works up to this point.
Next I want to use the variable created from the previous select statement in another select statement to query the users table and return the current value associated with the attribute that the variable represents.
CODE:
PROCEDURE value_update_proc_z(attrib_id INTEGER, uid IN VARCHAR2, attrib_value IN VARCHAR2)
IS
v_old_attrib_name attributes.attribute_name%TYPE;
v_oldattrib_value varchar2(100);
v_mymsg varchar2(2000);
BEGIN
EXECUTE IMMEDIATE 'SELECT attrib_name FROM attributes WHERE indx = ''' || attrib_id || '''' INTO v_old_attrib_name;
EXECUTE IMMEDIATE 'SELECT' || v_old_attrib_name || 'FROM USERS WHERE USERID = ''' || uid || '''' INTO v_oldattrib_value;
v_mymsg := v_old_attrib_name || ' ' || v_oldattrib_value;
END value_update_proc_z;
The first query should return a value from the attributes table based on a number passed into the procedure. For example, if attrib_id = 1, the query would return first_name, if attrib_id = 2, then last_name returned, and if attrib_id = 3, the would be returned email. The returned value would be assigned to the variable v_old_attrib_name.
Using the variable v_old_attrib_name in my select statement, I would expect that the second query would return a value, example; last_name would return williams, or email would return [email protected]. The result of this query would be assigned to the variable v_oldattrib_value.
Currently, the first Execute Immediate works and when I display the message I can see the value of that variable, but when I add the second Execute Immediate, I get a message that the operation could not be completed. This isn't an error generated by the system, it is a message set up by a previous developer.
I am open to suggestions for improvement.
Thanks!
select columnname from users where userid = :userid