0

顧客の通話を発信者に割り当てるためのストアド プロシージャを作成しようとしています。変数 @team_id で識別されるコーラーのチームがいくつかあります。私がやりたいことは、顧客 (@code) に誰かが割り当てられているかどうかを確認することです。もしそうなら、その人のIDを返してください。そうでない場合は、割り当て先を決定する THEN ステートメントを実行し、レコードをその発信者に更新して、その発信者の ID を返します。これが私が持っているものですが、選択内で更新を実行できません。可能であれば、毎回テーブルを更新する (ストアド プロシージャの最後に update 句を追加する) ことは避けたいと思います。

declare @team_id char(3), @code char(4)
--If team is an intake department
if (@team_id IN ('03V', '09X'))
SELECT 
    CASE 
        WHEN a.intake_caller_id IS NOT NULL and a.intake_caller_id <> '' THEN a.intake_caller_id
        ELSE
            (
                SELECT employee_id
                FROM 
                (
                    SELECT TOP 1 COUNT(a.id) as count, a.employee_id
                    FROM event.dbo.event a
                    JOIN event.dbo.event_triage b ON a.employee_id = b.employee_id
                    WHERE task_id in ('WS', 'WF', 'WT', 'WU' ) and a.status = 1 AND b.team_id = @team_id
                    GROUP BY a.employee_id
                    ORDER BY count ASC
                ) a
                update event.dbo.referral_data
                SET intake_caller_id = a.employee_id
                WHERE CODE_ = @code
            )
        END
    FROM  event.dbo.referral_data a 
    WHERE CODE_ = @code
ELSE 
--if team is a PO department
IF (@team_id IN ('00R', '154'))
    SELECT 
        CASE
            WHEN @team_id = '00R' AND intake_rx_caller_id IS NOT NULL AND intake_rx_caller_id <> '' THEN intake_rx_caller_id
            WHEN @team_id = '00R' AND (intake_rx_caller_id IS NULL OR intake_rx_caller_id = '') THEN 
                (
                    SELECT employee_id
                    FROM 
                    (
                        SELECT top 1 COUNT(a.id) as count, a.employee_id
                        FROM event.dbo.event a 
                        JOIN event.dbo.event_triage b ON a.employee_id = b.employee_id
                        WHERE task_id IN ('WR', 'CR') AND status = 1 and b.team_id = '00R'
                        GROUP BY a.employee_id
                        ORDER BY count ASC
                    ) a
                )
            WHEN @team_id = '154' AND reorder_rx_caller_id IS NOT NULL AND reorder_rx_caller_id <> '' THEN reorder_rx_caller_id
            WHEN @team_id = '154' AND (reorder_rx_caller_id IS NULL OR reorder_rx_caller_id = '') THEN
                (
                    SELECT employee_id
                    FROM
                    (
                        SELECT top 1 COUNT(a.id) as count, a.employee_id
                        FROM event.dbo.event a 
                        JOIN event.dbo.event_triage b ON a.employee_id = b.employee_id
                        WHERE task_id IN ('RS', 'RY') AND status = 1 and b.team_id = '154'
                        GROUP BY a.employee_id
                        ORDER BY count ASC
                    )a
                )
            END
    FROM event.dbo.doctor_data 
    WHERE CODE_ = @code 
4

1 に答える 1

0

最初に更新してから、最初の IF ステートメント内で選択しないでください。同じ条件付きロジックを update ステートメントに適用できます。

編集:ここにコードがあります。ただし、テストされていません....

DECLARE @team_id CHAR(3)
, @code CHAR(4) 
 IF ( @team_id IN ( '03V' , '09X' ) ) 
  BEGIN

        ;WITH  CTE
                AS ( SELECT TOP 1
                                COUNT(a.id) AS count
                              , a.employee_id
                     FROM       event.dbo.event a
                                JOIN event.dbo.event_triage b
                                      ON a.employee_id = b.employee_id
                     WHERE      task_id IN ( 'WS' , 'WF' , 'WT' , 'WU' )
                                AND a.status = 1
                                AND b.team_id = @team_id
                     GROUP BY   a.employee_id
                     ORDER BY   count ASC
                   )
              UPDATE      a
              SET         intake_caller_id = ( SELECT employee_id FROM CTE)
              FROM        event.dbo.referral_data AS a
              WHERE       CODE_ = @code
                          AND ISNULL(a.intake_caller_id,'')=''                                     

        SELECT      a.intake_caller_id
        FROM        event.dbo.referral_data a
        WHERE       CODE_ = @code
  END
ELSE  
  IF ( @team_id IN ( '00R' , '154' ) ) 
        BEGIN
              SELECT      CASE WHEN @team_id = '00R'
                                    AND intake_rx_caller_id IS NOT NULL
                                    AND intake_rx_caller_id <> '' THEN intake_rx_caller_id
                               WHEN @team_id = '00R'
                                    AND ( intake_rx_caller_id IS NULL
                                          OR intake_rx_caller_id = ''
                                        ) THEN ( SELECT employee_id
                                                 FROM   ( SELECT TOP 1
                                                                    COUNT(a.id) AS count
                                                                  , a.employee_id
                                                          FROM      event.dbo.event a
                                                                    JOIN event.dbo.event_triage b
                                                                          ON a.employee_id = b.employee_id
                                                          WHERE     task_id IN ( 'WR' , 'CR' )
                                                                    AND status = 1
                                                                    AND b.team_id = '00R'
                                                          GROUP BY  a.employee_id
                                                          ORDER BY  count ASC
                                                        ) a
                                               )
                               WHEN @team_id = '154'
                                    AND reorder_rx_caller_id IS NOT NULL
                                    AND reorder_rx_caller_id <> '' THEN reorder_rx_caller_id
                               WHEN @team_id = '154'
                                    AND ( reorder_rx_caller_id IS NULL
                                          OR reorder_rx_caller_id = ''
                                        ) THEN ( SELECT employee_id
                                                 FROM   ( SELECT TOP 1
                                                                    COUNT(a.id) AS count
                                                                  , a.employee_id
                                                          FROM      event.dbo.event a
                                                                    JOIN event.dbo.event_triage b
                                                                          ON a.employee_id = b.employee_id
                                                          WHERE     task_id IN ( 'RS' , 'RY' )
                                                                    AND status = 1
                                                                    AND b.team_id = '154'
                                                          GROUP BY  a.employee_id
                                                          ORDER BY  count ASC
                                                        ) a
                                               )
                          END
              FROM        event.dbo.doctor_data
              WHERE       CODE_ = @code 
        END
于 2012-08-27T18:33:43.383 に答える