Sunday, February 19, 2012

Can a sql 2005 function return more than a variable

Hi,

I have a sql 2005 function who return a distance from 2 zipcodes. This function is called from a Stored procedure like this :

SELECT *, dbo.fn_GetDistance (...) AS Distance

In this function, i have a Latitude and i want this Latitude to be also returned.

It is possible or a function can return only one variable?

If it is possible, what's the syntax of it?

Thanks in advance

You can return a table, something like this:

SETANSI_NULLSON

GO

SETQUOTED_IDENTIFIERON

GO

CREATEFUNCTION LongLatDistance

(

-- Add the parameters for the function here

@.ZipCode1varchar(10),@.ZipCode2varchar(10)

)

RETURNS @.ResultTableTABLE(LongitudeDecimal(18,6),Latitudedecimal(18,6),Distancedecimal(18,6))

AS

BEGIN

INSERTINTO @.ResultTable (Longitude,Latitude,Distance) SELECT FieldsFROM TableName

RETURN

END

GO

|||

And i call it how from the stored procedure?

|||

SELECT Longitude,Latitude,Distance FROM dbo.LongLatDistance('90210',92630')

I wasnt clear on what exactly else you wanted to return. The sample is only returning the coords of one zip code, you would modify it obviously for your situation.

No comments:

Post a Comment