Hi
I guess I know what you mean now. Actually Select S.SectyCode From tblMain2 as M LEFT JOIN tblSectyData as S ON M.Code = S.Code WHERE M.Code = @id would return a talbe, but in your stored procedure, you have assigned [SectyCode] 's value to @Code,
Select @Code = [SectyCode] From tblMain2 as M LEFT JOIN tblSectyData as S ON M.Code = S.Code WHERE M.Code = @id
so,as a result, you can only get one record in that table. that's the problem.
To fix this, you need to re-write your stord procedure. As far as I can see, You need to join [tblMain2] ,[tblSectyData ], and [text] to accomplish this job. Another solution is to use cursor (as you have mentioned," I guess I need a ForEach setup so for each id that pops up, it does the next step"). However, using cursor would involve some tricky steps...
Hope my suggestion helps
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.