1

I have read a few articles listed below to try and find an answer to my problem. They both seemed to have the same issue.

I saw this one but I read in the MySQL documentation that all procedures are by default non deterministic.

This one doesn't have anything to do with my problem because its SQL Server (not MySQL).

Here is the query I am using to create my procedure:

CREATE PROCEDURE getcusbyzip(zipcode VARCHAR(30),radius VARCHAR(30))
SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (

SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (

SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (

SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (

SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <radius
ORDER BY distance
LIMIT 0 , 20
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE )

Now If I run this query after that one:

CALL getcusbyzip(08360,50)

I get zero rows returned. But if I run the exact same statement inside the procedure as just a query and put the params in like this:

SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (

SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (

SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (

SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (

SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <50
ORDER BY distance
LIMIT 0 , 20
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE )

I get exactly what I wanted. The answer should only be one row but why is the result coming to me while not in a procedure but when I put it in one it will not give me the row.

I thought maybe it was because I did not have BEGIN and END but when I place that after the create line and END after the last line it fails to produce the procedure.

Community
  • 1
  • 1
Mark
  • 911
  • 13
  • 30
  • you have linked to a SQL Server question! ..(2nd link) – Mitch Wheat Feb 10 '13 at 02:22
  • Yes I know, that's why I said it has nothing to do with my problem. I will add that reason specifically to my post though so others are not confused. – Mark Feb 10 '13 at 02:25
  • Seems to me like you're passing in strings to your stored procedure when you want integers. – Trevoke Feb 10 '13 at 02:41
  • Unfortunately its not that. The only one that would matter for an INT would be radius so I changed that one to an INT and it gave the same results. – Mark Feb 10 '13 at 02:49
  • zip codes should always be varchars. 08360, as an INT is actually 8360. Yeah, you can pad it and hack it if you want, but truly, it's a string. In the cases where you want zip codes between certain values, just do one-off conversions to an int. – Flat Cat Jan 02 '16 at 19:00

1 Answers1

1

Your sample code and stored procedure code are too different from one another.

In order to identify your problem, you need to do the following

At the top of your sample code (not SP)

  • Declare a variable called zipcode of type varchar(30)
  • Declare a variable called radius of type varchar(30)

Then, modify the rest of the sample code to be exactly the same as the stored procedure.

You will be able to identify the exactly problem using this method.

As mentioned in the comments, it is likely due to the abuse of varchars being converted to numerics.

Summary

Declare and set variables at the top of your sample code so that the rest of the sample is EXACTLY the same as your stored procedure. Then you can start to identify the problem.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • How will I be able to "Identify exactly (the) problem"? I will do this but I need to know how to identify the problem. Are you anticipating me getting an error message? – Mark Feb 10 '13 at 04:51
  • No, if you perform what I described, both sets of code will execute exactly the same. They are not operating the same, because the code is different. It isn't not working because you made a SP, it's not working because you made a SP and changed the code to use variables instead of hard-coded values. So, make the code the same, then start adjusting it piece by piece back to the way you had it and see what breaks first. – Brian Webster Feb 10 '13 at 05:53
  • can you tell me how I am supposed to declare these? I am adding DECLARE zipcode VARCHAR(30) DECLARE radius VARCHAR(30) set zipcode = 08360 set radius = 50 Can you tell me what I am doing wrong? I just want to figure this stupid problem out so I can learn how to do stored procedures and I need a working sample with regards to what I am doing. – Mark Feb 10 '13 at 06:28