Share via

Stored Procedure - Get value from another SP

NachitoMax 416 Reputation points
2022-08-11T18:52:26.76+00:00

Hey

Im sure this is possible, i just need to find the right way to do it.

Overview
An existing database created by a previous guy years ago, converted from Access. I cannot change the database or table structures.

Requirement
Return job info from a job table using the job_id
add a boolean value to the return row where only a string value can be captured from another stored procedure

Stored Procedure 1

ALTER PROC [dbo].[usp_GetJobList]   
    @job_id int = null,  
 @thermal_break bit = False  
  
AS   
 SET NOCOUNT ON   
 SET XACT_ABORT ON    
  
SELECT dbo.job.job_id, dbo.job.name, dbo.job.short_description, @thermal_break AS thermal_break  
FROM        dbo.job  
WHERE dbo.job.job_id = @job_id  
ORDER BY     dbo.job.job_id DESC  

Stored procedure 2

ALTER PROC [dbo].[usp_GetThermalBreak]   
    @job_id int  = NULL  
AS   
 SET NOCOUNT ON   
 SET XACT_ABORT ON    
  
SELECT       dbo.lookup_in_ex.description_memo AS thermal_break  
FROM         dbo.job_in_ex INNER JOIN  
             dbo.lookup_in_ex ON dbo.job_in_ex.in_ex_id = dbo.lookup_in_ex.id  
WHERE       (dbo.job_in_ex.job_id = @job_id) AND (dbo.job_in_ex.in_ex_id = 43)  

Objective
GetJobList returns a single row. I need to collect the thermal_break value from GetThermalBreak and set @greg@thermalcloudsolutions.com _break (or an alternative method)

GetThermalBreak returns a single string value that is either 'thermal break' or nothing WHERE job_id = @Job _id

i need to get the value from GetThermalBreak
If the return value = 'thermal_break', return True
Else return False

How can i set this up in procedure 1 so that i can call GetThermalBreak using the job_id and return its value as a true or false boolean and then add the return value to the datarow so i get something like

job_id name thermal_break
123456 some job description True
246810 some other job False

Thanks

Azure SQL Database
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Isabellaz-1451 3,616 Reputation points
2022-08-16T02:18:16.89+00:00

Hi

Base on Erland's provided thread,you can merge the 2 SP,like below:

ALTER PROC [dbo].[usp_GetJobList]   
     @job_id int = null  
      
 AS   
  SET NOCOUNT ON   
  SET XACT_ABORT ON    
  
 DECLARE @thermal_break bit   
BEGIN TRANSACTION  
SELECT  @thermal_break = top 1 dbo.lookup_in_ex.description_memo AS thermal_break  
 FROM         dbo.job_in_ex INNER JOIN  
              dbo.lookup_in_ex ON dbo.job_in_ex.in_ex_id = dbo.lookup_in_ex.id  
 WHERE       (dbo.job_in_ex.job_id = @job_id) AND (dbo.job_in_ex.in_ex_id = 43)  
COMMIT TRANSACTION  
  
 SELECT dbo.job.job_id, dbo.job.name, dbo.job.short_description, @thermal_break AS thermal_break  
 FROM        dbo.job  
 WHERE dbo.job.job_id = @job_id  
 ORDER BY     dbo.job.job_id DESC  

Best Regards,
Isabella

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 Reputation points
    2022-08-16T05:45:46.777+00:00

    Hi @NachitoMax
    If you want to pass value from one SP to another SP, then you can try OUTPUT, like this:

     ALTER PROC [dbo].[usp_GetThermalBreak]   
         @job_id int   
         @thermal_break varchar(20) OUTPUT  
     AS   
      SET NOCOUNT ON   
      SET XACT_ABORT ON    
       
     SELECT @thermal_break = CASE WHEN dbo.lookup_in_ex.description_memo ='thermal_break'   
                                  THEN 'TRUE'  
                                  ELSE 'FALSE' END  
     FROM dbo.job_in_ex INNER JOIN dbo.lookup_in_ex ON dbo.job_in_ex.in_ex_id = dbo.lookup_in_ex.id  
     WHERE (dbo.job_in_ex.job_id = @job_id) AND (dbo.job_in_ex.in_ex_id = 43)  
     GO  
          
     ALTER PROC [dbo].[usp_GetJobList]   
         @job_id int = null  
     AS  
     BEGIN  
       SET NOCOUNT ON   
       SET XACT_ABORT ON    
       DECLARE @thermal_break varchar(20)  
       EXEC usp_GetThermalBreak @job_id, @thermal_break OUTPUT  
         
       SELECT dbo.job.job_id, dbo.job.name, dbo.job.short_description, @thermal_break AS thermal_break  
       FROM dbo.job  
       WHERE dbo.job.job_id = @job_id  
       ORDER BY dbo.job.job_id DESC  
     END  
     GO  
    

    Best regards,
    LiHong

    Was this answer helpful?

    0 comments No comments

  2. Erland Sommarskog 134.4K Reputation points MVP Volunteer Moderator
    2022-08-11T21:26:32.913+00:00

    The most direct solution may be to use INSERT @Bluemchen (...) EXEC sp to capture the output, but this method is fragile.

    I have an article on my web site How to Share Data between Stored Procedures that discuss the different options in more detail.

    Was this answer helpful?

    0 comments No comments

  3. Tom Phillips 17,786 Reputation points
    2022-08-11T20:30:36.37+00:00

    If those are just select statements, it would be better and easier to use as a view or a function.

    Was this answer helpful?

    0 comments No comments

  4. ENGIN SENOL 1 Reputation point
    2022-08-11T19:37:11.663+00:00
    • Create a Function (second_sp_function) to call second SP with Openquery with one parameter (JobID)
      select * from openquery(Server, 'exec sp_who')
    • on the first query use "case when second_sp_function(JobID) = 'thermal_break' then True else False end as thermal_break" instead @greg@thermalcloudsolutions.com _break AS thermal_break

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.