Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 08:33:10
|
Hi there, I'm trying to use a SP and a function which I found somewhere, only I get an error.The function looks like this:CREATE FUNCTION [dbo].[CalculateDistance]( @LATI float, @LNG float, @DBLAT float, @DBLONG float)RETURNS floatAS BEGIN RETURN 6371.04 * acos(cos(pi() / 2 - radians(90.0 - @DBLAT)) *COS(PI() / 2 - RADIANS(90.0 - @LATI)) * COS(RADIANS(@DBLONG) -RADIANS(@LNG)) + SIN(PI() / 2 - RADIANS(90.0 - @DBLAT)) * SIN(PI() / 2- RADIANS(90.0 - @LATI))) END And the sp looks like this:CREATE PROCEDURE [dbo].[FindNearest]( @LATI float, @LNG float, @Distance float)ASSELECT dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) AS dist, LocatieID, LocatieNaam, LocatieBezoekStraat, LocatieBezoekNummer, LocatieBezoekPostcode, LocatieBezoekPlaatsnaam, LocatieHoofdNetwerkID, LocatieNetwerkPartner, LocatieLon, LocatieLat, LocatieZoomFROM ZMLocatieWHERE dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) < @DistanceORDER BY distWhen I try to run it, SQL comes up with:Msg 119, Level 15, State 1, Line 4Must pass parameter number 2 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'.Can anyone explain what this means, and what I should do about it? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-29 : 08:38:39
|
ORDER BY 1 dist DESCPeter LarssonHelsingborg, Sweden |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-29 : 08:44:02
|
or you need to use it as Derived tableSelect * from(your query) T order by DISTCan you show us how you executed that procedure?MadhivananFailing to plan is Planning to fail |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 08:47:13
|
ORDER BY 1 DESCDoes not seem to matter, any other ideas? |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 08:48:24
|
It said:USE [npz]GODECLARE @return_value intEXEC @return_value = [dbo].[FindNearest] @LATI = 52,352723, @LNG = 4,863569, @Distance = 2SELECT 'Return Value' = @return_valueGO |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 08:57:39
|
Also tried derived table, does not seem to work.... |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-29 : 08:58:45
|
You should use Output parameter to get value into a VariableRead about it sql server help fileMadhivananFailing to plan is Planning to fail |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 09:01:24
|
I think I understand what you mean, but how and where do you do that.declare @somevariableand then... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-29 : 09:02:10
|
quote: Originally posted by trouble2 ORDER BY 1 DESCDoes not seem to matter, any other ideas?
Did you even try to test?You can't ORDER BY aliases, as you can do in ACCESS.If you don't understand ORDER BY 1 DESC, just tell us, or tryORDER BY dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) DESC, which is the same thing.Peter LarssonHelsingborg, Sweden |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 09:08:45
|
Don't get angry, I just didn't know...Indeed I thought it was some kind of alias, and I did try.Like I said... it didn't work.It seems I've got to use output parameters... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-29 : 09:24:49
|
Either of these two queries work great for meSELECT dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) AS dist, LocatieID, LocatieNaam, LocatieBezoekStraat, LocatieBezoekNummer, LocatieBezoekPostcode, LocatieBezoekPlaatsnaam, LocatieHoofdNetwerkID, LocatieNetwerkPartner, LocatieLon, LocatieLat, LocatieZoomFROM ZMLocatieWHERE dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) < @DistanceORDER BY 1 And thisselect d.*FROM ( SELECT dbo.CalculateDistance(@LATI, @LNG, LocatieLat, LocatieLon) AS Dist, LocatieID, LocatieNaam, LocatieBezoekStraat, LocatieBezoekNummer, LocatieBezoekPostcode, LocatieBezoekPlaatsnaam, LocatieHoofdNetwerkID, LocatieNetwerkPartner, LocatieLon, LocatieLat, LocatieZoom FROM ZMLocatie ) dWHERE d.Dist < @DistanceORDER BY d.Dist Peter LarssonHelsingborg, Sweden |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-29 : 09:29:00
|
Peso, the problem is that the questioner is not using Output parameter MadhivananFailing to plan is Planning to fail |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 09:38:12
|
Yes, well now that we know, could you please, please, please help me with that...I don't understand what I am doing wrong. |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-09-29 : 11:33:53
|
It had nothing to do with output parameters... check my values:@LATI = 52,352723,@LNG = 4,863569,I should have done:@LATI = 52.352723,@LNG = 4.863569, |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-10-02 : 03:29:55
|
quote: Originally posted by trouble2 It had nothing to do with output parameters... check my values:@LATI = 52,352723,@LNG = 4,863569,I should have done:@LATI = 52.352723,@LNG = 4.863569,
Failing to look is uuuhm.... well what is it?!?!??!??! |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-02 : 08:41:54
|
"When I try to run it, SQL comes up with:Msg 119, Level 15, State 1, Line 4Must pass parameter number 2 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'."I don't see this answered, in case its still an issue then in general you can do:EXEC dbo.FindNearest @LATI=1.234, @LNG=4.5678, @Distance=6.7890andEXEC dbo.FindNearest 1.234, 4.5678, 6.7890but that's not recommended in case the sequence of declared parameters changes in the futureand you can even doEXEC dbo.FindNearest 1.234, 4.5678, @Distance=6.7890which is handy if you want to leave out, say, the 3rd, 4th ... parameters [assuming they are optional] and just specify the Nth parameters. Again, not recommended for the first 2 parameters to not have their names.But you cannot doEXEC dbo.FindNearest @LATI=1.234, @LNG=4.5678, 6.7890i.e. name some parameters and NOT all the REMAINING parameters, so this would also be wrong:EXEC dbo.FindNearest 1.234, @LNG=4.5678, 6.7890but only with regard to the last parameter.I always name the parameters so I'm a bit hazy on the exact details of this, but I think there are some circumstances where you can get away with this which may relate to compulsory, rather than optional, Sproc parameters being unnamed, so you may be able to get away with "looser" code than I'm suggesting, but either way I think that not naming the parameters to an SProc call is bad practice.Unfortunately you cannot name parameters in a Function call, so that rather weakens the deal - you could easily pass Lat/Long/DBlat and Distance in the wrong order - all the figures look pretty similar, after all!"You can't ORDER BY aliases, as you can do in ACCESS"You sure Peso?SELECT [Dist] = D, [Lat] = LFROM( SELECT [D] = 1, [L] = 200 UNION ALL SELECT [D] = 2, [L] = 100) AS XORDER BY [dist] or am I misunderstanding something?Kristen |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-02 : 09:01:53
|
No, you're right. My bad.When did that change? I have avoided using ORDER BY aliases since from a long time ago when it didn't work. I assumed it was still so. Hey, I learned something new today!Peter LarssonHelsingborg, Sweden |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-02 : 09:48:47
|
>>When did that change?Without any notice MadhivananFailing to plan is Planning to fail |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-02 : 10:09:53
|
"I have avoided using ORDER BY aliases since from a long time ago when it didn't work"I think the practice (old and new way) is a bit dubious:SELECT [MyDate] = CONVERT(varchar(11), MyDate, 11)FROM MyTableORDER BY MyDateis now ordering alphabetically, rather than chronologically!Bug, Feature, Feature-introduced-by-Bug-fix or Bug-introduced-by-Feature!Kristen |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2006-10-02 : 10:25:35
|
Thanks anyway....It's working great. I've found it on the google maps api blog.Works really nicely with the google maps api to calculate points which fall within some radius (in km) of another point.So this way I can make people enter their zip code and find some locations within 5 - 10 - 15 km's. Very cool. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-02 : 10:26:30
|
Kristen, How about these?Select ISNUMERIC(12e3)Select ISDATE(2005)Select cast('2006' as Datetime)Select cast(2006 as Datetime)MadhivananFailing to plan is Planning to fail |
|
|
Next Page
|
|
|
|
|