CREATE PROC Test_GetCollegeInfo @type int AS BEGIN DECLARE @where nvarchar(800),@v int,@sql nvarchar(max) IF @type>2 Begin SET @where='AND colleges.id='+cast(@type AS varchar(100)) END ELSE BEGIN SET @where='' END SET @sql=' SELECT * FROM collegeMajor INNER JOIN MajorsType ON collegeMajor.majorType=MajorsType.id INNER JOIN colleges ON colleges.id=collegeMajor.collageID WHERE 1=1 '+@where EXEC (@sql) End