Call sp from a Case statement

  • Hi,

    I am trying to execute a Stored Procedure after two other jobs in the schedule have completed successfully, so I am looking to a specific filed (DW_CREATED_DATE) which is the date that the data loaded.
    This is what I have done, but it seems that the EXECUTE do not work within the CASE statement, so I am looking for help on the solution.

    [code language="sql"]
    SELECT
    CASE 
           WHEN cast(A.[DW_CREATED_DATE] as date) = '2017-12-03' AND cast(B.[DW_CREATED_DATE] as date) = '2017-12-03' THEN EXEC sp_ChurnRate 
    else NULL end

    FROM TABLE_A A
           INNER JOIN TABLE_B B ON CAST(B.[DW_CREATED_DATE] AS DATE) = CAST(A.[DW_CREATED_DATE] AS DATE) 
    [/code]
    When running the above, I get the error message:
    << Incorrect syntax near the keyword 'EXEC'. >>
    Any help and all help is very much appreciated.
    Thanks in advance,

    All help and Any help is appreciated

  • What are you trying to do here exactly??? You can't execute a stored procedure inside a SELECT statement. At a pure guess, maybe what you're after is a (Table Value) function, however, without knowing what sp_ChurnRate does, and how it related to the rest of your statement, that;s about all the advice I can give.

    You'd be better off providing the SQL for sp_ChurnRate, which would be a start. We may then also need DDL for your Table, as well as consumable sample data and your expected output.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Hi, Many thanks for your reply. What I'm trying to do is running a script/code which I put in a ‘Stored Procedure’ after the other two jobs have completed successfully

    This is the stored procedure, which delete all records from the table and then insert the most recent information in the table

    AS
    DELETE FROM [Imart].[ChurnRate]
    INSERT INTO [Imart].[ChurnRate]
    SELECT
        BASE.SUBS_WEEK_AND_YEAR, DAY_ID, DATE, CHURN.BB_PACKAGE ,CHURN.DTH_CUSTOMER_TYPE, CURRWK, LASTWK, TOTAL_CHURN    
    FROM
    (select
        subs_week_and_Year, Day_ID, cast(CALENDAR_DATE as date) date, BB_PACKAGE_DESC, sum(CURR_WEEK) currwk, sum(LAST_WEEK) LASTWK,DTH_CUSTOMER_TYPE
    from dbo.Base
    group by
        subs_week_and_Year, Day_ID, cast(CALENDAR_DATE as date), BB_PACKAGE_DESC, DTH_CUSTOMER_TYPE
    ) BASE
    left JOIN
    (SELECT
        SUBS_WEEK_AND_YEAR, BB_PACKAGE, DTH_CUSTOMER_TYPE, SUM(TOTAL_RECORDS) TOTAL_CHURN
    FROM Imart.BB_Churn
    GROUP BY
        SUBS_WEEK_AND_YEAR, BB_PACKAGE, DTH_CUSTOMER_TYPE
    ) CHURN ON CHURN.SUBS_WEEK_AND_YEAR = BASE.SUBS_WEEK_AND_YEAR
            AND CHURN.BB_PACKAGE = BASE.BB_PACKAGE_DESC
            and churn.DTH_CUSTOMER_TYPE = base.DTH_CUSTOMER_TYPE
    ORDER BY
    BASE.subs_week_and_Year ASC
    ;

    Again, many thanks for the help.

    All help and Any help is appreciated

  • Wait... That SP is only doing deletes and inserts..? What would you be expecting your unnamed column to be returning in your select statement, considering that your SP isn't returning any data.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Monday, December 4, 2017 6:22 AM

    Wait... That SP is only doing deletes and inserts..? What would you be expecting your unnamed column to be returning in your select statement, considering that your SP isn't returning any data.

    Think this is more workflow, so instead of SELECT CASE should be an IF statement.

    Vitor da Fonseca - Monday, December 4, 2017 6:08 AM

    Hi, Many thanks for your reply. What I'm trying to do is running a script/code which I put in a ‘Stored Procedure’ after the other two jobs have completed successfully
      

    What is the logic behind the two jobs, how do they execute, is it via SQL Agent, windows task scheduler or some other methods?

    If they are automated jobs like the SQL Agent, just add another step and do an IF check 

    IF This = That
    BEGIN
    EXEC sp_churnrate
    END

  • The SP delete all records from the table "Imart.ChurnRate" and then inserts information from two other different tables with these column names:

    BASE.SUBS_WEEK_AND_YEAR, DAY_ID, DATE, CHURN.BB_PACKAGE ,CHURN.DTH_CUSTOMER_TYPE, CURRWK, LASTWK, TOTAL_CHURN

    What I am trying to do is instead of guessing a time that the first two jobs are completed successfully, is to have a job which run the SQL to build the table Imart.ChurnRate.

    The first two jobs when they run, the column DW_CREATED_DATE is added with the date that it run, i.e. when they have run today the column will be populated for all records with today's date (04-12-2017), this also serves as a check, and off course if for any reason something went wrong I am able to run the query again for the date required.
    I hope that I've managed to explain, and please I apologise if my English is not the most correct...

    Thanks in advance,

    All help and Any help is appreciated

  • Vitor da Fonseca - Monday, December 4, 2017 6:45 AM

    The SP delete all records from the table "Imart.ChurnRate" and then inserts information from two other different tables with these column names:

    BASE.SUBS_WEEK_AND_YEAR, DAY_ID, DATE, CHURN.BB_PACKAGE ,CHURN.DTH_CUSTOMER_TYPE, CURRWK, LASTWK, TOTAL_CHURN

    What I am trying to do is instead of guessing a time that the first two jobs are completed successfully, is to have a job which run the SQL to build the table Imart.ChurnRate.

    The first two jobs when they run, the column DW_CREATED_DATE is added with the date that it run, i.e. when they have run today the column will be populated for all records with today's date (04-12-2017), this also serves as a check, and off course if for any reason something went wrong I am able to run the query again for the date required.
    I hope that I've managed to explain, and please I apologise if my English is not the most correct...

    Thanks in advance,

    If its a Job, add a 3rd step to run the stored proc

    So the logic would be
    Step1 - Populate Base Table, on success go to next step, on failure quit report failure
    Step 2 - Populate BB_Churn, on success go to next, on failure quit report failure
    Step 3 - Execute sp_churnrate, on success quit report success, on failure quit report failure

  • Not as simple as that, sorry
    Two distinct jobs which one with different steps, that is why I am having difficulties: crying:

    All help and Any help is appreciated

  • Vitor da Fonseca - Monday, December 4, 2017 7:01 AM

    Not as simple as that, sorry
    Two distinct jobs which one with different steps, that is why I am having difficulties: crying:

    Why not get the jobs to call each other, End of Job 1 Start Job 2, End of Job 2 Start Job 3

    Create a logic table, end of Job 1 Populate a table with a row of value 1.  end of Job 2 Populate a second row with value 1.  job 3 running always, if sum(value) = 2 exec sp_churnrate  reset the values to 0. rinse and repeat  or go with IF TableA.Date = CONVERT(DATE,GETDATE()) AND TableB.Date = CONVERT(DATE,GETDATE()) EXEC sp_churnrate.

    Problem with the latter option if the job is constantly running to check if the values match then the stored proc will always be running.

  • anthony.green - Monday, December 4, 2017 7:08 AM

    Vitor da Fonseca - Monday, December 4, 2017 7:01 AM

    Not as simple as that, sorry
    Two distinct jobs which one with different steps, that is why I am having difficulties: crying:

    Why not get the jobs to call each other, End of Job 1 Start Job 2, End of Job 2 Start Job 3

    Create a logic table, end of Job 1 Populate a table with a row of value 1.  end of Job 2 Populate a second row with value 1.  job 3 running always, if sum(value) = 2 exec sp_churnrate  reset the values to 0. rinse and repeat  or go with IF TableA.Date = CONVERT(DATE,GETDATE()) AND TableB.Date = CONVERT(DATE,GETDATE()) EXEC sp_churnrate.

    Problem with the latter option if the job is constantly running to check if the values match then the stored proc will always be running.

    Great, Many thanks,
    I will try to build something with your first solution, I hope I can do it :D, still a newbie when it comes to SQL

    All help and Any help is appreciated

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply