我想循环遍历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中实现这一目标?
答案 0 :(得分:1)
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');"