Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:56:26+00:00 2026-06-10T08:56:26+00:00

I am trying to create a stored procedure for assigning call in customers to

  • 0

I am trying to create a stored procedure for assigning call in customers to callers. We have several teams of callers that are identified by a variable @team_id. What I want to do is check and see if the customer (@code) has someone assigned to them yet. If they do, return that person’s ID. If not, run the THEN statement that determines who it should be assigned to, update the record to that caller, and return that caller’s ID. Here is what I have, but it won’t let me run the update inside the select. I would like to avoid updating the table every time (adding an update clause to the end of the stored procedure) if I can.

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:56:27+00:00Added an answer on June 10, 2026 at 8:56 am

    Why dont you update first and then do a select inside first IF statement.
    You can apply the same conditional logic in update statement.

    Edit:here is the code.Not tested though….

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a stored procedure that utilizes a variable number of
I'm trying to create a stored procedure that will be used by a client
I have an already existing stored procedure. But when I am trying to create
2 Hi! I am trying to create stored procedure that gone return varchar value,
Is it possible to create a stored procedure that can call a function/generate C#
I am trying to create a stored procedure that when given a month value,
I'm trying to create a stored procedure that uses SELECT TOP 20 * from
I'm trying to create a stored procedure that inserts into a database table, but
I am trying to create a stored procedure that will take a row with
Example I'm trying to create a stored procedure you can call with or without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.