Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2003
    Location
    Belfast
    Posts
    16

    Unanswered: Problem with Multiple select cases...

    I've got a stored procedure that handles some decisions, based on two parameters being sent to it...an ID (@ID) and a filter nvarchar string e.g. 'all'

    can anyone tell me what is wrong with this statement? Or a better way to achieve what I want. Executing a string literal doesn't really bode that well with me so all advice welcome.

    mssql server

    Code:
    SELECT @query = 
    	CASE @filterSection
    		WHEN 'all' THEN 'SELECT jobShortDesc,jobStartImmediate,jobDesc,jobId,jobTitle,jobStart,jobEnd,jobPersist,jobReference,fld_locationName,fld_applicationType,fld_applicationData,jobapplicationMethod FRom tblRecruitmentJob INNER JOIN tblRecruitmentLocation ON jobLocationId = fld_locationid INNER JOIN tblRecruitmentApplicationMethod ON jobApplicationMethod = fld_applicationId ORDER BY jobStart DESC,fld_locationName ASC;'
    		WHEN 'alldate' THEN 'SELECT jobShortDesc,jobStartImmediate,jobDesc,jobId,jobTitle,jobStart,jobEnd,jobPersist,jobReference,fld_locationName,fld_applicationType,fld_applicationData,jobapplicationMethod FRom tblRecruitmentJob INNER JOIN tblRecruitmentLocation ON jobLocationId = fld_locationid INNER JOIN tblRecruitmentApplicationMethod ON jobApplicationMethod = fld_applicationId WHERE (jobPersist = 1 AND (jobStart<=dbo.StripTimeFromDate(GetDate()) AND jobEnd>=dbo.StripTimeFromDate(GetDate()))) OR jobPersist=0 ORDER BY jobStart DESC,fld_locationName ASC;'
    		WHEN 'category' THEN 'SELECT jobShortDesc,jobStartImmediate,jobDesc,jobId,jobTitle,jobStart,jobEnd,jobPersist,jobReference,fld_locationName,fld_applicationType,fld_applicationData,jobapplicationMethod FRom tblRecruitmentJob INNER JOIN tblRecruitmentLocation ON jobLocationId = fld_locationid INNER JOIN tblRecruitmentDepartments ON jobDepartmentId =fld_depId INNER JOIN tblRecruitmentApplicationMethod ON jobApplicationMethod = fld_applicationId WHERE jobDepartmentId=' + @ID + ' ORDER BY jobStart DESC,fld_locationName ASC;'
    		WHEN 'categorydate' THEN 'SELECT jobShortDesc,jobStartImmediate,jobDesc,jobId,jobTitle,jobStart,jobEnd,jobPersist,jobReference,fld_locationName,fld_applicationType,fld_applicationData,jobapplicationMethod FRom tblRecruitmentJob INNER JOIN tblRecruitmentLocation ON jobLocationId = fld_locationid INNER JOIN tblRecruitmentDepartments ON jobDepartmentId =fld_depId INNER JOIN tblRecruitmentApplicationMethod ON jobApplicationMethod = fld_applicationId WHERE ((jobPersist = 1 AND (jobStart<=dbo.StripTimeFromDate(GetDate()) AND jobEnd>=dbo.StripTimeFromDate(GetDate()))) OR jobPersist=0) AND jobDepartmentId=' +@ID+ ' ORDER BY jobStart DESC,fld_locationName ASC;'
    		ELSE
    		''
    	END
    
    EXEC (@query)

    Executing this query results in the following...

    Code:
    Conversion failed when converting the varchar value 'SELECT jobShortDesc,jobStartImmediate,jobDesc,jobId,jobTitle,jobStart,jobEnd,jobPersist,jobReference,fld_locationName,fld_applicationType,fld_applicationData,jobapplicationMethod FRom tblRecruitmentJob INNER JOIN tblRecruitmentLocation ON jobLocationId = fld_locationid INNER JOIN tblRecruitmentApplicationMethod ON jobApplicationMethod = fld_applicationId ORDER BY jobStart DESC,fld_locationName ASC;' to data type int.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    ...' + Convert(varchar(11), @id) + '...
    But I forsee a bigger problem, the solution to which is to create 4 other sprocs (one per different SQL statement) (none of which use dynamic SQL!) and call those sprocs based on the parameter value!

    The reason for this is because of cached query plans and performance
    George
    Home | Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •