Jump to content

question about stored Procedure


ballouta

Recommended Posts

Hello

 

I am writing a Stored Procedure that take zip code as IN parameter and search for the ZIP CODE in zipcodes table

if exists it should provide city and state as outputs

otherwise, set the default and state as San Francisco and CA.

 

The question is HOW do i check if the zip code was found?

 

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
SELECT * FROM zipcodes where zip_code = zip_in;

END
delimiter

thanks

Link to comment
Share on other sites

Basically you have 2 options:

a) Do the checking in the same SP; for that you have to modify your SELECT and use it with the SELECT... INTO syntax like:

        SELECT city, state INTO city_out, state_out FROM zipcodes WHERE zip_code = zip_in;
        // and validate the values in city_out or state_out to determine if they have value after the select

 

b) or do the checking in your PHP code after you call the SP using any of the mysql API functions (mysql_num_rows($resultset), mysql_fetch.. etc).

 

As an additional suggestion: you should include in you SP the necessary coding to control possibles errors using the DECLARE HANDLER syntax.

 

a good source to learn how to use Stored Procedure is this PDF document... Error Handling is covered starting at the end of page 29

Link to comment
Share on other sites

You can do something like this:

 

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
SET @was_found  = (SELECT 1 FROM zipcodes where zip_code = zip_in);
IF @was_found = 1 THEN
	-- The value was found
ELSE
	-- The value wasn't found
END IF;
END
delimiter

 

This line:

SET @was_found  = (SELECT 1 FROM zipcodes where zip_code = zip_in);

@was_found will either equal 1 (found) or NULL (not found)

Link to comment
Share on other sites

You can do something like this:

 

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
SET @was_found  = (SELECT 1 FROM zipcodes where zip_code = zip_in);
IF @was_found = 1 THEN
	-- The value was found
ELSE
	-- The value wasn't found
END IF;
END
delimiter

 

This line:

SET @was_found  = (SELECT 1 FROM zipcodes where zip_code = zip_in);

@was_found will either equal 1 (found) or NULL (not found)

 

and how do you get the values for city_out and state_out that the OP needs?... doing other SELECT?.... that is the function of SELECT INTO 

Link to comment
Share on other sites

He can do this then:

 

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
create temporary table if not exists city(city char(15), state char(2));
insert into city select city, state from zipcodes where zip_code = zip_in;
set @rows = (select count(*) from city);

IF @rows = 1 THEN
	-- The value was found
	select * from city;
ELSE
	-- The value wasn't found
END IF;
END//

Link to comment
Share on other sites

your code doesn't make any sense at all...

 

Or do you just not understand it?

 

In my eyes it does exactly what he wants, I just didn't set the default to San Francisco, CA

 

mine takes the city (if found) and and state and adds it to a temp table.

 

it counts the rows in the temp table

 

if there is 1 row it was found, if there wasn't a row nothing was found.

Link to comment
Share on other sites

your code doesn't make any sense at all...

 

Or do you just not understand it?

 

In my eyes it does exactly what he wants, I just didn't set the default to San Francisco, CA

 

mine takes the city (if found) and and state and adds it to a temp table.

 

it counts the rows in the temp table

 

if there is 1 row it was found, if there wasn't a row nothing was found.

 

Oh boy.... Oh boy...

 

Your "code"

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
create temporary table if not exists city(city char(15), state char(2));
insert into city select city, state from zipcodes where zip_code = zip_in;
set @rows = (select count(*) from city);

IF @rows = 1 THEN
	-- The value was found
	select * from city;
ELSE
	-- The value wasn't found
END IF;
END//

 

how it should be (in its simple form no adding error handlers)

delimiter //
CREATE PROCEDURE zip_code_search (IN zip_in CHAR, OUT city_out VARCHAR, OUT state_out CHAR)
BEGIN
        SET city_out = '';
        SELECT city, state INTO city_out, state_out FROM zipcodes WHERE zip_code = zip_in;
IF (city_out = '') THEN
                SET city_out = 'San Francisco'; 
                SET state_out = 'CA';
END IF;
END//

 

and yes... you are right... I don't understand your "fantastic code" ... it is FUBAR!!  do yourself a favor a read a little the document that I did post for the OP  you really need it.

 

 

Link to comment
Share on other sites

Hello again!

 

I am wondering if this SP can be solved starting with this statement:

If exists (SELECT * FROM zipcodes where zip_code = zip_in)
Then

 

another thing i would like to know, can we user a cursor to fetch the City and State names?

 

if yes how would the SP look like?

 

I appreciate ur help

thanks

Link to comment
Share on other sites

Hello again!

 

I am wondering if this SP can be solved starting with this statement:

If exists (SELECT * FROM zipcodes where zip_code = zip_in)
Then

 

To validate the existence of the zip_code ...yes... however will be more efficient to write that in this way

  IF EXISTS (SELECT 1 FROM zipcodes WHERE zip_code = zip_in) THEN
     /// whatever
  ELSE
    // whatever
  END IF

 

another thing i would like to know, can we user a cursor to fetch the City and State names?

 

if yes how would the SP look like?

No clear why you will like to do that... answer depend on your objectives... why you don't tell us what exactly are you trying to do and we will try to answer accordingly.

 

Cursors are RecordSet and you can process them almost exactly the same as you process them in PHP, therefore if your objective is return in some way the city and state to your PHP code my original example could do exactly that without any further complication.

 

But as I said... no clear which are your objectives to give you a better answer

 

Link to comment
Share on other sites

Thank you for ur reply

I am asking specific questions because it is a homework and I am trying to solve it using some techniques

I saw in class.

 

i studies If Exists, this is why

Moreover, I wanted to know

if I say

 

If exists (SELECT * FROM zipcodes where zip_code = zip_in)
THEN

how do i assign the arguments state and city OUT

we never had such example in class

 

thank you

Link to comment
Share on other sites

great... glad to help... read the document that I posted for you... it will help you to understand better Functions/Procedures and Cursors usage

 

how do i assign the arguments state and city OUT

we never had such example in classever

 

if you use the IF then you can't assign the city_out and state_out in that select  that is why a much better solution is to use the SELECT INTO syntax that I posted before... with the IF you are able to only check for existence of the zip code... to return the city_out, state_out in that schema you must to make another select which will be totally unneficient 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.