SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Call sp from a Case statement


Call sp from a Case statement

Author
Message
Vitor da Fonseca
Vitor da Fonseca
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 50
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
Thom A
Thom A
SSC Guru
SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)

Group: General Forum Members
Points: 78215 Visits: 19839
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 :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Vitor da Fonseca
Vitor da Fonseca
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 50
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
[code language="sql"]
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
;
[/code]

Again, many thanks for the help.


All help and Any help is appreciated
Thom A
Thom A
SSC Guru
SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)

Group: General Forum Members
Points: 78215 Visits: 19839
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 :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
anthony.green
anthony.green
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99662 Visits: 8691
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








How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Vitor da Fonseca
Vitor da Fonseca
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 50
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
anthony.green
anthony.green
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99662 Visits: 8691
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



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Vitor da Fonseca
Vitor da Fonseca
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 50
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
anthony.green
anthony.green
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99662 Visits: 8691
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.




How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Vitor da Fonseca
Vitor da Fonseca
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 50
[quote]
anthony.green - Monday, December 4, 2017 7:08 AM
[quote]
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:
[/quote]

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.
[/quote]

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


All help and Any help is appreciated
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search