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
.