Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:28:44+00:00 2026-05-31T00:28:44+00:00

I want to create a procedure which returns a table on the basis of

  • 0

I want to create a procedure which returns a table on the basis of input of parmeters as fields and operator …how can I use operator selection which is passed as a parameter string

ALTER PROCEDURE dbo.sp_getStaffRecord
(
   @deptname varchar(50),
   @dob date,
   @active bit,
   @salary int,
   @firstname varchar(50),
   @lastname varchar(50),
   @OperatorDob varchar(2),
   @OperatorSalary varchar(2)
)
AS
    select 
        st.id, firstname, lastname, deptname, salary, dob,
        (select firstname + ', ' + lastname from StaffTable 
         where firstname = @firstname and lastname = @lastname) as [Reporting To],  
        doj, active 
    from 
        StaffTable st 
    inner join 
        DepartmentTable dt on dt.id = st.dept 
    where 
        dt.deptname = @deptname 
        and 
        (
            if (@OperatorDob = '>=')
               st.dob >= @dob  
            else if (@OperatorDob = '<=') 
               st.dob <= @dob
            else if (@OperatorDob = '=')
               st.dob = @dob
            else if (@OperatorDob = '>')
               st.dob > @dob
            else if (@OperatorDob = '<=')
               st.dob = @dob
         ) 
    and st.active = @active 
    and st.salary >= @salary

    RETURN
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:28:45+00:00Added an answer on May 31, 2026 at 12:28 am

    You can’t use IF..ELSE the way you want to. What you can do is combine the value of the @OperatorDob with the logic it should execute:

    WHERE
       dt.deptname = @deptname 
     AND
    (
      (@OperatorDob = '>=' AND st.dob >= @dob)
      OR
      (@OperatorDob = '<=' AND st.dob <= @dob)
      OR
      (@OperatorDob = '=' AND st.dob = @dob)
      OR
      (@OperatorDob = '>' AND st.dob > @dob)
      OR
      (@OperatorDob = '<=' AND st.dob = @dob)
    )
     AND st.active = @active 
     AND st.salary >= @salary
    

    If the @OperatorDob value happens not to be any of those specified above (e.g. '!=') then the query won’t produce any results. This might be a desirable side-effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call the Stored Procedure which returns a table in the Table
I have a stored procedure in which i want to create a user defined
I'm using a stored procedure to create a temporary table and want to return
How can I create an Oracle stored procedure which accepts a variable number of
I want to create a stored procedure which takes integer values as a @top
I want to create a stored procedure (on SQL Server 2005) that fetches a
I want to make this kind of query: create procedure something @name varchar(13) as
I want to create a function that performs a function passed by parameter on
I have sql stored procedure which pass and return some values: CREATE PROCEDURE [dbo].[GetMoney]
i have a Stored Procedure which returns output as a ref cursor. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.