如何为SQL server中的每个循环使用

如何为SQL server中的每个循环使用,sql,sql-server,Sql,Sql Server,我想在SQL中循环一个结果集。到目前为止,我只知道如何在powershell中执行此操作。见下文: foreach ($TestName in $DSFailures | % {$_.TestName}) { $Query= "USE TestDS insert into #temptable SELECT space(iteration * 4) + TheFullEntityName + ' (' + rtrim(TheTy

我想在SQL中循环一个结果集。到目前为止,我只知道如何在powershell中执行此操作。见下文:

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 

我如何在SQL中实现这一点?

可能与@Oscar重复这是完全不同的
Here is the answer:

Function Get-FunctionDependencies{ 
 $FPM_Functions =  "select replace (name, '_CLR','') AS FPM_FunctionName
                    into #temptable1
                    from sys.objects
                    where name like 'fn_clr%'
                    select * from #temptable1"
  $GetCLRCallingFunctions = Invoke-Sqlcmd -ServerInstance "sql" -Database "DB" -Query  $FPM_Functions
  foreach ($FPM_FunctionName in $GetCLRCallingFunctions | % {$_.FPM_FunctionName}) {
  Write-Output "--These are the dependencies for $FPM_FunctionName" 
  $query1 = "  SELECT referencing_entity_name as [FPM Function Dependencies] FROM sys.dm_sql_referencing_entities ('dbo.$FPM_FunctionName', 'OBJECT');"