Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional where clause based on a stored procedure parameter?

I have a SQL Server 2005 stored proc that takes a parameter: @includeClosedProjects.

There's a WHERE clause that I want to control based on this param.

create proc sel_projects
(@incClosedRel int = 1)
as

SELECT projectId, projectName
FROM project
WHERE CompletionStatusCID NOT IN (34, 35) <-- controlled by @incClosedRel 

I want to get all projects (exclude the where clause), when @incClosedRel =1. Otherwise, include the where clause.

like image 700
kacalapy Avatar asked Nov 11 '10 21:11

kacalapy


Video Answer


1 Answers

SELECT projectId, projectName
FROM project
WHERE CompletionStatusCID NOT IN (34, 35) 
    Or @incClosedRel = 1
like image 57
Thomas Avatar answered Sep 30 '22 17:09

Thomas