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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 use EXEC in sub query

Author  Topic 

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2007-12-23 : 23:23:09
Hi

I have a stored procedure that does a post code lookup of addresses, I want to find all Addresses within a specific range of PostCodes. I have stored procedure (uspPostCodesWithinMyArea) that does some complex math to find all local post codes that are within my region and returns them as a list. Is it possible to call this procedure from my Sub Select. For example

SELECT Name, Address1
WHERE PostCode IN (exec uspPostCodesWithinMyArea @Latitude @Longitude)

My procedure uspPostCodesWithinMyArea is called from lots of places so it would be nice to keep this encapsulated by it self, and not cut and paste this every where

The only other way I thought of doing this was to put the list from uspPostCodesWithinMyArea into a temp table, but this makes the query much slower.

Any help would be much appreciated

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-24 : 00:54:11
I think since you need to call this at many places its better to put results onto temp table and take inner join wit temp table at whatever places you want to filter rather than calling SP everywhere. Try putting an index on PostCode column of temp table and main table. the above query will then become:-


SELECT Name, Address1
FROM Table t
INNER JOIN #temp tmp
ON t.PostCode=tmp.PostCode
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2007-12-24 : 01:26:24
Hi, thanks for the reply

The problem that im having with the temp table is that it takes too long to populate, as in some cases im inserting 30,0000 records. The insert command seems to be the bottle neck

Im doing a basic insert into #temp to populate it, could I speed this up some how?

many thanks once again



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-24 : 01:32:41
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2007-12-24 : 01:53:50
Hi,

Thanks for the link, is there anyway to use the OpenRowSet method without having to to hard code the database name, its just that this stored proc will be used on serveral different databases

thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-24 : 18:37:58
If you want to travel the dark side,
http://www.sommarskog.se/dynamic_sql.html



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -