[dba-SQLServer] Stored Procedure help

John Colby jwcolby at gmail.com
Thu Oct 8 10:50:51 CDT 2015


I have a stored procedure where I built up a bunch of common table
expressions to do various things related to counting data.

At the bottom I have a bunch of Select statements which I would like to
select ONE from by passing in a parameter.

ALTER PROCEDURE [dbo].[usp_MoveDistance]
 @FromYrMo as char(6), @ToYrMo as char(6)

A bunch of CTEs here

--SELECT * FROM cteMoveNewOld
--SELECT * FROM cteMoveNewOldWithRowNum
--SELECT * FROM cteMoveLast
--SELECT * from cteMoveLastWithDistance
SELECT * FROM cteMoveLastDistance_GrpBySt_DistanceCode
--SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode
--Select * from cteMoveLastDistance_GrpByStNew_StOld

My current method is to come in and uncomment one line, run the SP, comment
that out, uncomment another, run that.


So I want to add a new input variable to pass in a report name and use that
to select one of the CTEs.

ALTER PROCEDURE [dbo].[usp_MoveDistance]
@RptName varchar(50), @FromYrMo as char(6), @ToYrMo as char(6)

AFAICT the ONLY thing I can do as the last line of the SP is a select,
which is why I currently uncomment one of the lines to run it.

Is there any way to use an IF as the last line (block) to select one of the
selects to return data?


-- 
John W. Colby
Colby Consulting


More information about the dba-SQLServer mailing list