Question

I am having two Stored Procedures of redundant code. The SPs will differ based on the table from which it is selected. I would like to combine those two SPs. Please help out.

The main SP is:

CREATE PROCEDURE [dbo].[spGetEmployeesBySearchString]            
-------------
-------------
@OtherListType_ID INT
@manager_employee_number VARCHAR(255)
-------------
-------------
DECLARE @IsGetFullTeamUnderManager bit=0
IF (@OtherListType_ID=3 AND @manager_employee_number IS NOT NULL)
    SET @IsGetFullTeamUnderManager = 1

IF (@IsGetFullTeamUnderManager=1)
BEGIN
    EXEC spFullTeamUnderManager <<Parameters>>
    RETURN
END

SELECT e.a,e.b,e.c,e.d,......
FROM Employee emp
INNER JOIN .....
WHERE ..........

The Second Stored Procedure is:

CREATE PROCEDURE [dbo].[spFullTeamUnderManager]            
-------------
-------------
-------------
SELECT e.a,e.b,e.c,e.d,......
FROM dbo.fnFullTeamUnderManager(@manager_employee_number) emp 
INNER JOIN .....
WHERE ..........

In the main SP, based on the value of @OtherListType_ID, I am calling the second SP. But the select statement, join conditions and the where conditions remains the same in both the Stored Procedures. I need to remove the redundant code. Please help out.

Was it helpful?

Solution

You can't, generally, parameterise the rowset sources in a FROM clause. You could do the following and hope the optimizer is smart enough to do it right:

SELECT e.a,e.b,e.c,e.d,......
FROM (
 SELECT * FROM dbo.fnFullTeamUnderManager(@manager_employee_number) WHERE @IsGetFullTeamUnderManager=1
 UNION ALL
 SELECT * FROM Employee WHERE @IsGetFullTeamUnderManager=0
) emp 
INNER JOIN .....
WHERE ..........

If the function and Employee don't share exactly the same columns, you'll have to name the common columns explicitly instead of using SELECT * - some might insist that you should do that anyway, but today I'm in a "get it done" mood.

OTHER TIPS

CREATE PROCEDURE [dbo].[spGetEmployeesBySearchString]            
-------------
-------------
@OtherListType_ID INT,
@manager_employee_number VARCHAR(255)
-------------
-------------
DECLARE @IsGetFullTeamUnderManager bit=0
IF (@OtherListType_ID=@FullTeam AND @manager_employee_number IS NOT NULL)
    SET @IsGetFullTeamUnderManager = 1

IF (@IsGetFullTeamUnderManager=1)
BEGIN    
    SELECT e.a,e.b,e.c,e.d,......
    FROM dbo.fnFullTeamUnderManager(@manager_employee_number) emp 
    INNER JOIN .....    
    WHERE ..........    
END

SELECT e.a,e.b,e.c,e.d,......
FROM Employee emp
INNER JOIN .....
WHERE ..........
CREATE PROCEDURE [dbo].[spGetEmployeesBySearchString]            
-------------
-------------
@OtherListType_ID INT
@manager_employee_number VARCHAR(255)
-------------
-------------

-- Me
DECLARE @SQL varchar(3000);
DECLARE @TableName varchar(30);
SET @TableName = 'Employee ';



DECLARE @IsGetFullTeamUnderManager bit=0
IF (@OtherListType_ID=3 AND @manager_employee_number IS NOT NULL)
    SET @IsGetFullTeamUnderManager = 1

IF (@IsGetFullTeamUnderManager=1)
BEGIN
    SET @TableName = 'dbo.fnFullTeamUnderManager(' + @manager_employee_number +') ';
END

SET @SQL = 'SELECT e.a,e.b,e.c,e.d
            FROM ' + @TableName + ' emp
            INNER JOIN .....
            WHERE a = ' + convert(varchar(4), @aNumber);

EXEC(@SQL);

You should also know there are better ways to execute dynamic sql statements.

Using EXEC SP_EXECUTESQL(@SQL) instead of EXEC(@SQL) is more likely to promote query plan reuse and improve security. In addition, using this approach you can ensure that the data values being passed into the query are the correct datatypes.

More Reading Execute Dynamic SQL commands in SQL Server

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top