I want to loop through a result set in SQL. So far I only know how to do this in powershell. See below:
foreach ($TestName in $DSFailures | % {$_.TestName}) {
$Query= "USE TestDS
insert into #temptable
SELECT space(iteration * 4) + TheFullEntityName + ' (' + rtrim(TheType) + ')' as EntityName, *
FROM dbo.fn_DependantObjects('$TestName', 1, 0)
ORDER BY ThePath"
Invoke-Sqlcmd -ServerInstance "SQL2016" -Database "db" -Query $Query
How can I achieve this in SQL?