views:

39

answers:

1

I have a query that looks like this:

DECLARE Match_Cursor CURSOR
FOR
   SELECT ID,UserKey,TypeCode
   FROM [DB1].Table1 as t1
OPEN Match_Cursor

FETCH NEXT FROM Match_cursor INTO @ID,@UserKey,@TypeCode;
   WHILE (@@FETCH_STATUS <> -1)
   BEGIN
      INSERT INTO #TempTable
          SELECT  t2.Name, t2.Address, t2.Country, @UserKey, @TypeCode
          FROM  [DB1].[DBO].udf_TableFunction(@ID) as t2
          where @typeCode = 142 AND t2.Country = 'US'
FETCH NEXT FROM Match_cursor INTO @ID,@UserKey,@TypeCode;
   END

SELECT * FROM #TempTable

Does anyone have suggestions for rewriting this using joins? Assume there is a foreign key relation ship between t1.ID and t2.ID.

A: 

Use cross apply to pass Table1.ID to udf_TableFunction.

Here is a pseudo code on how you might go about to do so. (I don't have access to SSMS right now so couldn't test it)

insert #TempTable(...)
select ...
from  table1 t1 cross apply [DBO].udf_TableFunction(t1.ID) t2
where ...
Sung Meister
Ended up breaking the table functions into the original query and using cross apply.
Abe Miessler