0

I'm having trouble creating a stored procedure with one IN and three OUT parameters. Basically I have three queries which I want to put in a stored procedure.

How can I relate the OUT paramters with each of the queries?

CREATE OR REPLACE PROCEDURE 'SP_NAME'(
 @phone_number IN VARCHAR2

 REG OUT INTEGER
 EMAIL OUT VARCHAR2
 VALID OUT INTEGER )

AS
BEGIN 

--I just need 1 or 0 to valid if exists on the table
SELECT COUNT (phone_number) FROM users
  WHERE phone_number = @phone_number


-- this should bring me the email a different table
SELECT email FROM details_users
  WHERE phone_number = @phone_number

-- and this bring if exists in oooother table 
SELECT count(phone_number) FROM suscribers
  WHERE phone_number = @phone_number
END;
1
  • 3
    Why is this tagged as oracle? Looks like SQL Server Commented Mar 17, 2018 at 21:38

2 Answers 2

1

Your revised business logic features three tables. In the absence of any indication that the tables contents are related it's better to keep them as separate queries.

Here is my solution (featuring valid Oracle syntax).

CREATE OR REPLACE PROCEDURE SP_NAME (
  p_phone_number IN VARCHAR2
  , p_REG OUT INTEGER
  , p_EMAIL OUT VARCHAR2
  , p_VALID OUT INTEGER )    
AS
  l_usr_cnt pls_integer;
  l_email details_users.email%type;
  l_sub_cnt pls_integer;
BEGIN 
  SELECT COUNT (*) into l_usr_cnt
  FROM users u
  WHERE u.phone_number = p_phone_number;

  begin
      select du.email into l_email
      from details_users du
      where du.phone_number = p_phone_number;
  exception
      when others then
        l_email := null;
  end;

  SELECT COUNT (*) into l_sub_cnt
  FROM suscribers s -- probably a typo :-/
  WHERE s.phone_number = p_phone_number;

  if l_usr_cnt != 0 then
    p_reg := 1; -- "true"
  else
    p_reg := 0; -- "false"
  end if;
  p_email := l_email;
  if l_sub_cnt != 0 then
    p_valid := 1;
  else
    p_valid := 0;
  end if;

END;

Note it is permitted to populate an OUT parameter directly from a query, like this:

  SELECT COUNT (*) 
  into p_reg
  FROM users u
  WHERE u.phone_number = p_phone_number;

However, it is accepted good practice to work with local variables and only assign OUT parameters at the end of the procedure. This is to ensure consistent state is passed to the calling program, (which matters particularly if the called procedure throws an exception).

Also, I prefixed all the parameter names with p_. This is also optional but good practice. Having distinct namespaces is always safer, but it especially matters when the parameter names would otherwise match table column names (i.e. phone_number).

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

3 Comments

thanks for the heads up I correct my query, basicaly I need 2 booleans out and one string and I really dont know how to relate with each query, hope the you can help
For future reference it would be better if you wrote a question that actually reflected what you're trying to achieve instead of wasting peoples' time with some dashed-off nonsense.
once again thanks, and how it would be if I have an Array as a p_ parameter ? just by curiosity !!!!
0

You may use below queries then assign the result to each of the out parameters. I notice that 1st and 3rd query are the same. Is it copy/paste error? Or you mean phone number is valid or not?

SELECT COUNT (phone_number) INTO REG FROM users
WHERE phone_number = @phone_number;

SELECT email INTO EMAIL FROM users
WHERE phone_number = @phone_number;

REG := VALID;

Comments

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.