1

I have to tables as: table1:

UID | COLLEGE_NAME | COLLEGE_ADDRESS
------------------------------------

table2:

UID | COMPANY_NAME | COMPANY_ADDRESS
------------------------------------

i have 2 queries:

select * from table1 where uid='$uid';   
select * from table2 where uid='$uid';

i want to write this two queries in one procedure.

2
  • 1
    do you want to combine their result? Commented Feb 22, 2013 at 9:35
  • ya i have to combine the results also. Commented Feb 22, 2013 at 9:36

3 Answers 3

2

structure for multiple select quires in single procedure:

CREATE PROCEDURE sample(l_uid INT) BEGIN

SELECT * FROM college_edu WHERE uid= l_uid;

SELECT * FROM work_experience WHERE uid= l_id;

END

Sign up to request clarification or add additional context in comments.

Comments

0

Here's the statement to create STORED PROCEDURE.

DELIMITER $$
CREATE PROCEDURE procedureName(IN _uid VARCHAR(15))
BEGIN
    SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address
    FROM    table1
    WHERE uid = _uid
    UNION ALL
    SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address
    FROM    table2
    WHERE uid = _uid
END $$
DELIMITER ;

notice that UNION has ALL keyword on it to add duplicate records on the result list. But if you prefer UNIQUE, remove the ALL keyword on the UNION.

2 Comments

what if i don't want to union result, i just want to inherit multiple select queries in procedure then?
@HarjeetJadeja Why did you accept this answer ? With this answer, you lose the information of whether that UID is for a college or company.
0

below code may serve you purpose. Also the additional tbl column will let you know from which table your data is coming and it would help in further manipulation.

 SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address , 1 as tbl
    FROM    table1
    WHERE uid = _uid
    UNION ALL
    SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address , 2 as tbl
    FROM    table2
    WHERE uid = _uid

1 Comment

what if i don't want to union result, i just want to inherit multiple select queries in procedure then?

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.