Here is the OP's formatted so we can read it.
ALTER PROCEDURE [dbo].[zianGetAllJobsSearched10_1_13FAST]
-- Add the parameters for the stored procedure here
@Title VARCHAR(250)
,@Industry INT
,@Industry2 INT
,@Industry3 INT
,@Date INT
,@JobTitle INT
,@JobType INT
,@Experience INT
,@Education INT
,@State INT
,@City INT
,@Salary INT
,@MaxSalary INT
,@fromRec INT
,@toRec INT
,@SortType VARCHAR(50)
,@SortOrder VARCHAR(10)
WITH RECOMPILE
AS
DECLARE @ActualDate DATETIME = cast((
(
SELECT dateadd(d, @Date, GETDATE())
)
) AS VARCHAR(20));
IF (@Title = '')
SET @Title = NULL;
WITH EMPLOYMENT_OPPORTUNITIES_CTE (
id
,title
,contactperson
,lastmodified
,description
,workexperiencetypeid
,workexperience
,jobtypeid
,AcademicExperienceTypeId
,industryid
,industryid2
,industryid3
,salaryminid
,salarymaxid
,
--jobTitle,
city
,STATE
,PostalCode
,positions
,lastmodified2
)
--,deadline)
AS (
SELECT e.id
,title
,contactperson
,lastmodified
,e.description
,workexperiencetypeid
,isnull((
SELECT we.[Name]
FROM workexperiencetypes we
WHERE we.id = e.workexperiencetypeid
), '') AS workexperience
,isnull((
SELECT jot.[Name]
FROM jobtypes jot
WHERE jot.id = e.jobtypeid
), '') AS jobtype
,isnull((
SELECT edu.[Name]
FROM Degree edu
WHERE edu.Id = e.AcademicExperienceTypeId
), '') AS education
,isnull((
SELECT ind.[Name]
FROM industries ind
WHERE ind.id = e.industryid
), '') AS industryname
,isnull((
SELECT ind.[Name]
FROM industries ind
WHERE ind.id = e.industryid2
), '') AS industryname2
,isnull((
SELECT ind.[Name]
FROM industries ind
WHERE ind.id = e.industryid3
), '') AS industryname3
,salaryminid
,salarymaxid
,
--isnull((select jt.[Name] from jobTitles jt where jt.id=e.jobtypeid),'') as jobTitle,
isnull((
SELECT ci.[Name]
FROM cities ci
WHERE ci.id = c.cityid
), '') AS city
,isnull((
SELECT r.[AbbreviatedName]
FROM regions r
WHERE r.id = c.regionid
), '') AS STATE
,isnull((
SELECT comp.[PostalCode]
FROM Companys comp
WHERE comp.Id = c.id
), '') AS PostalCode
,positions
,substring(cast(e.LastModified AS VARCHAR(20)), 1, 12) AS lastmodified2
--ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RowNumber
FROM EmploymentOpportunities e WITH (NOLOCK)
--left outer join companys c on e.officeid=c.id
--inner join companys c on e.officeid=c.id
INNER JOIN companys c ON e.companyid = c.id
WHERE (
@Title IS NULL
OR title = @Title
)
AND (
@Industry = 0
OR e.industryid = @Industry
)
AND (
@Industry2 = 0
OR Industryid2 = @Industry2
)
AND (
@Industry3 = 0
OR Industryid3 = @Industry3
)
AND (
@Date = 0
OR lastmodified >= @Date
)
AND lastmodified IS NOT NULL
AND lastmodified > @ActualDate
--and e.LastModified >''' + cast(((select dateadd(d,@Date,GETDATE()))) as varchar(20)) + ''''
--and (@JobTitle = 0 OR title = @JobTitle)
AND (
@JobType = 0
OR jobtypeid = @JobType
)
AND (
@Experience = 0
OR workexperiencetypeid = @Experience
)
AND (
@education = 0
OR academicexperiencetypeid = @education
)
AND (
@State = 0
OR c.RegionId = @State
)
AND (
@City = 0
OR c.CityId = @City
)
AND (
@Salary = 0
OR SalaryMinID >= @Salary
)
AND (
@MaxSalary = 0
OR SalaryMaxID <= @MaxSalary
)
)
SELECT id
,title AS Title
,contactperson AS ContactPerson
,lastmodified
,description AS Description
,workexperience
,jobtypeid AS jobtype
,academicexperiencetypeid AS education
,industryid AS industryname
,industryid2 AS industryname2
,industryid3 AS industryname3
,salaryminid AS salary
,salarymaxid AS maxsalary
,
--jobtitle as jobTitle,
city + ', ' + STATE + ', ' + PostalCode AS Location
,positions AS Positions
,
--deadline,
rownumber AS RowNumber
,
--(select COUNT(*) from EMPLOYMENT_OPPORTUNITIES_CTE) as TotalCount FROM (
--(SELECT
-- Total_Rows= SUM(st.row_count)
--FROM
-- sys.dm_db_partition_stats st
--WHERE
-- object_name(object_id) = 'EMPLOYMENT_OPPORTUNITIES_CTE' AND (index_id < 2))
-- as TotalCount FROM (
(
SELECT rowcnt
FROM sys.sysindexes
WHERE id = OBJECT_ID('EMPLOYMENT_OPPORTUNITIES_CTE')
AND (
indid = 0
OR indid = 1
)
) AS TotalCount
FROM (
SELECT id
,title
,contactperson
,lastmodified
,description
,workexperience
,jobtypeid
,academicexperiencetypeid
,industryid
,industryid2
,industryid3
,salaryminid
,salarymaxid
,
--jobtitle,
city
,STATE
,PostalCode
,
--Location,
positions
,
--deadline,
ROW_NUMBER() OVER (
ORDER BY LastModified DESC
) RowNumber
FROM EMPLOYMENT_OPPORTUNITIES_CTE
) p1
WHERE RowNumber BETWEEN @fromRec
AND @toRec
ORDER BY CASE
WHEN @SortType = 'LastModified'
AND @SortOrder = 'DESC'
THEN LastModified
END DESC
,CASE
WHEN @SortType = 'LastModified'
AND @SortOrder != 'DESC'
THEN LastModified
END
,CASE
WHEN @SortType = 'City'
AND @SortOrder = 'DESC'
THEN City
END DESC
,CASE
WHEN @SortType = 'City'
AND @SortOrder != 'DESC'
THEN City
END
,CASE
WHEN @SortType = 'State'
AND @SortOrder = 'DESC'
THEN STATE
END DESC
,CASE
WHEN @SortType = 'State'
AND @SortOrder != 'DESC'
THEN STATE
END
,CASE
WHEN @SortType = 'Title'
AND @SortOrder = 'DESC'
THEN Title
END DESC
,CASE
WHEN @SortType = 'Title'
AND @SortOrder != 'DESC'
THEN Title
END
OPTION (MAXDOP 8)
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/