MySQL过程返回0行

时间:2018-03-24 20:48:36

标签: mysql sql stored-procedures latitude-longitude

我有一个MySQL脚本(使用Workbench),用于标识设置坐标对中位置的距离。返回某个半径范围内的那些位置,并对这些行进行大量计算。

问题在于,当我使用变量运行查询但不在过程中运行时,脚本按照我的预期运行。当我把它变成一个存储过程时,我输入的lat / long变量并不重要。但是,不会抛出任何错误。

我选择了我在proc中输入的lat / long变量,它们看起来符合预期,即它们具有正确的小数位数。

如果有人可以看一看并提出建议,我将非常感激。谢谢!

两个脚本如下所示。

/*Code run outside of procedure:*/

Set @Radius = 200,
@Latitude = 40.76,
@Longitude = -73.97;

Select
   *
From

          (
        Select
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible,
          sum(b.TIV) TIV,
            case 
            when sum(b.TIV) - (b.Excess + b.Deductible) >= b.Limit then (b.Limit * b.Line)
            when sum(b.TIV) - (b.Excess + b.Deductible) < b.Limit and sum(b.TIV) - (b.Excess + b.Deductible) > 0 then (sum(b.TIV) - (b.Excess + b.Deductible)) * b.Line
            when sum(b.TIV) - (b.Excess + b.Deductible) <= 0 then 0 end Exposure$
        From

                (
                Select
                  a.ID,
                  a.ContractID,
                  a.InsuredName,
                  a.UserDefined1,
                  a.Line,
                  a.Limit,
                  a.Excess,
                  a.Deductible,
                  TIV
                From

                      (
                        Select
                            a.ID,
                            a.ContractID,
                            a.InsuredName,
                            a.UserDefined1,
                            a.Line,
                            a.Limit,
                            a.Excess,
                            a.Deductible,
                            TIV,
                            case when cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude)) > 1 
                                then sign(cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude))) 
                                else (6378100 * acos(cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude)))) 
                                end Distance
                        From
                            Location l
                            inner join Account a on a.ID = l.ID
                            )a
                        Where
                          a.Distance <= @Radius

                )b

        Group by
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible
        )c
Where
  c.Exposure$ > 0

/*Stored procedure (returning 0 rows):*/

Delimiter //

Create Procedure sp_RingAccounts (in Radius int, in Latitude decimal(9,6), in Longitude decimal(9,6))

Begin

Select Latitude;
Select Longitude;
Select Radius;

Select
   *
From

          (
        Select
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible,
          sum(b.TIV) TIV,
            case 
            when sum(b.TIV) - (b.Excess + b.Deductible) >= b.Limit then (b.Limit * b.Line)
            when sum(b.TIV) - (b.Excess + b.Deductible) < b.Limit and sum(b.TIV) - (b.Excess + b.Deductible) > 0 then (sum(b.TIV) - (b.Excess + b.Deductible)) * b.Line
            when sum(b.TIV) - (b.Excess + b.Deductible) <= 0 then 0 end Exposure$
        From

                (
                Select
                  a.ID,
                  a.ContractID,
                  a.InsuredName,
                  a.UserDefined1,
                  a.Line,
                  a.Limit,
                  a.Excess,
                  a.Deductible,
                  TIV
                From

                      (
                        Select
                            a.ID,
                            a.ContractID,
                            a.InsuredName,
                            a.UserDefined1,
                            a.Line,
                            a.Limit,
                            a.Excess,
                            a.Deductible,
                            TIV,
                            case when cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude)) > 1 
                                then sign(cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude))) 
                                else (6378100 * acos(cos(radians(l.Latitude))*cos(radians(@Latitude))* cos(radians(@Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(@Latitude)))) 
                                end Distance
                        From
                            Location l
                            inner join Account a on a.ID = l.ID
                            )a
                        Where
                          a.Distance <= @Radius

                )b

        Group by
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible
        )c
Where
  c.Exposure$ > 0 ;

End ;

 //
 Delimiter ;

1 个答案:

答案 0 :(得分:0)

所以我发现问题是什么,我在程序体中使用@在我的变量前面,我不需要。

所以当我需要使用'Latitude'时,我正在放置@Latitude。可能应该早点发现这一点。我认为这样做并没有给我一个错误这一事实让我失望,并且对我使用SSMS所知道的偏见也让我把@放在变量前面。

工作脚本现在看起来像:

Delimiter //

Create Procedure sp_RingAccounts (in Radius int, in Latitude decimal(9,6), in Longitude decimal(9,6))

Begin

Select
   *
From

          (
        Select
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible,
          sum(b.TIV) TIV,
            case 
            when sum(b.TIV) - (b.Excess + b.Deductible) >= b.Limit then (b.Limit * b.Line)
            when sum(b.TIV) - (b.Excess + b.Deductible) < b.Limit and sum(b.TIV) - (b.Excess + b.Deductible) > 0 then (sum(b.TIV) - (b.Excess + b.Deductible)) * b.Line
            when sum(b.TIV) - (b.Excess + b.Deductible) <= 0 then 0 end Exposure$
        From

                (
                Select
                  a.ID,
                  a.ContractID,
                  a.InsuredName,
                  a.UserDefined1,
                  a.Line,
                  a.Limit,
                  a.Excess,
                  a.Deductible,
                  TIV
                From

                      (
                        Select
                            a.ID,
                            a.ContractID,
                            a.InsuredName,
                            a.UserDefined1,
                            a.Line,
                            a.Limit,
                            a.Excess,
                            a.Deductible,
                            TIV,
                            case when cos(radians(l.Latitude))*cos(radians(Latitude))* cos(radians(Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(Latitude)) > 1 
                                then sign(cos(radians(l.Latitude))*cos(radians(Latitude))* cos(radians(Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(Latitude))) 
                                else (6378100 * acos(cos(radians(l.Latitude))*cos(radians(Latitude))* cos(radians(Longitude)- radians(l.Longitude))+ sin(radians(l.Latitude))* sin(radians(Latitude)))) 
                                end Distance
                        From
                            Location l
                            inner join Account a on a.ID = l.ID
                            )a
                        Where
                          a.Distance <= Radius

                )b

        Group by
          b.ID,
          b.ContractID,
          b.InsuredName,
          b.UserDefined1,
          b.Line,
          b.Limit,
          b.Excess,
          b.Deductible
        )c
Where
  c.Exposure$ > 0 ;

End ;

 //
 Delimiter ;

谢谢, Jourden