sql - Different results from executing a stored procedure using exec sp_executesql compared to EXEC -


i have table in database consists of postcodes latitude , longitude example:

id  postcode    latitude         longitude 1   ll29 8ht    53.2973289489746    -3.72970223426819 

the table generated mvc code-first application, latitude , longitude data type real

i have stored procedure takes input searchlatitude , searchlongitude , calculates distances:

create procedure [dbo].[stockistsearch]      @searchlat float = 0,     @searchlng float = 0 begin     set nocount on; select top 40  s.id id, a.company company, a.firstname firstname, a.lastname lastname, a.address1 address1, a.address2 address2, a.city city, a.zippostalcode zippostalcode, a.phonenumber phonenumber, a.faxnumber faxnumber, a.email email, s.latitude latitude, s.longitude longitude, ( 3959 * acos( cos( radians(@searchlat) ) * cos( radians( s.latitude ) ) * cos( radians( s.longitude ) - radians(@searchlng) ) + sin( radians(@searchlat) ) * sin( radians( s.latitude ) ) ) ) distance stockist s, customer c, address s.customerid = c.id , c.shippingaddress_id = a.id      order distance end 

if execute through sql server management studio, executes following:

declare @return_value int  exec    @return_value = [dbo].[stockistsearch]     @searchlat = 53.29,     @searchlng = -3.72  select  'return value' = @return_value  go 

which return distance correctly (0.645 miles)

however, when executed through application using :

var result = this.database.sqlquery<tentity>(commandtext, parameters).tolist(); 

which according trace, executes:

exec sp_executesql n'stockistsearch',n'@searchlat float,@searchlng float',@searchlat=53.29,@searchlng=-3.72 

which results in different (and wrong) result calculated distance, (it returns distance 3688.96 miles!)

i've stepped through code in debug , parameters being entered correctly , have confirmed exec sp_executesql giving different result exec when ran directly in sql server management studio

i'm @ loss why happening , although stored procedure seems functioning correctly, results showing through application wrong.

please can please give me advice before go mad!!

thanks

update

thanks comments far , @mckay, i've narrowed down ignoring input parameters when called exec sp_executesql , using defaults 0 both latitude , longitude. if remove defaults, error:

procedure or function 'stockistsearch' expects parameter '@searchlat', not supplied. 

the sql query is

exec sp_executesql n'stockistsearch', n'@searchlat real, @searchlng real',@searchlat=53.29,@searchlng=-3.72 

this sql automatically generate entity framework (code-first mvc ef doesn't support stored procedures should able query stored procedures using following in dbcontext:)

var result = this.database.sqlquery<tentity>(commandtext, parameters).tolist(); 

would appreciate further help

fixed :-)

when calling :

var result = this.database.sqlquery<tentity>(commandtext, parameters).tolist(); 

the command text has have parameters appended so

commandtext = "stockistsearch @searchlat,@searchlng" 

so resulting sql is;

exec sp_executesql n'stockistsearch @searchlat,@searchlng', n'@searchlat real, @searchlng real',@searchlat=53.29,@searchlng=-3.72 

it accepts input parameters , calculates correct distance

numbers different make me think maybe it's using default values something? have taken out defaults?

maybe add parameters select, make sure you're expecting.

select top 40      @searchlatitude,     @searchlongitude,     l.id id,     l.postcode postcode,     l.latitude latitude,     l.longitude longitude, 

this might aid in debugging process.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -