SQL Server通过CTE迭代

时间:2017-05-17 08:27:37

标签: sql-server loops cursor common-table-expression temp-tables

存储过程#1:(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [mPlayer pause]; [super viewWillDisappear:animated]; [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; } (void)remoteControlReceivedWithEvent:(UIEvent *)event { switch (event.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: if([mPlayer rate] == 0){ [mPlayer play]; } else { [mPlayer pause]; } break; case UIEventSubtypeRemoteControlPlay: [mPlayer play]; break; case UIEventSubtypeRemoteControlPause: [mPlayer pause]; break; default: break; } } 参数返回1行@CountryOrganizationID

存储过程#2:MeasurableID@OrganizationID参数创建一个CTE(使用@MeasurableID为每个ROW_NUMBER() OVER (PARTITION BY Entities.ID ORDER BY Contracts.UTCmatched desc)返回一个单独的行)

我试图将这些组合成一个存储过程,其中Entity param通过SP1代码返回(比如5)行;然后通过SP2代码迭代每一行以返回单个表(如果有20个实体则返回100行)。

我尝试过嵌套和递归的CTE都无济于事。我正在考虑一个循环/光标和/或填充最终结果到临时表中,但坦率地说有点丢失(超出我的经验水平)所以会很感激建议主要采取的方法。

提前致谢。

SP1

@Country

SP2

        SELECT
            dbo.Measurables.OrganizationID,
            dbo.Measurables.ID AS MeasurableID

            FROM dbo.Measurables INNER JOIN dbo.Organizations ON dbo.Organizations.ID = dbo.Measurables.OrganizationID

            WHERE   dbo.Measurables.EndUTC > SYSUTCDATETIME ( )  
                AND dbo.Measurables.OrganizationID IN
                                        (
                                                SELECT
                                                    dbo.Countries2Organizations.OrganizationID
                                                FROM
                                                    dbo.Countries2Organizations
                                                WHERE
                                                    dbo.Countries2Organizations.Sport IN 
                                                        (
                                                            SELECT
                                                                dbo.Countries2Sports.SportName
                                                            FROM
                                                                dbo.Countries2Sports
                                                            WHERE
                                                                dbo.Countries2Sports.CountryCode = @CountryCode
                                                        )
                                                AND (   dbo.Countries2Organizations.CountryCode = @CountryCode  OR dbo.Countries2Organizations.CountryCode = '')

                                        )

--Result:     OrganizationID     MeasurableID
                    --1                 2017
                    --1                 2018
                    --2                 2021
                    --3                 2023

1 个答案:

答案 0 :(得分:2)

我建议使用函数,而不是存储过程 它工作得更快:

1)没有循环,将在一个查询中重试数据

2)具有内联函数查询优化器可以优化整个查询

以下是如何使用函数执行此操作的示例:

CREATE FUNCTION dbo.Func1 (
    @Country    INT
)
RETURNS TABLE
RETURN
    SELECT
        dbo.Measurables.OrganizationID,
        dbo.Measurables.ID AS MeasurableID
    FROM dbo.Measurables
...

CREATE FUNCTION dbo.Func2 (
    @OrganizationID  INT,
    @MeasurableID    INT
)
RETURNS TABLE
RETURN
    WITH 
        LastScore as ( SELECT Entities.ID, Results.Score,
                             Results.UTC,   
...


SELECT *
FROM dbo.Func1(@Country)                    F1
CROSS APPLY dbo.Func2(F1.OrganizationID, F1.MeasurableID) F2