I am using CTE to get records and loop through the CTE and perform some operations on the data.
Here is the table
Table A
Id | Name
---+-----------------------------
1 | Samuel,Jack
2 | Williams,In: Edison,Thomas
3 | Wick,John : Isac, Newton
I am creating a CTE to filter records which has ':' in data.
;WITH cte
(
SELECT Name AS filteredNames
FROM TableA
WHERE Name LIKE '%:%'
)
SELECT * FROM cte
will return rows similar to the one below
filteredNames
--------------
Williams,In: Edison,Thomas
Wick,John : Isac, Newton
I want to read row by row from the CTE and perform logic to swap the names separated by ','
keeping ':'
on the name.
Final output should be like this:
finalOutput
--------------
In,Williams: Thomas,Edison
John, Wick : Newton,Isac
I need to set each row to variable some thing like
DECLARE @tempName nvarchar(500)
SET @tempName = SELECT filteredNames FROM cte
I want to loop through cte
and set each row value to @tempName
variable and perform logic on the @tempname.
Please let me know how can we read row by row. Is there any chance to do without using a cursor?